From 5167a1319d93dc8592de3992f8b1a7941c0890c1 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Sat, 18 Apr 2020 21:16:30 -0400 Subject: [PATCH] 1.0.27 Use DataTracker --- CHANGELOG.md | 3 ++ README.md | 1 + .../block/DragonEggHolderBlockEntity.java | 2 +- .../block/TeleportationRestrictorBlock.java | 2 - .../reliccraft/client/RelicCraftClient.java | 2 - .../reliccraft/mixin/MixinEntity.java | 45 +++++++++++++------ .../reliccraft/mixin/MixinLivingEntity.java | 5 ++- .../mixin/MixinServerPlayerEntity.java | 16 ------- .../UpdateTeleportCooldownS2CPacket.java | 30 ------------- src/main/resources/reliccraft.mixins.json | 1 - 10 files changed, 39 insertions(+), 68 deletions(-) delete mode 100644 src/main/java/com/thebrokenrail/reliccraft/mixin/MixinServerPlayerEntity.java delete mode 100644 src/main/java/com/thebrokenrail/reliccraft/packet/UpdateTeleportCooldownS2CPacket.java diff --git a/CHANGELOG.md b/CHANGELOG.md index ccfdb6e..2d07e39 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +**1.0.27** +* Use DataTracker + **1.0.26** * Improve Teleportation Restrictor diff --git a/README.md b/README.md index c69ec1d..d1a77ab 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,7 @@ This mod was created for [ModFest 1.15](https://modfest.github.io/1.15/) * Teleportation Restrictors * Fueled by a Dragon Egg * 128x128x128 radius + * 4-Second Cooldown When Leaving Field * Teleportation Beacons * Fueled by a Dragon Egg * Dragon Eggs are Now Renewable diff --git a/src/main/java/com/thebrokenrail/reliccraft/block/DragonEggHolderBlockEntity.java b/src/main/java/com/thebrokenrail/reliccraft/block/DragonEggHolderBlockEntity.java index 78291f8..bc22074 100644 --- a/src/main/java/com/thebrokenrail/reliccraft/block/DragonEggHolderBlockEntity.java +++ b/src/main/java/com/thebrokenrail/reliccraft/block/DragonEggHolderBlockEntity.java @@ -97,7 +97,7 @@ public class DragonEggHolderBlockEntity extends BlockEntity implements Inventory if (hasWorld()) { assert getWorld() != null; Block block = getWorld().getBlockState(getPos()).getBlock(); - if (block instanceof AbstractDragonEggHolderBlock) { + if (block instanceof AbstractDragonEggHolderBlock && !getWorld().isClient()) { ((AbstractDragonEggHolderBlock) block).tick(getWorld(), getPos()); } } diff --git a/src/main/java/com/thebrokenrail/reliccraft/block/TeleportationRestrictorBlock.java b/src/main/java/com/thebrokenrail/reliccraft/block/TeleportationRestrictorBlock.java index c2d0e32..2cc7f3e 100644 --- a/src/main/java/com/thebrokenrail/reliccraft/block/TeleportationRestrictorBlock.java +++ b/src/main/java/com/thebrokenrail/reliccraft/block/TeleportationRestrictorBlock.java @@ -19,8 +19,6 @@ public class TeleportationRestrictorBlock extends AbstractDragonEggHolderBlock { boolean cannotTeleport(); void resetTeleportCooldown(); - - void setTeleportCooldown(byte cooldown); } public TeleportationRestrictorBlock() { diff --git a/src/main/java/com/thebrokenrail/reliccraft/client/RelicCraftClient.java b/src/main/java/com/thebrokenrail/reliccraft/client/RelicCraftClient.java index cc55f8a..5bfb94c 100644 --- a/src/main/java/com/thebrokenrail/reliccraft/client/RelicCraftClient.java +++ b/src/main/java/com/thebrokenrail/reliccraft/client/RelicCraftClient.java @@ -3,7 +3,6 @@ package com.thebrokenrail.reliccraft.client; import com.thebrokenrail.reliccraft.RelicCraft; import com.thebrokenrail.reliccraft.client.entity.RelicEntityRenderer; import com.thebrokenrail.reliccraft.item.RelicItem; -import com.thebrokenrail.reliccraft.packet.UpdateTeleportCooldownS2CPacket; import com.thebrokenrail.reliccraft.packet.UpdateTimeDilationS2CPacket; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.api.EnvType; @@ -57,6 +56,5 @@ public class RelicCraftClient implements ClientModInitializer { EntityRendererRegistry.INSTANCE.register(RelicCraft.RELIC_ENTITY, (entityRenderDispatcher, context) -> new RelicEntityRenderer(entityRenderDispatcher)); ClientSidePacketRegistryImpl.INSTANCE.register(new Identifier(RelicCraft.NAMESPACE, "update_time_dilation"), UpdateTimeDilationS2CPacket::handle); - ClientSidePacketRegistryImpl.INSTANCE.register(new Identifier(RelicCraft.NAMESPACE, "update_teleport_cooldown"), UpdateTeleportCooldownS2CPacket::handle); } } diff --git a/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinEntity.java b/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinEntity.java index 50da2e4..80bfe92 100644 --- a/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinEntity.java +++ b/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinEntity.java @@ -2,7 +2,13 @@ package com.thebrokenrail.reliccraft.mixin; import com.thebrokenrail.reliccraft.block.TeleportationRestrictorBlock; import net.minecraft.entity.Entity; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.data.DataTracker; +import net.minecraft.entity.data.TrackedData; +import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.nbt.CompoundTag; +import net.minecraft.server.world.ServerWorld; +import net.minecraft.world.World; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -17,24 +23,35 @@ public abstract class MixinEntity implements TeleportationRestrictorBlock.Telepo @Shadow protected boolean inNetherPortal; + @Shadow + public abstract World getEntityWorld(); + + @Shadow + public abstract DataTracker getDataTracker(); + @Unique - protected byte teleportCooldown = 2; + private static final byte DEFAULT_TELEPORT_COOLDOWN = 80; + + @Unique + private static final TrackedData TELEPORT_COOLDOWN = DataTracker.registerData(Entity.class, TrackedDataHandlerRegistry.BYTE); + + @Inject(at = @At("RETURN"), method = "") + public void init(EntityType type, World world, CallbackInfo info) { + getDataTracker().startTracking(TELEPORT_COOLDOWN, (byte) 0); + } @Override public void resetTeleportCooldown() { - teleportCooldown = 4; + getDataTracker().set(TELEPORT_COOLDOWN, (byte) 0); } - @Override - public void setTeleportCooldown(byte teleportCooldown) { - this.teleportCooldown = teleportCooldown; - } - - @Inject(at = @At("HEAD"), method = "tick") - public void tick(CallbackInfo info) { + @Inject(at = @At("RETURN"), method = "baseTick") + public void baseTick(CallbackInfo info) { updateNetherPortal(); - if (teleportCooldown > 0) { - teleportCooldown--; + byte data = getDataTracker().get(TELEPORT_COOLDOWN); + if (getEntityWorld() instanceof ServerWorld && data < DEFAULT_TELEPORT_COOLDOWN) { + data++; + getDataTracker().set(TELEPORT_COOLDOWN, data); } } @@ -54,17 +71,17 @@ public abstract class MixinEntity implements TeleportationRestrictorBlock.Telepo @Override public boolean cannotTeleport() { - return teleportCooldown > 0; + return getDataTracker().get(TELEPORT_COOLDOWN) < DEFAULT_TELEPORT_COOLDOWN; } @Inject(at = @At("HEAD"), method = "toTag") public void toTag(CompoundTag tag, CallbackInfoReturnable info) { - tag.putByte("RelicCraftTeleportCooldown", teleportCooldown); + tag.putByte("RelicCraftTeleportCooldown", getDataTracker().get(TELEPORT_COOLDOWN)); } @Inject(at = @At("HEAD"), method = "fromTag") public void fromTag(CompoundTag tag, CallbackInfo info) { - teleportCooldown = tag.getByte("RelicCraftTeleportCooldown"); + getDataTracker().set(TELEPORT_COOLDOWN, tag.getByte("RelicCraftTeleportCooldown")); updateNetherPortal(); } } diff --git a/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinLivingEntity.java b/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinLivingEntity.java index 29c4e1f..2ac1b3c 100644 --- a/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinLivingEntity.java +++ b/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinLivingEntity.java @@ -1,5 +1,6 @@ package com.thebrokenrail.reliccraft.mixin; +import com.thebrokenrail.reliccraft.block.TeleportationRestrictorBlock; import net.minecraft.entity.LivingEntity; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; @@ -8,10 +9,10 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @SuppressWarnings("unused") @Mixin(LivingEntity.class) -public class MixinLivingEntity extends MixinEntity { +public abstract class MixinLivingEntity { @Inject(at = @At("HEAD"), method = "teleport", cancellable = true) public void teleport(double x, double y, double z, boolean particleEffects, CallbackInfoReturnable info) { - if (cannotTeleport()) { + if (((TeleportationRestrictorBlock.TeleportingEntity) this).cannotTeleport()) { info.setReturnValue(false); } } diff --git a/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinServerPlayerEntity.java b/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinServerPlayerEntity.java deleted file mode 100644 index 34eee80..0000000 --- a/src/main/java/com/thebrokenrail/reliccraft/mixin/MixinServerPlayerEntity.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.thebrokenrail.reliccraft.mixin; - -import com.thebrokenrail.reliccraft.packet.UpdateTeleportCooldownS2CPacket; -import net.minecraft.server.network.ServerPlayerEntity; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -@Mixin(ServerPlayerEntity.class) -public class MixinServerPlayerEntity extends MixinEntity { - @Inject(at = @At("RETURN"), method = "playerTick") - public void playerTick(CallbackInfo info) { - UpdateTeleportCooldownS2CPacket.send((ServerPlayerEntity) (Object) this, teleportCooldown); - } -} diff --git a/src/main/java/com/thebrokenrail/reliccraft/packet/UpdateTeleportCooldownS2CPacket.java b/src/main/java/com/thebrokenrail/reliccraft/packet/UpdateTeleportCooldownS2CPacket.java deleted file mode 100644 index fe36e39..0000000 --- a/src/main/java/com/thebrokenrail/reliccraft/packet/UpdateTeleportCooldownS2CPacket.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.thebrokenrail.reliccraft.packet; - -import com.thebrokenrail.reliccraft.RelicCraft; -import com.thebrokenrail.reliccraft.block.TeleportationRestrictorBlock; -import io.netty.buffer.Unpooled; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; -import net.fabricmc.fabric.api.network.PacketContext; -import net.minecraft.network.packet.s2c.play.CustomPayloadS2CPacket; -import net.minecraft.server.network.ServerPlayerEntity; -import net.minecraft.util.Identifier; -import net.minecraft.util.PacketByteBuf; - -@SuppressWarnings("unused") -public class UpdateTeleportCooldownS2CPacket { - public static void send(ServerPlayerEntity player, byte cooldown) { - PacketByteBuf buf = new PacketByteBuf(Unpooled.buffer()); - buf.writeByte(cooldown); - - player.networkHandler.sendPacket(new CustomPayloadS2CPacket(new Identifier(RelicCraft.NAMESPACE, "update_teleport_cooldown"), buf)); - } - - @Environment(EnvType.CLIENT) - public static void handle(PacketContext context, PacketByteBuf buf) { - byte cooldown = buf.readByte(); - if (context.getPlayer() != null) { - ((TeleportationRestrictorBlock.TeleportingEntity) context.getPlayer()).setTeleportCooldown(cooldown); - } - } -} diff --git a/src/main/resources/reliccraft.mixins.json b/src/main/resources/reliccraft.mixins.json index b738468..f915779 100644 --- a/src/main/resources/reliccraft.mixins.json +++ b/src/main/resources/reliccraft.mixins.json @@ -19,7 +19,6 @@ "MixinLocateCommand", "MixinNetherStarItem", "MixinServerChunkManager", - "MixinServerPlayerEntity", "MixinServerWorld", "MixinThrownEnderpearlEntity", "MixinWorld"