Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Implement the PlayerEvent.Clone event #88

Merged
merged 2 commits into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion patchwork-events-entity/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
archivesBaseName = "patchwork-events-entity"
version = getSubprojectVersion(project, "0.2.0")
version = getSubprojectVersion(project, "0.3.0")

dependencies {
compile project(path: ':patchwork-fml', configuration: 'dev')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,42 @@ public Entity getTarget() {
}
}

/*
* Fired when the EntityPlayer is cloned, typically caused by the network sending a RESPAWN_PLAYER event.
* Either caused by death, or by traveling from the End to the overworld.
*/
public static class Clone extends PlayerEvent {
private final PlayerEntity original;
private final boolean wasDeath;

public Clone(PlayerEntity newPlayer, PlayerEntity oldPlayer, boolean wasDeath) {
super(newPlayer);
this.original = oldPlayer;
this.wasDeath = wasDeath;
}

/**
* @return The old EntityPlayer that this new entity is a clone of.
*/
public PlayerEntity getOriginal() {
return original;
}

/**
* True if this event was fired because the player died.
* False if it was fired because the entity switched dimensions.
*
* @return Whether this event was caused by the player dying.
*/
public boolean isWasDeath() {
return wasDeath;
}
}

/*TODO Events:
HarvestCheck
BreakSpeed
NameFormat
Clone
LoadFromFile
SaveToFile
Visibility
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package net.patchworkmc.mixin.event.entity;

import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -40,4 +42,11 @@ private void hookDeath(DamageSource source, CallbackInfo callback) {
callback.cancel();
}
}

@Inject(method = "copyFrom", at = @At("TAIL"))
private void hookCopyFromForCloneEvent(ServerPlayerEntity oldPlayer, boolean alive, CallbackInfo info) {
@SuppressWarnings("ConstantConditions")
ServerPlayerEntity speThis = (ServerPlayerEntity) (Object) this;
MinecraftForge.EVENT_BUS.post(new PlayerEvent.Clone(speThis, oldPlayer, !alive));
}
}