Use DataTracker
This commit is contained in:
parent
bb35c94d12
commit
5167a1319d
@ -1,5 +1,8 @@
|
||||
# Changelog
|
||||
|
||||
**1.0.27**
|
||||
* Use DataTracker
|
||||
|
||||
**1.0.26**
|
||||
* Improve Teleportation Restrictor
|
||||
|
||||
|
@ -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
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -19,8 +19,6 @@ public class TeleportationRestrictorBlock extends AbstractDragonEggHolderBlock {
|
||||
boolean cannotTeleport();
|
||||
|
||||
void resetTeleportCooldown();
|
||||
|
||||
void setTeleportCooldown(byte cooldown);
|
||||
}
|
||||
|
||||
public TeleportationRestrictorBlock() {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Byte> TELEPORT_COOLDOWN = DataTracker.registerData(Entity.class, TrackedDataHandlerRegistry.BYTE);
|
||||
|
||||
@Inject(at = @At("RETURN"), method = "<init>")
|
||||
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<CompoundTag> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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<Boolean> info) {
|
||||
if (cannotTeleport()) {
|
||||
if (((TeleportationRestrictorBlock.TeleportingEntity) this).cannotTeleport()) {
|
||||
info.setReturnValue(false);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -19,7 +19,6 @@
|
||||
"MixinLocateCommand",
|
||||
"MixinNetherStarItem",
|
||||
"MixinServerChunkManager",
|
||||
"MixinServerPlayerEntity",
|
||||
"MixinServerWorld",
|
||||
"MixinThrownEnderpearlEntity",
|
||||
"MixinWorld"
|
||||
|
Reference in New Issue
Block a user