From f2b823ab0aa9af99f8490a2bbde1c4af69bec3f0 Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Wed, 30 Oct 2024 16:22:55 +0100 Subject: [PATCH] fix: Stop using capability because they're no longer persistent on NeoForge which caused the mod to make you invulnerable --- .../mods/hardcorerevival/HardcoreRevival.java | 21 ---- .../HardcoreRevivalManager.java | 94 +++++++-------- ...pl.java => InMemoryPlayerRevivalData.java} | 86 ++++---------- .../InMemoryRevivalDataProvider.java | 95 +++++++++++++++ .../hardcorerevival/InternalMethodsImpl.java | 8 +- .../mods/hardcorerevival/MixinHooks.java | 8 +- .../PersistentRevivalDataProvider.java | 109 ++++++++++++++++++ .../PlayerHardcoreRevivalManager.java | 79 +++++++++++++ .../hardcorerevival/RevivalDataProvider.java | 37 ++++++ .../capability/HardcoreRevivalData.java | 42 ------- .../InvalidHardcoreRevivalData.java | 92 --------------- .../hardcorerevival/client/GuiHelper.java | 3 +- .../client/HardcoreRevivalClient.java | 21 ++-- .../client/HardcoreRevivalClientManager.java | 20 ---- .../command/ReviveCommand.java | 4 +- .../handler/KnockoutHandler.java | 19 ++- .../handler/KnockoutRestrictionHandler.java | 21 ++-- .../handler/KnockoutSyncHandler.java | 31 ++--- .../handler/LoginLogoutHandler.java | 25 ++-- .../handler/RescueHandler.java | 30 +++-- .../mixin/LivingEntityMixin.java | 6 +- .../hardcorerevival/mixin/PlayerMixin.java | 1 - .../network/AcceptFateMessage.java | 3 +- .../network/HardcoreRevivalDataMessage.java | 10 +- .../network/RescueMessage.java | 10 +- .../fabric/FabricHardcoreRevival.java | 1 - .../hardcorerevival/fabric/FabricPlayer.java | 2 - .../fabric/mixin/FabricPlayerMixin.java | 1 - .../hardcorerevival/ForgeHardcoreRevival.java | 4 - .../NeoForgeHardcoreRevival.java | 18 --- 30 files changed, 472 insertions(+), 429 deletions(-) rename common/src/main/java/net/blay09/mods/hardcorerevival/{capability/HardcoreRevivalDataImpl.java => InMemoryPlayerRevivalData.java} (50%) create mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryRevivalDataProvider.java create mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/PersistentRevivalDataProvider.java create mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/PlayerHardcoreRevivalManager.java create mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/RevivalDataProvider.java delete mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalData.java delete mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/capability/InvalidHardcoreRevivalData.java delete mode 100644 common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClientManager.java diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevival.java b/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevival.java index 0352f4d..b454296 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevival.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevival.java @@ -1,27 +1,18 @@ package net.blay09.mods.hardcorerevival; import net.blay09.mods.balm.api.Balm; -import net.blay09.mods.balm.api.proxy.SidedProxy; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalDataImpl; -import net.blay09.mods.hardcorerevival.capability.InvalidHardcoreRevivalData; import net.blay09.mods.hardcorerevival.command.ReviveCommand; import net.blay09.mods.hardcorerevival.compat.Compat; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.blay09.mods.hardcorerevival.handler.*; import net.blay09.mods.hardcorerevival.network.ModNetworking; import net.blay09.mods.hardcorerevival.stats.ModStats; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.player.Player; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class HardcoreRevival { public static final String MOD_ID = "hardcorerevival"; - private static final SidedProxy manager = Balm.sidedProxy("net.blay09.mods.hardcorerevival.HardcoreRevivalManager", "net.blay09.mods.hardcorerevival.client.HardcoreRevivalClientManager"); - private static final HardcoreRevivalData clientRevivalData = new HardcoreRevivalDataImpl(); - public static final Logger logger = LogManager.getLogger(); public static void initialize() { @@ -42,16 +33,4 @@ public static void initialize() { Balm.initializeIfLoaded(Compat.INVENTORY_TOTEM, "new.blay09.mods.hardcorerevival.compat.InventoryTotemAddon"); } - public static HardcoreRevivalManager getManager() { - return manager.get(); - } - - public static HardcoreRevivalData getRevivalData(Entity entity) { - return entity instanceof Player player ? getManager().getRevivalData(player) : InvalidHardcoreRevivalData.INSTANCE; - } - - public static HardcoreRevivalData getClientRevivalData() { - return clientRevivalData; - } - } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevivalManager.java b/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevivalManager.java index 881ba4a..ec37486 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevivalManager.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/HardcoreRevivalManager.java @@ -4,8 +4,6 @@ import net.blay09.mods.hardcorerevival.api.PlayerKnockedOutEvent; import net.blay09.mods.hardcorerevival.api.PlayerRescuedEvent; import net.blay09.mods.hardcorerevival.api.PlayerRevivedEvent; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; -import net.blay09.mods.hardcorerevival.capability.InvalidHardcoreRevivalData; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfigData; import net.blay09.mods.hardcorerevival.handler.KnockoutSyncHandler; @@ -32,14 +30,8 @@ public class HardcoreRevivalManager { public static final ResourceKey NOT_RESCUED_IN_TIME = ResourceKey.create(Registries.DAMAGE_TYPE, ResourceLocation.fromNamespaceAndPath(HardcoreRevival.MOD_ID, "not_rescued_in_time")); - public HardcoreRevivalData getRevivalData(Player player) { - HardcoreRevivalData provider = Balm.getProviders().getProvider(player, HardcoreRevivalData.class); - return provider != null ? provider : InvalidHardcoreRevivalData.INSTANCE; - } - - public void knockout(Player player, DamageSource source) { - HardcoreRevivalData revivalData = getRevivalData(player); - if (revivalData.isKnockedOut()) { + public static void knockout(Player player, DamageSource source) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { return; } @@ -47,25 +39,25 @@ public void knockout(Player player, DamageSource source) { player.stopRiding(); player.removeEffect(MobEffects.REGENERATION); - revivalData.setKnockedOut(true); - revivalData.setKnockoutTicksPassed(0); - revivalData.setLastKnockoutAt(System.currentTimeMillis()); + PlayerHardcoreRevivalManager.setKnockedOut(player, true); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(player, 0); + PlayerHardcoreRevivalManager.setLastKnockoutAt(player, System.currentTimeMillis()); player.awardStat(ModStats.knockouts); // Punish consecutive knockouts - final var lastRescuedAt = revivalData.getLastRescuedAt(); + final var lastRescuedAt = PlayerHardcoreRevivalManager.getLastRescuedAt(player); final var consecutiveThresholdSeconds = HardcoreRevivalConfig.getActive().consecutiveKnockoutThresholdSeconds; final var secondsSinceLastRescue = (System.currentTimeMillis() - lastRescuedAt) / 1000; final var isConsecutiveKnockout = consecutiveThresholdSeconds > 0 && lastRescuedAt > 0 && secondsSinceLastRescue <= consecutiveThresholdSeconds; if (isConsecutiveKnockout) { if (HardcoreRevivalConfig.getActive().resumeTimerOnConsecutiveKnockout) { - revivalData.setKnockoutTicksPassed(revivalData.getLastKnockoutTicksPassed()); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(player, PlayerHardcoreRevivalManager.getLastKnockoutTicksPassed(player)); } final var multiplyTimerOnConsecutiveKnockout = HardcoreRevivalConfig.getActive().multiplyTimerOnConsecutiveKnockout; final var maxTicksUntilDeath = HardcoreRevivalConfig.getActive().secondsUntilDeath * 20; - final var ticksLeft = maxTicksUntilDeath - revivalData.getKnockoutTicksPassed(); + final var ticksLeft = maxTicksUntilDeath - PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player); final var newTicksLeft = (int) (ticksLeft * multiplyTimerOnConsecutiveKnockout); - revivalData.setKnockoutTicksPassed(maxTicksUntilDeath - newTicksLeft); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(player, maxTicksUntilDeath - newTicksLeft); } // Fire event for compatibility addons @@ -91,14 +83,13 @@ public void knockout(Player player, DamageSource source) { updateKnockoutEffects(player); } - public void wakeup(Player player) { + public static void wakeup(Player player) { wakeup(player, true); } - public void wakeup(Player player, boolean applyEffects) { - final var revivalData = getRevivalData(player); - revivalData.setLastRescuedAt(System.currentTimeMillis()); - revivalData.setLastKnockoutTicksPassed(revivalData.getKnockoutTicksPassed()); + public static void wakeup(Player player, boolean applyEffects) { + PlayerHardcoreRevivalManager.setLastRescuedAt(player, System.currentTimeMillis()); + PlayerHardcoreRevivalManager.setLastKnockoutTicksPassed(player, PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player)); reset(player); if (applyEffects) { @@ -128,7 +119,7 @@ public void wakeup(Player player, boolean applyEffects) { Balm.getEvents().fireEvent(new PlayerRevivedEvent(player)); } - private int tryParseInt(@Nullable String text, int defaultVal) { + private static int tryParseInt(@Nullable String text, int defaultVal) { if (text != null) { try { return Integer.parseInt(text); @@ -139,9 +130,8 @@ private int tryParseInt(@Nullable String text, int defaultVal) { return defaultVal; } - public void finishRescue(Player player) { - HardcoreRevivalData revivalData = getRevivalData(player); - Player rescueTarget = revivalData.getRescueTarget(); + public static void finishRescue(Player player) { + Player rescueTarget = PlayerHardcoreRevivalManager.getRescueTarget(player); if (rescueTarget != null) { MinecraftServer server = rescueTarget.getServer(); if (server != null) { @@ -151,7 +141,7 @@ public void finishRescue(Player player) { Balm.getNetworking().sendTo(rescueTarget, new RevivalSuccessMessage(rescueTarget.getId())); Balm.getNetworking().sendToTracking(rescueTarget, new RevivalSuccessMessage(rescueTarget.getId())); - revivalData.setRescueTarget(null); + PlayerHardcoreRevivalManager.setRescueTarget(player, null); Balm.getEvents().fireEvent(new PlayerRescuedEvent(rescueTarget, player)); } @@ -160,20 +150,19 @@ public void finishRescue(Player player) { Balm.getHooks().setForcedPose(player, null); } - public void abortRescue(Player player) { - HardcoreRevivalData revivalData = getRevivalData(player); - Player rescueTarget = revivalData.getRescueTarget(); + public static void abortRescue(Player player) { + Player rescueTarget = PlayerHardcoreRevivalManager.getRescueTarget(player); if (rescueTarget != null) { - revivalData.setRescueTime(0); - revivalData.setRescueTarget(null); + PlayerHardcoreRevivalManager.setRescueTime(player, 0); + PlayerHardcoreRevivalManager.setRescueTarget(player, null); Balm.getNetworking().sendTo(player, new RevivalProgressMessage(-1, -1)); - KnockoutSyncHandler.sendHardcoreRevivalData(rescueTarget, rescueTarget, getRevivalData(rescueTarget)); + KnockoutSyncHandler.sendHardcoreRevivalData(rescueTarget, rescueTarget); Balm.getHooks().setForcedPose(player, null); } } - public void notRescuedInTime(Player player) { + public static void notRescuedInTime(Player player) { // Disable respawn invulnerability to prevent players from surviving knockout after login with offline timer enabled if (player instanceof ServerPlayerAccessor accessor) { accessor.setSpawnInvulnerableTime(0); @@ -181,48 +170,43 @@ public void notRescuedInTime(Player player) { final var damageTypes = player.level().registryAccess().registryOrThrow(Registries.DAMAGE_TYPE); final var damageSource = new DamageSource(damageTypes.getHolderOrThrow(NOT_RESCUED_IN_TIME)); - final var revivalData = getRevivalData(player); - revivalData.setLastKnockoutTicksPassed(0); + PlayerHardcoreRevivalManager.setLastKnockoutTicksPassed(player, 0); reset(player); player.hurt(damageSource, Float.MAX_VALUE); } - public void reset(Player player) { - HardcoreRevivalData revivalData = getRevivalData(player); - revivalData.setKnockedOut(false); - revivalData.setKnockoutTicksPassed(0); + public static void reset(Player player) { + PlayerHardcoreRevivalManager.setKnockedOut(player, false); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(player, 0); updateKnockoutEffects(player); } - public void updateKnockoutEffects(Player player) { - HardcoreRevivalData revivalData = getRevivalData(player); + public static void updateKnockoutEffects(Player player) { if (HardcoreRevivalConfig.getActive().glowOnKnockout) { - player.setGlowingTag(revivalData.isKnockedOut()); + player.setGlowingTag(PlayerHardcoreRevivalManager.isKnockedOut(player)); } - Balm.getHooks().setForcedPose(player, revivalData.isKnockedOut() ? Pose.FALL_FLYING : null); + Balm.getHooks().setForcedPose(player, PlayerHardcoreRevivalManager.isKnockedOut(player) ? Pose.FALL_FLYING : null); - KnockoutSyncHandler.sendHardcoreRevivalDataToWatching(player, revivalData); + KnockoutSyncHandler.sendHardcoreRevivalDataToWatching(player); } - public void startRescue(Player player, Player target) { - HardcoreRevivalData revivalData = getRevivalData(player); - revivalData.setRescueTarget(target); - revivalData.setRescueTime(0); + public static void startRescue(Player player, Player target) { + PlayerHardcoreRevivalManager.setRescueTarget(player, target); + PlayerHardcoreRevivalManager.setRescueTime(player, 0); Balm.getNetworking().sendTo(player, new RevivalProgressMessage(target.getId(), 0.1f)); - KnockoutSyncHandler.sendHardcoreRevivalData(target, target, getRevivalData(target), true); + KnockoutSyncHandler.sendHardcoreRevivalData(target, target, true); Balm.getHooks().setForcedPose(player, Pose.CROUCHING); } - public boolean isRescuing(Player player) { - HardcoreRevivalData revivalData = getRevivalData(player); - Player rescueTarget = revivalData.getRescueTarget(); + public static boolean isRescuing(Player player) { + Player rescueTarget = PlayerHardcoreRevivalManager.getRescueTarget(player); return rescueTarget != null; } - boolean isKnockedOut(Player player) { - return getRevivalData(player).isKnockedOut(); + public static boolean isKnockedOut(Player player) { + return PlayerHardcoreRevivalManager.isKnockedOut(player); } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalDataImpl.java b/common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryPlayerRevivalData.java similarity index 50% rename from common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalDataImpl.java rename to common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryPlayerRevivalData.java index 0dc90b5..f1cda00 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalDataImpl.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryPlayerRevivalData.java @@ -1,16 +1,8 @@ -package net.blay09.mods.hardcorerevival.capability; +package net.blay09.mods.hardcorerevival; -import net.minecraft.nbt.CompoundTag; import net.minecraft.world.entity.player.Player; -public class HardcoreRevivalDataImpl implements HardcoreRevivalData { - private static final String KNOCKED_OUT = "KnockedOut"; - private static final String KNOCKOUT_TICKS_PASSED = "KnockoutTicksPassed"; - private static final String LAST_KNOCKOUT_TICKS_PASSED = "LastKnockoutTicksPassed"; - private static final String LAST_RESCUED_AT = "LastRescuedAt"; - private static final String LAST_KNOCKOUT_AT = "LastKnockoutAt"; - private static final String LAST_LOGOUT_AT = "LastLogoutAt"; - +class InMemoryPlayerRevivalData { private boolean knockedOut; private int knockoutTicksPassed; private int lastKnockoutTicksPassed; @@ -20,105 +12,67 @@ public class HardcoreRevivalDataImpl implements HardcoreRevivalData { private int rescueTime; private Player rescueTarget; - @Override - public void setKnockedOut(boolean knockedOut) { - this.knockedOut = knockedOut; - } - - @Override public boolean isKnockedOut() { return knockedOut; } - @Override - public void setKnockoutTicksPassed(int knockoutTicksPassed) { - this.knockoutTicksPassed = knockoutTicksPassed; + public void setKnockedOut(boolean knockedOut) { + this.knockedOut = knockedOut; } - @Override public int getKnockoutTicksPassed() { return knockoutTicksPassed; } - @Override - public void setLastKnockoutTicksPassed(int lastKnockoutTicksPassed) { - this.lastKnockoutTicksPassed = lastKnockoutTicksPassed; + public void setKnockoutTicksPassed(int knockoutTicksPassed) { + this.knockoutTicksPassed = knockoutTicksPassed; } - @Override public int getLastKnockoutTicksPassed() { return lastKnockoutTicksPassed; } - @Override - public void setLastKnockoutAt(long lastKnockoutAt) { - this.lastKnockoutAt = lastKnockoutAt; - } - - @Override - public void setLastRescuedAt(long lastRescuedAt) { - this.lastRescuedAt = lastRescuedAt; + public void setLastKnockoutTicksPassed(int lastKnockoutTicksPassed) { + this.lastKnockoutTicksPassed = lastKnockoutTicksPassed; } - @Override public long getLastRescuedAt() { return lastRescuedAt; } - @Override + public void setLastRescuedAt(long lastRescuedAt) { + this.lastRescuedAt = lastRescuedAt; + } + public long getLastKnockoutAt() { return lastKnockoutAt; } - @Override - public void setLastLogoutAt(long lastLogoutAt) { - this.lastLogoutAt = lastLogoutAt; + public void setLastKnockoutAt(long lastKnockoutAt) { + this.lastKnockoutAt = lastKnockoutAt; } - @Override public long getLastLogoutAt() { return lastLogoutAt; } - @Override - public void setRescueTime(int rescueTime) { - this.rescueTime = rescueTime; + public void setLastLogoutAt(long lastLogoutAt) { + this.lastLogoutAt = lastLogoutAt; } - @Override public int getRescueTime() { return rescueTime; } - @Override - public void setRescueTarget(Player rescueTarget) { - this.rescueTarget = rescueTarget; + public void setRescueTime(int rescueTime) { + this.rescueTime = rescueTime; } - @Override public Player getRescueTarget() { return rescueTarget; } - @Override - public CompoundTag serialize() { - CompoundTag tagCompound = new CompoundTag(); - tagCompound.putBoolean(KNOCKED_OUT, isKnockedOut()); - tagCompound.putInt(KNOCKOUT_TICKS_PASSED, getKnockoutTicksPassed()); - tagCompound.putInt(LAST_KNOCKOUT_TICKS_PASSED, getLastKnockoutTicksPassed()); - tagCompound.putLong(LAST_LOGOUT_AT, getLastLogoutAt()); - tagCompound.putLong(LAST_KNOCKOUT_AT, getLastKnockoutAt()); - tagCompound.putLong(LAST_RESCUED_AT, getLastRescuedAt()); - return tagCompound; - } - - @Override - public void deserialize(CompoundTag tag) { - setKnockedOut(tag.getBoolean(KNOCKED_OUT)); - setKnockoutTicksPassed(tag.getInt(KNOCKOUT_TICKS_PASSED)); - setLastKnockoutTicksPassed(tag.getInt(LAST_KNOCKOUT_TICKS_PASSED)); - setLastLogoutAt(tag.getLong(LAST_LOGOUT_AT)); - setLastKnockoutAt(tag.getLong(LAST_KNOCKOUT_AT)); - setLastRescuedAt(tag.getLong(LAST_RESCUED_AT)); + public void setRescueTarget(Player rescueTarget) { + this.rescueTarget = rescueTarget; } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryRevivalDataProvider.java b/common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryRevivalDataProvider.java new file mode 100644 index 0000000..0ec62cc --- /dev/null +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/InMemoryRevivalDataProvider.java @@ -0,0 +1,95 @@ +package net.blay09.mods.hardcorerevival; + +import net.minecraft.world.entity.player.Player; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class InMemoryRevivalDataProvider implements RevivalDataProvider { + + private final Map playerData = new HashMap<>(); + + private InMemoryPlayerRevivalData getPlayerData(Player player) { + return playerData.computeIfAbsent(player.getUUID(), uuid -> new InMemoryPlayerRevivalData()); + } + + @Override + public void setKnockedOut(Player player, boolean knockedOut) { + getPlayerData(player).setKnockedOut(knockedOut); + } + + @Override + public boolean isKnockedOut(Player player) { + return getPlayerData(player).isKnockedOut(); + } + + @Override + public void setKnockoutTicksPassed(Player player, int knockoutTicksPassed) { + getPlayerData(player).setKnockoutTicksPassed(knockoutTicksPassed); + } + + @Override + public int getKnockoutTicksPassed(Player player) { + return getPlayerData(player).getKnockoutTicksPassed(); + } + + @Override + public void setLastKnockoutTicksPassed(Player player, int lastKnockoutTicksPassed) { + getPlayerData(player).setLastKnockoutTicksPassed(lastKnockoutTicksPassed); + } + + @Override + public int getLastKnockoutTicksPassed(Player player) { + return getPlayerData(player).getLastKnockoutTicksPassed(); + } + + @Override + public void setLastRescuedAt(Player player, long lastRescuedAt) { + getPlayerData(player).setLastRescuedAt(lastRescuedAt); + } + + @Override + public long getLastRescuedAt(Player player) { + return getPlayerData(player).getLastRescuedAt(); + } + + @Override + public void setLastKnockoutAt(Player player, long lastKnockoutAt) { + getPlayerData(player).setLastKnockoutAt(lastKnockoutAt); + } + + @Override + public long getLastKnockoutAt(Player player) { + return getPlayerData(player).getLastKnockoutAt(); + } + + @Override + public void setLastLogoutAt(Player player, long lastLogoutAt) { + getPlayerData(player).setLastLogoutAt(lastLogoutAt); + } + + @Override + public long getLastLogoutAt(Player player) { + return getPlayerData(player).getLastLogoutAt(); + } + + @Override + public void setRescueTime(Player player, int rescueTime) { + getPlayerData(player).setRescueTime(rescueTime); + } + + @Override + public int getRescueTime(Player player) { + return getPlayerData(player).getRescueTime(); + } + + @Override + public void setRescueTarget(Player player, Player rescueTarget) { + getPlayerData(player).setRescueTarget(rescueTarget); + } + + @Override + public Player getRescueTarget(Player player) { + return getPlayerData(player).getRescueTarget(); + }} diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/InternalMethodsImpl.java b/common/src/main/java/net/blay09/mods/hardcorerevival/InternalMethodsImpl.java index 31fc41b..b8bb40e 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/InternalMethodsImpl.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/InternalMethodsImpl.java @@ -8,22 +8,22 @@ public class InternalMethodsImpl implements InternalMethods { @Override public void knockout(Player player, DamageSource damageSource) { - HardcoreRevival.getManager().knockout(player, damageSource); + HardcoreRevivalManager.knockout(player, damageSource); } @Override public void wakeup(Player player, boolean applyEffects) { - HardcoreRevival.getManager().wakeup(player, applyEffects); + HardcoreRevivalManager.wakeup(player, applyEffects); } @Override public boolean isKnockedOut(Player player) { - return HardcoreRevival.getManager().isKnockedOut(player); + return PlayerHardcoreRevivalManager.isKnockedOut(player); } @Override public int getKnockoutTicksPassed(Player player) { - return HardcoreRevival.getManager().getRevivalData(player).getKnockoutTicksPassed(); + return PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player); } @Override diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/MixinHooks.java b/common/src/main/java/net/blay09/mods/hardcorerevival/MixinHooks.java index 69f585e..0a504f0 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/MixinHooks.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/MixinHooks.java @@ -8,15 +8,15 @@ public class MixinHooks { public static boolean shouldCancelMovement(Entity entity) { - return entity instanceof Player && HardcoreRevival.getRevivalData(entity).isKnockedOut(); + return entity instanceof Player player && PlayerHardcoreRevivalManager.isKnockedOut(player); } - public static boolean shouldCancelHealing(Player entity) { - return HardcoreRevival.getRevivalData(entity).isKnockedOut(); + public static boolean shouldCancelHealing(Player player) { + return PlayerHardcoreRevivalManager.isKnockedOut(player); } public static boolean shouldCancelFire(Entity entity) { - return HardcoreRevival.getRevivalData(entity).isKnockedOut(); + return entity instanceof Player player && PlayerHardcoreRevivalManager.isKnockedOut(player); } public static void handleProcessPlayerRotation(ServerPlayer player, ServerboundMovePlayerPacket packet) { diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/PersistentRevivalDataProvider.java b/common/src/main/java/net/blay09/mods/hardcorerevival/PersistentRevivalDataProvider.java new file mode 100644 index 0000000..3ea63b7 --- /dev/null +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/PersistentRevivalDataProvider.java @@ -0,0 +1,109 @@ +package net.blay09.mods.hardcorerevival; + +import net.blay09.mods.balm.api.Balm; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.entity.player.Player; + +public class PersistentRevivalDataProvider implements RevivalDataProvider { + + private static final String TAG_NAME = "HardcoreRevivalData"; + private static final String KNOCKED_OUT = "KnockedOut"; + private static final String KNOCKOUT_TICKS_PASSED = "KnockoutTicksPassed"; + private static final String LAST_KNOCKOUT_TICKS_PASSED = "LastKnockoutTicksPassed"; + private static final String LAST_RESCUED_AT = "LastRescuedAt"; + private static final String LAST_KNOCKOUT_AT = "LastKnockoutAt"; + private static final String LAST_LOGOUT_AT = "LastLogoutAt"; + private static final String RESCUE_TARGET = "RescueTarget"; + + private static CompoundTag getRevivalData(Player player) { + CompoundTag persistedData = Balm.getHooks().getPersistentData(player); + CompoundTag compound = persistedData.getCompound(TAG_NAME); + persistedData.put(TAG_NAME, compound); + return compound; + } + + @Override + public void setKnockedOut(Player player, boolean knockedOut) { + getRevivalData(player).putBoolean(KNOCKED_OUT, knockedOut); + } + + @Override + public boolean isKnockedOut(Player player) { + return getRevivalData(player).getBoolean(KNOCKED_OUT); + } + + @Override + public void setKnockoutTicksPassed(Player player, int knockoutTicksPassed) { + getRevivalData(player).putInt(KNOCKOUT_TICKS_PASSED, knockoutTicksPassed); + } + + @Override + public int getKnockoutTicksPassed(Player player) { + return getRevivalData(player).getInt(KNOCKOUT_TICKS_PASSED); + } + + @Override + public void setLastKnockoutTicksPassed(Player player, int lastKnockoutTicksPassed) { + getRevivalData(player).putInt(LAST_KNOCKOUT_TICKS_PASSED, lastKnockoutTicksPassed); + } + + @Override + public int getLastKnockoutTicksPassed(Player player) { + return getRevivalData(player).getInt(LAST_KNOCKOUT_TICKS_PASSED); + } + + @Override + public void setLastRescuedAt(Player player, long lastRescuedAt) { + getRevivalData(player).putLong(LAST_RESCUED_AT, lastRescuedAt); + } + + @Override + public long getLastRescuedAt(Player player) { + return getRevivalData(player).getLong(LAST_RESCUED_AT); + } + + @Override + public void setLastKnockoutAt(Player player, long lastKnockoutAt) { + getRevivalData(player).putLong(LAST_KNOCKOUT_AT, lastKnockoutAt); + } + + @Override + public long getLastKnockoutAt(Player player) { + return getRevivalData(player).getLong(LAST_KNOCKOUT_AT); + } + + @Override + public void setLastLogoutAt(Player player, long lastLogoutAt) { + getRevivalData(player).putLong(LAST_LOGOUT_AT, lastLogoutAt); + } + + @Override + public long getLastLogoutAt(Player player) { + return getRevivalData(player).getLong(LAST_LOGOUT_AT); + } + + @Override + public void setRescueTime(Player player, int rescueTime) { + getRevivalData(player).putInt(LAST_RESCUED_AT, rescueTime); + } + + @Override + public int getRescueTime(Player player) { + return getRevivalData(player).getInt(LAST_RESCUED_AT); + } + + @Override + public void setRescueTarget(Player player, Player rescueTarget) { + getRevivalData(player).putUUID(RESCUE_TARGET, rescueTarget.getGameProfile().getId()); + } + + @Override + public Player getRescueTarget(Player player) { + final var server = player.level().getServer(); + if (server != null) { + final var tag = getRevivalData(player); + return tag.contains(RESCUE_TARGET) ? server.getPlayerList().getPlayer(tag.getUUID(RESCUE_TARGET)) : null; + } + return null; + } +} diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/PlayerHardcoreRevivalManager.java b/common/src/main/java/net/blay09/mods/hardcorerevival/PlayerHardcoreRevivalManager.java new file mode 100644 index 0000000..ea7c574 --- /dev/null +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/PlayerHardcoreRevivalManager.java @@ -0,0 +1,79 @@ +package net.blay09.mods.hardcorerevival; + +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; +import org.jetbrains.annotations.Nullable; + +public class PlayerHardcoreRevivalManager { + + private static final RevivalDataProvider persistentDataProvider = new PersistentRevivalDataProvider(); + private static final RevivalDataProvider inMemoryDataProvider = new InMemoryRevivalDataProvider(); + + public static RevivalDataProvider getRevivalDataProvider(@Nullable Level world) { + return world == null || world.isClientSide ? inMemoryDataProvider : persistentDataProvider; + } + + public static void setKnockedOut(Player player, boolean knockedOut) { + getRevivalDataProvider(player.level()).setKnockedOut(player, knockedOut); + } + + public static boolean isKnockedOut(Player player) { + return getRevivalDataProvider(player.level()).isKnockedOut(player); + } + + public static void setKnockoutTicksPassed(Player player, int knockoutTicksPassed) { + getRevivalDataProvider(player.level()).setKnockoutTicksPassed(player, knockoutTicksPassed); + } + + public static int getKnockoutTicksPassed(Player player) { + return getRevivalDataProvider(player.level()).getKnockoutTicksPassed(player); + } + + public static void setLastKnockoutTicksPassed(Player player, int lastKnockoutTicksPassed) { + getRevivalDataProvider(player.level()).setLastKnockoutTicksPassed(player, lastKnockoutTicksPassed); + } + + public static int getLastKnockoutTicksPassed(Player player) { + return getRevivalDataProvider(player.level()).getLastKnockoutTicksPassed(player); + } + + public static void setLastRescuedAt(Player player, long lastRescuedAt) { + getRevivalDataProvider(player.level()).setLastRescuedAt(player, lastRescuedAt); + } + + public static long getLastRescuedAt(Player player) { + return getRevivalDataProvider(player.level()).getLastRescuedAt(player); + } + + public static void setLastKnockoutAt(Player player, long lastKnockoutAt) { + getRevivalDataProvider(player.level()).setLastKnockoutAt(player, lastKnockoutAt); + } + + public static long getLastKnockoutAt(Player player) { + return getRevivalDataProvider(player.level()).getLastKnockoutAt(player); + } + + public static void setLastLogoutAt(Player player, long lastLogoutAt) { + getRevivalDataProvider(player.level()).setLastLogoutAt(player, lastLogoutAt); + } + + public static long getLastLogoutAt(Player player) { + return getRevivalDataProvider(player.level()).getLastLogoutAt(player); + } + + public static void setRescueTime(Player player, int rescueTime) { + getRevivalDataProvider(player.level()).setRescueTime(player, rescueTime); + } + + public static int getRescueTime(Player player) { + return getRevivalDataProvider(player.level()).getRescueTime(player); + } + + public static void setRescueTarget(Player player, Player rescueTarget) { + getRevivalDataProvider(player.level()).setRescueTarget(player, rescueTarget); + } + + public static Player getRescueTarget(Player player) { + return getRevivalDataProvider(player.level()).getRescueTarget(player); + } +} \ No newline at end of file diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/RevivalDataProvider.java b/common/src/main/java/net/blay09/mods/hardcorerevival/RevivalDataProvider.java new file mode 100644 index 0000000..b6bad91 --- /dev/null +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/RevivalDataProvider.java @@ -0,0 +1,37 @@ +package net.blay09.mods.hardcorerevival; + +import net.minecraft.world.entity.player.Player; + +public interface RevivalDataProvider { + void setKnockedOut(Player player, boolean knockedOut); + + boolean isKnockedOut(Player player); + + void setKnockoutTicksPassed(Player player, int knockoutTicksPassed); + + int getKnockoutTicksPassed(Player player); + + void setLastKnockoutTicksPassed(Player player, int lastKnockoutTicksPassed); + + int getLastKnockoutTicksPassed(Player player); + + void setLastRescuedAt(Player player, long lastRescuedAt); + + long getLastRescuedAt(Player player); + + void setLastKnockoutAt(Player player, long lastKnockoutAt); + + long getLastKnockoutAt(Player player); + + void setLastLogoutAt(Player player, long lastLogoutAt); + + long getLastLogoutAt(Player player); + + void setRescueTime(Player player, int rescueTime); + + int getRescueTime(Player player); + + void setRescueTarget(Player player, Player rescueTarget); + + Player getRescueTarget(Player player); +} diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalData.java b/common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalData.java deleted file mode 100644 index c9b6034..0000000 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/capability/HardcoreRevivalData.java +++ /dev/null @@ -1,42 +0,0 @@ -package net.blay09.mods.hardcorerevival.capability; - -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.entity.player.Player; - -public interface HardcoreRevivalData { - void setKnockedOut(boolean knockedOut); - - boolean isKnockedOut(); - - void setKnockoutTicksPassed(int knockoutTicksPassed); - - int getKnockoutTicksPassed(); - - void setLastKnockoutTicksPassed(int lastKnockoutTicksPassed); - - int getLastKnockoutTicksPassed(); - - void setLastRescuedAt(long lastRescuedAt); - - long getLastRescuedAt(); - - void setLastKnockoutAt(long lastKnockoutAt); - - long getLastKnockoutAt(); - - void setLastLogoutAt(long lastLogoutAt); - - long getLastLogoutAt(); - - void setRescueTime(int rescueTime); - - int getRescueTime(); - - void setRescueTarget(Player rescueTarget); - - Player getRescueTarget(); - - CompoundTag serialize(); - - void deserialize(CompoundTag tag); -} diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/capability/InvalidHardcoreRevivalData.java b/common/src/main/java/net/blay09/mods/hardcorerevival/capability/InvalidHardcoreRevivalData.java deleted file mode 100644 index 5a1dd72..0000000 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/capability/InvalidHardcoreRevivalData.java +++ /dev/null @@ -1,92 +0,0 @@ -package net.blay09.mods.hardcorerevival.capability; - -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.entity.player.Player; -import org.jetbrains.annotations.Nullable; - -public class InvalidHardcoreRevivalData implements HardcoreRevivalData { - - public static final HardcoreRevivalData INSTANCE = new InvalidHardcoreRevivalData(); - - @Override - public void setKnockedOut(boolean knockedOut) { - } - - @Override - public boolean isKnockedOut() { - return false; - } - - @Override - public void setKnockoutTicksPassed(int knockoutTicksPassed) { - } - - @Override - public int getKnockoutTicksPassed() { - return 0; - } - - @Override - public void setLastKnockoutTicksPassed(int lastKnockoutTicksPassed) { - } - - @Override - public int getLastKnockoutTicksPassed() { - return 0; - } - - @Override - public void setLastRescuedAt(long lastRescuedAt) { - } - - @Override - public long getLastRescuedAt() { - return 0; - } - - @Override - public void setLastKnockoutAt(long lastKnockoutAt) { - } - - @Override - public long getLastKnockoutAt() { - return 0; - } - - @Override - public void setLastLogoutAt(long lastLogoutAt) { - } - - @Override - public long getLastLogoutAt() { - return 0; - } - - @Override - public void setRescueTime(int rescueTime) { - } - - @Override - public int getRescueTime() { - return 0; - } - - @Override - public void setRescueTarget(@Nullable Player rescueTarget) { - } - - @Nullable - @Override - public Player getRescueTarget() { - return null; - } - - @Override - public CompoundTag serialize() { - return new CompoundTag(); - } - - @Override - public void deserialize(CompoundTag tag) { - } -} diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/client/GuiHelper.java b/common/src/main/java/net/blay09/mods/hardcorerevival/client/GuiHelper.java index ea21fe2..14481b3 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/client/GuiHelper.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/client/GuiHelper.java @@ -1,6 +1,7 @@ package net.blay09.mods.hardcorerevival.client; import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; @@ -28,7 +29,7 @@ public static void renderDeathTimer(GuiGraphics guiGraphics, int width, int heig } else { int maxTicksUntilDeath = HardcoreRevivalConfig.getActive().secondsUntilDeath * 20; if (maxTicksUntilDeath > 0) { - int deathSecondsLeft = Math.max(0, (maxTicksUntilDeath - HardcoreRevival.getClientRevivalData().getKnockoutTicksPassed()) / 20); + int deathSecondsLeft = Math.max(0, (maxTicksUntilDeath - PlayerHardcoreRevivalManager.getKnockoutTicksPassed(Minecraft.getInstance().player)) / 20); guiGraphics.drawCenteredString(font, I18n.get("gui.hardcorerevival.rescue_time_left", deathSecondsLeft), width / 2, height / 2 + 10, 16777215); } else { guiGraphics.drawCenteredString(font, I18n.get("gui.hardcorerevival.wait_for_rescue"), width / 2, height / 2 + 10, 16777215); diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClient.java b/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClient.java index a6ddd78..4c27067 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClient.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClient.java @@ -8,8 +8,7 @@ import net.blay09.mods.balm.api.event.client.GuiDrawEvent; import net.blay09.mods.balm.api.event.client.KeyInputEvent; import net.blay09.mods.balm.api.event.client.OpenScreenEvent; -import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.blay09.mods.hardcorerevival.network.RescueMessage; import net.minecraft.client.Minecraft; @@ -43,7 +42,7 @@ public static void initialize() { private static boolean isKnockedOut() { LocalPlayer player = Minecraft.getInstance().player; - return HardcoreRevival.getClientRevivalData().isKnockedOut() && player != null && player.isAlive(); + return player != null && PlayerHardcoreRevivalManager.isKnockedOut(player) && player.isAlive(); } public static void onOpenScreen(OpenScreenEvent event) { @@ -61,7 +60,7 @@ public static void onFovUpdate(FovUpdateEvent event) { public static void onGuiDrawPre(GuiDrawEvent.Pre event) { // Flash the health bar red if the player is knocked out if (event.getElement() == GuiDrawEvent.Element.HEALTH && isKnockedOut()) { - int knockoutTicksPassed = HardcoreRevival.getClientRevivalData().getKnockoutTicksPassed(); + int knockoutTicksPassed = PlayerHardcoreRevivalManager.getKnockoutTicksPassed(Minecraft.getInstance().player); float redness = (float) Math.sin(knockoutTicksPassed / 2f); RenderSystem.setShaderColor(1f, 1f - redness, 1 - redness, 1f); } @@ -113,11 +112,10 @@ public static void onGuiDrawPost(GuiDrawEvent.Post event) { } } - if (!HardcoreRevival.getClientRevivalData() - .isKnockedOut() && mc.player != null && !mc.player.isSpectator() && mc.player.isAlive() && !isRescuing) { + if (!PlayerHardcoreRevivalManager.isKnockedOut(mc.player) && mc.player != null && !mc.player.isSpectator() && mc.player.isAlive() && !isRescuing) { Entity pointedEntity = Minecraft.getInstance().crosshairPickEntity; - if (pointedEntity != null && HardcoreRevival.getRevivalData(pointedEntity) - .isKnockedOut() && mc.player.distanceTo(pointedEntity) <= HardcoreRevivalConfig.getActive().rescueDistance) { + if (pointedEntity instanceof Player pointedPlayer && PlayerHardcoreRevivalManager.isKnockedOut(pointedPlayer) && mc.player.distanceTo( + pointedEntity) <= HardcoreRevivalConfig.getActive().rescueDistance) { Component rescueKeyText = mc.options.keyUse.getTranslatedKeyMessage(); var textComponent = Component.translatable("gui.hardcorerevival.hold_to_rescue", rescueKeyText); guiGraphics.drawString(mc.font, @@ -153,8 +151,7 @@ public static void onClientTick(Minecraft client) { wasKnockedOut = true; } - HardcoreRevivalData revivalData = HardcoreRevival.getRevivalData(client.player); - revivalData.setKnockoutTicksPassed(revivalData.getKnockoutTicksPassed() + 1); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(client.player, PlayerHardcoreRevivalManager.getKnockoutTicksPassed(client.player) + 1); } else { if (wasKnockedOut) { Balm.getHooks().setForcedPose(client.player, null); @@ -167,8 +164,8 @@ public static void onClientTick(Minecraft client) { } // If right mouse is held down, and player is not in spectator mode, send rescue packet - if (client.options.keyUse.isDown() && !client.player.isSpectator() && client.player.isAlive() && !HardcoreRevival.getClientRevivalData() - .isKnockedOut()) { + if (client.options.keyUse.isDown() && !client.player.isSpectator() && client.player.isAlive() && !PlayerHardcoreRevivalManager.isKnockedOut( + client.player)) { if (!isRescuing) { Balm.getNetworking().sendToServer(new RescueMessage(true)); isRescuing = true; diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClientManager.java b/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClientManager.java deleted file mode 100644 index 0a482fc..0000000 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/client/HardcoreRevivalClientManager.java +++ /dev/null @@ -1,20 +0,0 @@ -package net.blay09.mods.hardcorerevival.client; - -import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; -import net.minecraft.client.Minecraft; -import net.minecraft.world.entity.player.Player; - -public class HardcoreRevivalClientManager extends HardcoreRevivalManager { - - @Override - public HardcoreRevivalData getRevivalData(Player player) { - if (Minecraft.getInstance().player == player) { - return HardcoreRevival.getClientRevivalData(); - } - - return super.getRevivalData(player); - } - -} diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/command/ReviveCommand.java b/common/src/main/java/net/blay09/mods/hardcorerevival/command/ReviveCommand.java index 5b5049d..e15ea45 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/command/ReviveCommand.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/command/ReviveCommand.java @@ -3,7 +3,7 @@ import com.google.common.collect.ImmutableList; import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.arguments.BoolArgumentType; -import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.Commands; import net.minecraft.commands.arguments.EntityArgument; @@ -27,7 +27,7 @@ public static void register(CommandDispatcher dispatcher) { private static int reviveEntities(CommandSourceStack source, Collection targets, boolean skipEffects) { for (Entity entity : targets) { if (entity instanceof Player player) { - HardcoreRevival.getManager().wakeup(player, !skipEffects); + HardcoreRevivalManager.wakeup(player, !skipEffects); } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutHandler.java b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutHandler.java index 7c5c82d..ff5144e 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutHandler.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutHandler.java @@ -4,8 +4,8 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.event.*; import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.api.PlayerAboutToKnockOutEvent; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; import net.minecraft.core.registries.Registries; @@ -33,7 +33,7 @@ public static void onPlayerDamage(LivingDamageEvent event) { if (event.getEntity() instanceof ServerPlayer player) { DamageSource damageSource = event.getDamageSource(); - if (HardcoreRevival.getRevivalData(event.getEntity()).isKnockedOut()) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { Entity attacker = damageSource.getEntity(); if (attacker instanceof Mob mob) { mob.setTarget(null); @@ -51,7 +51,7 @@ public static void onPlayerDamage(LivingDamageEvent event) { if (!aboutToKnockOutEvent.isCanceled()) { event.setDamageAmount(Math.min(event.getDamageAmount(), Math.max(0f, player.getHealth() - 1f))); - HardcoreRevival.getManager().knockout(player, damageSource); + HardcoreRevivalManager.knockout(player, damageSource); } } } @@ -95,26 +95,25 @@ private static boolean isKnockoutEnabledFor(ServerPlayer player, DamageSource da public static void onPlayerTick(ServerPlayer player) { //if (event.phase == TickEvent.Phase.START && event.side == LogicalSide.SERVER) { - HardcoreRevivalData revivalData = HardcoreRevival.getRevivalData(player); - if (revivalData.isKnockedOut() && player.isAlive()) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player) && player.isAlive()) { // Make sure health stays locked at half a heart player.setHealth(1f); - revivalData.setKnockoutTicksPassed(revivalData.getKnockoutTicksPassed() + 1); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(player, PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player) + 1); if (player.tickCount % 20 == 0) { - Balm.getHooks().setForcedPose(player, revivalData.isKnockedOut() ? Pose.FALL_FLYING : null); + Balm.getHooks().setForcedPose(player, PlayerHardcoreRevivalManager.isKnockedOut(player) ? Pose.FALL_FLYING : null); } int maxTicksUntilDeath = HardcoreRevivalConfig.getActive().secondsUntilDeath * 20; - if (maxTicksUntilDeath > 0 && revivalData.getKnockoutTicksPassed() >= maxTicksUntilDeath) { - HardcoreRevival.getManager().notRescuedInTime(player); + if (maxTicksUntilDeath > 0 && PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player) >= maxTicksUntilDeath) { + HardcoreRevivalManager.notRescuedInTime(player); } } } public static void onPlayerRespawn(PlayerRespawnEvent event) { - HardcoreRevival.getManager().reset(event.getNewPlayer()); + HardcoreRevivalManager.reset(event.getNewPlayer()); } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutRestrictionHandler.java b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutRestrictionHandler.java index 8395a3a..3e006c1 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutRestrictionHandler.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutRestrictionHandler.java @@ -1,14 +1,11 @@ package net.blay09.mods.hardcorerevival.handler; -import com.mojang.brigadier.exceptions.CommandSyntaxException; import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.event.*; -import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.minecraft.ChatFormatting; import net.minecraft.network.chat.Component; -import net.minecraft.server.players.PlayerList; -import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BowItem; import net.minecraft.world.item.ItemStack; @@ -44,7 +41,7 @@ public static void onCommand(CommandEvent event) { return; } - if (HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { player.sendSystemMessage(Component.translatable("commands.disabled_when_knocked_out").withStyle(ChatFormatting.RED)); event.setCanceled(true); } @@ -52,7 +49,7 @@ public static void onCommand(CommandEvent event) { public static void onHeal(LivingHealEvent event) { if (event.getEntity() instanceof Player player) { - if (HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { event.setCanceled(false); } } @@ -60,7 +57,7 @@ public static void onHeal(LivingHealEvent event) { public static void onDigSpeed(DigSpeedEvent event) { Player player = event.getPlayer(); - if (player != null && HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (player != null && PlayerHardcoreRevivalManager.isKnockedOut(player)) { event.setSpeedOverride(0f); event.setCanceled(true); } @@ -68,7 +65,7 @@ public static void onDigSpeed(DigSpeedEvent event) { public static void onUseBlock(UseBlockEvent event) { Player player = event.getPlayer(); - if (HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { ItemStack itemStack = player.getItemInHand(event.getHand()); if (!HardcoreRevivalConfig.getActive().allowBows || !(itemStack.getItem() instanceof BowItem)) { event.setCanceled(true); @@ -77,8 +74,8 @@ public static void onUseBlock(UseBlockEvent event) { } public static void onUseItem(UseItemEvent event) { - LivingEntity player = event.getPlayer(); - if (HardcoreRevival.getRevivalData(player).isKnockedOut()) { + final var player = event.getPlayer(); + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { ItemStack itemStack = player.getItemInHand(event.getHand()); if (!HardcoreRevivalConfig.getActive().allowBows || !(itemStack.getItem() instanceof BowItem)) { event.setCanceled(true); @@ -88,7 +85,7 @@ public static void onUseItem(UseItemEvent event) { public static void onTossItem(TossItemEvent event) { Player player = event.getPlayer(); - if (HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { // We try to suppress the drop on the client too, but if that failed for some reason, just try to revert the action if (player.addItem(event.getItemStack())) { event.setCanceled(true); @@ -98,7 +95,7 @@ public static void onTossItem(TossItemEvent event) { public static void onAttack(PlayerAttackEvent event) { Player player = event.getPlayer(); - if (player != null && HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (player != null && PlayerHardcoreRevivalManager.isKnockedOut(player)) { if (HardcoreRevivalConfig.getActive().allowUnarmedMelee && player.getMainHandItem().isEmpty()) { return; } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutSyncHandler.java b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutSyncHandler.java index 3099d29..31db718 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutSyncHandler.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/KnockoutSyncHandler.java @@ -2,12 +2,10 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.event.ChunkTrackingEvent; -import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.network.HardcoreRevivalDataMessage; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; public class KnockoutSyncHandler { @@ -18,25 +16,30 @@ public static void initialize() { public static void onStartChunkTracking(ChunkTrackingEvent.Start event) { MinecraftServer server = event.getLevel().getServer(); for (ServerPlayer player : server.getPlayerList().getPlayers()) { - HardcoreRevivalData revivalData = HardcoreRevival.getRevivalData(player); - if (revivalData.isKnockedOut()) { - sendHardcoreRevivalData(event.getPlayer(), player, revivalData); + if (PlayerHardcoreRevivalManager.isKnockedOut(player)) { + sendHardcoreRevivalData(event.getPlayer(), player); } } } - public static void sendHardcoreRevivalDataToWatching(Player player, HardcoreRevivalData revivalData) { - HardcoreRevivalDataMessage message = new HardcoreRevivalDataMessage(player.getId(), revivalData.isKnockedOut(), revivalData.getKnockoutTicksPassed(), false); + public static void sendHardcoreRevivalDataToWatching(Player player) { + HardcoreRevivalDataMessage message = new HardcoreRevivalDataMessage(player.getId(), + PlayerHardcoreRevivalManager.isKnockedOut(player), + PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player), + false); Balm.getNetworking().sendToTracking(player, message); - sendHardcoreRevivalData(player, player, revivalData); + sendHardcoreRevivalData(player, player); } - public static void sendHardcoreRevivalData(Player player, Entity entity, HardcoreRevivalData revivalData) { - sendHardcoreRevivalData(player, entity, revivalData, false); + public static void sendHardcoreRevivalData(Player toPlayer, Player forPlayer) { + sendHardcoreRevivalData(toPlayer, forPlayer, false); } - public static void sendHardcoreRevivalData(Player player, Entity entity, HardcoreRevivalData revivalData, boolean beingRescued) { - HardcoreRevivalDataMessage message = new HardcoreRevivalDataMessage(entity.getId(), revivalData.isKnockedOut(), revivalData.getKnockoutTicksPassed(), beingRescued); - Balm.getNetworking().sendTo(player, message); + public static void sendHardcoreRevivalData(Player toPlayer, Player forPlayer, boolean beingRescued) { + HardcoreRevivalDataMessage message = new HardcoreRevivalDataMessage(forPlayer.getId(), + PlayerHardcoreRevivalManager.isKnockedOut(forPlayer), + PlayerHardcoreRevivalManager.getKnockoutTicksPassed(forPlayer), + beingRescued); + Balm.getNetworking().sendTo(toPlayer, message); } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/LoginLogoutHandler.java b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/LoginLogoutHandler.java index 667fd35..bfcc303 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/LoginLogoutHandler.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/LoginLogoutHandler.java @@ -3,11 +3,9 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.event.PlayerLoginEvent; import net.blay09.mods.balm.api.event.PlayerLogoutEvent; -import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; +import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.entity.player.Player; @@ -21,31 +19,22 @@ public static void initialize() { public static void onPlayerLogin(PlayerLoginEvent event) { ServerPlayer player = event.getPlayer(); - CompoundTag data = Balm.getHooks().getPersistentData(player); - HardcoreRevivalData revivalData = HardcoreRevival.getRevivalData(player); - revivalData.deserialize(data.getCompound("HardcoreRevival")); - if (HardcoreRevivalConfig.getActive().continueTimerWhileOffline && revivalData.isKnockedOut()) { + if (HardcoreRevivalConfig.getActive().continueTimerWhileOffline && PlayerHardcoreRevivalManager.isKnockedOut(player)) { final var now = System.currentTimeMillis(); - final var then = revivalData.getLastLogoutAt(); + final var then = PlayerHardcoreRevivalManager.getLastLogoutAt(player); final var millisPassed = (int) Math.max(0, now - then); final var secondsPassed = millisPassed / 1000; final var ticksPassed = secondsPassed * 20; - revivalData.setKnockoutTicksPassed(revivalData.getKnockoutTicksPassed() + ticksPassed); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(player, PlayerHardcoreRevivalManager.getKnockoutTicksPassed(player) + ticksPassed); } - HardcoreRevival.getManager().updateKnockoutEffects(player); + HardcoreRevivalManager.updateKnockoutEffects(player); } public static void onPlayerLogout(PlayerLogoutEvent event) { Player player = event.getPlayer(); - CompoundTag data = Balm.getHooks().getPersistentData(player); - HardcoreRevivalData revivalData = HardcoreRevival.getRevivalData(player); - revivalData.setLastLogoutAt(player.level().getGameTime()); - Tag tag = revivalData.serialize(); - if (tag != null) { - data.put("HardcoreRevival", tag); - } + PlayerHardcoreRevivalManager.setLastLogoutAt(player, player.level().getGameTime()); } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/RescueHandler.java b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/RescueHandler.java index 13bd663..f7c872b 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/handler/RescueHandler.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/handler/RescueHandler.java @@ -2,8 +2,8 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.api.event.*; -import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; +import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.blay09.mods.hardcorerevival.network.RevivalProgressMessage; import net.minecraft.server.level.ServerPlayer; @@ -21,54 +21,52 @@ public static void initialize() { public static void onUseItem(UseItemEvent event) { // Prevent player from using items while they're rescuing - if (HardcoreRevival.getManager().isRescuing(event.getPlayer())) { + if (HardcoreRevivalManager.isRescuing(event.getPlayer())) { event.setCanceled(true); } } public static void onUseBlock(UseBlockEvent event) { // Prevent player from placing blocks while they're rescuing - if (HardcoreRevival.getManager().isRescuing(event.getPlayer())) { + if (HardcoreRevivalManager.isRescuing(event.getPlayer())) { event.setCanceled(true); } } public static void onAttack(PlayerAttackEvent event) { // Stop rescuing if the player does something other than rescuing - HardcoreRevival.getManager().abortRescue(event.getPlayer()); + HardcoreRevivalManager.abortRescue(event.getPlayer()); } public static void onPlayerTick(ServerPlayer player) { // if (event.side == LogicalSide.SERVER && event.phase == TickEvent.Phase.END) { - HardcoreRevivalData revivalData = HardcoreRevival.getRevivalData(player); - Player rescueTarget = revivalData.getRescueTarget(); + Player rescueTarget = PlayerHardcoreRevivalManager.getRescueTarget(player); if (rescueTarget != null) { // Stop rescuing if the target logged out - HardcoreRevivalData rescueTargetData = HardcoreRevival.getRevivalData(rescueTarget); - final int knockoutTicksPassed = rescueTargetData.getKnockoutTicksPassed(); + final int knockoutTicksPassed = PlayerHardcoreRevivalManager.getKnockoutTicksPassed(rescueTarget); final int maxTicksUntilDeath = HardcoreRevivalConfig.getActive().secondsUntilDeath * 20; if (!rescueTarget.isAlive() || (maxTicksUntilDeath > 0 && knockoutTicksPassed >= maxTicksUntilDeath)) { - HardcoreRevival.getManager().abortRescue(player); + HardcoreRevivalManager.abortRescue(player); } else { // Stop rescuing if the player is out of range float dist = player.distanceTo(rescueTarget); if (dist > HardcoreRevivalConfig.getActive().rescueDistance) { - HardcoreRevival.getManager().abortRescue(player); + HardcoreRevivalManager.abortRescue(player); } else { - int rescueTime = revivalData.getRescueTime() + 1; - revivalData.setRescueTime(rescueTime); + int rescueTime = PlayerHardcoreRevivalManager.getRescueTime(player) + 1; + PlayerHardcoreRevivalManager.setRescueTime(player, rescueTime); // Delay death while rescuing - rescueTargetData.setKnockoutTicksPassed(knockoutTicksPassed - 1); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(rescueTarget, knockoutTicksPassed - 1); int maxRescueActionTicks = HardcoreRevivalConfig.getActive().rescueActionTicks; int step = maxRescueActionTicks / 4; if (rescueTime >= maxRescueActionTicks) { - HardcoreRevival.getManager().finishRescue(player); + HardcoreRevivalManager.finishRescue(player); } else if (rescueTime % step == 0) { Balm.getNetworking() .sendTo(player, new RevivalProgressMessage(rescueTarget.getId(), (float) rescueTime / (float) maxRescueActionTicks)); - KnockoutSyncHandler.sendHardcoreRevivalData(rescueTarget, rescueTarget, rescueTargetData, true); + KnockoutSyncHandler.sendHardcoreRevivalData(rescueTarget, rescueTarget, true); } } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/LivingEntityMixin.java b/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/LivingEntityMixin.java index b5259a2..c0ca979 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/LivingEntityMixin.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/LivingEntityMixin.java @@ -1,7 +1,8 @@ package net.blay09.mods.hardcorerevival.mixin; -import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.player.Player; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; @@ -12,7 +13,8 @@ public class LivingEntityMixin { @Inject(method = "canBeSeenAsEnemy()Z", at = @At("HEAD"), cancellable = true) private void canBeSeenAsEnemy(CallbackInfoReturnable cir) { - if (HardcoreRevival.getRevivalData((LivingEntity) (Object) this).isKnockedOut()) { + final var livingEntity = (LivingEntity) (Object) this; + if (livingEntity instanceof Player player && PlayerHardcoreRevivalManager.isKnockedOut(player)) { cir.setReturnValue(false); } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/PlayerMixin.java b/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/PlayerMixin.java index 4fabad2..58334e8 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/PlayerMixin.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/mixin/PlayerMixin.java @@ -1,7 +1,6 @@ package net.blay09.mods.hardcorerevival.mixin; import net.blay09.mods.hardcorerevival.MixinHooks; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; import net.minecraft.world.entity.player.Player; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/network/AcceptFateMessage.java b/common/src/main/java/net/blay09/mods/hardcorerevival/network/AcceptFateMessage.java index 9a60ff5..c52362e 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/network/AcceptFateMessage.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/network/AcceptFateMessage.java @@ -1,6 +1,7 @@ package net.blay09.mods.hardcorerevival.network; import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; @@ -16,7 +17,7 @@ public static void handle(ServerPlayer player, AcceptFateMessage message) { return; } - HardcoreRevival.getManager().notRescuedInTime(player); + HardcoreRevivalManager.notRescuedInTime(player); } @Override diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/network/HardcoreRevivalDataMessage.java b/common/src/main/java/net/blay09/mods/hardcorerevival/network/HardcoreRevivalDataMessage.java index fe32bbb..795868a 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/network/HardcoreRevivalDataMessage.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/network/HardcoreRevivalDataMessage.java @@ -1,7 +1,7 @@ package net.blay09.mods.hardcorerevival.network; import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.client.HardcoreRevivalClient; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; @@ -44,11 +44,9 @@ public static HardcoreRevivalDataMessage decode(FriendlyByteBuf buf) { public static void handle(Player player, HardcoreRevivalDataMessage message) { if (player != null) { Entity entity = player.level().getEntity(message.entityId); - if (entity != null) { - HardcoreRevivalData revivalData = entity.getId() == player.getId() ? HardcoreRevival.getClientRevivalData() : HardcoreRevival.getRevivalData( - entity); - revivalData.setKnockedOut(message.knockedOut); - revivalData.setKnockoutTicksPassed(message.knockoutTicksPassed); + if (entity instanceof Player targetEntity) { + PlayerHardcoreRevivalManager.setKnockedOut(targetEntity, message.knockedOut); + PlayerHardcoreRevivalManager.setKnockoutTicksPassed(targetEntity, message.knockoutTicksPassed); HardcoreRevivalClient.setBeingRescued(message.beingRescued); } } diff --git a/common/src/main/java/net/blay09/mods/hardcorerevival/network/RescueMessage.java b/common/src/main/java/net/blay09/mods/hardcorerevival/network/RescueMessage.java index 77b365e..425efb4 100644 --- a/common/src/main/java/net/blay09/mods/hardcorerevival/network/RescueMessage.java +++ b/common/src/main/java/net/blay09/mods/hardcorerevival/network/RescueMessage.java @@ -1,6 +1,8 @@ package net.blay09.mods.hardcorerevival.network; import net.blay09.mods.hardcorerevival.HardcoreRevival; +import net.blay09.mods.hardcorerevival.HardcoreRevivalManager; +import net.blay09.mods.hardcorerevival.PlayerHardcoreRevivalManager; import net.blay09.mods.hardcorerevival.config.HardcoreRevivalConfig; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.network.protocol.common.custom.CustomPacketPayload; @@ -44,14 +46,14 @@ private static boolean isLookingTowards(Player player, Entity candidate) { } public static void handle(ServerPlayer player, RescueMessage message) { - if (player == null || !player.isAlive() || player.isSpectator() || HardcoreRevival.getRevivalData(player).isKnockedOut()) { + if (player == null || !player.isAlive() || player.isSpectator() || PlayerHardcoreRevivalManager.isKnockedOut(player)) { return; } if (message.active) { final double range = HardcoreRevivalConfig.getActive().rescueDistance; List candidates = player.level().getEntitiesOfClass(Player.class, player.getBoundingBox().inflate(range), p -> { - if (p == null || !HardcoreRevival.getRevivalData(p).isKnockedOut()) { + if (p == null || !PlayerHardcoreRevivalManager.isKnockedOut(p)) { return false; } @@ -72,11 +74,11 @@ public static void handle(ServerPlayer player, RescueMessage message) { } } if (target != null) { - HardcoreRevival.getManager().startRescue(player, target); + HardcoreRevivalManager.startRescue(player, target); } } else { - HardcoreRevival.getManager().abortRescue(player); + HardcoreRevivalManager.abortRescue(player); } } diff --git a/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricHardcoreRevival.java b/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricHardcoreRevival.java index f9c042a..df15ca2 100644 --- a/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricHardcoreRevival.java +++ b/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricHardcoreRevival.java @@ -4,7 +4,6 @@ import net.blay09.mods.balm.api.EmptyLoadContext; import net.blay09.mods.balm.fabric.provider.FabricBalmProviders; import net.blay09.mods.hardcorerevival.HardcoreRevival; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.lookup.v1.entity.EntityApiLookup; import net.minecraft.resources.ResourceLocation; diff --git a/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricPlayer.java b/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricPlayer.java index 6d4d816..f8b2678 100644 --- a/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricPlayer.java +++ b/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/FabricPlayer.java @@ -1,7 +1,5 @@ package net.blay09.mods.hardcorerevival.fabric; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; - public interface FabricPlayer { HardcoreRevivalData getHardcoreRevivalData(); } diff --git a/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/mixin/FabricPlayerMixin.java b/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/mixin/FabricPlayerMixin.java index 4e2edb7..a737e85 100644 --- a/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/mixin/FabricPlayerMixin.java +++ b/fabric/src/main/java/net/blay09/mods/hardcorerevival/fabric/mixin/FabricPlayerMixin.java @@ -1,7 +1,6 @@ package net.blay09.mods.hardcorerevival.fabric.mixin; import net.blay09.mods.hardcorerevival.fabric.FabricPlayer; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalDataImpl; import net.minecraft.world.entity.player.Player; import org.spongepowered.asm.mixin.Mixin; diff --git a/forge/src/main/java/net/blay09/mods/hardcorerevival/ForgeHardcoreRevival.java b/forge/src/main/java/net/blay09/mods/hardcorerevival/ForgeHardcoreRevival.java index 3d704e9..8091d72 100644 --- a/forge/src/main/java/net/blay09/mods/hardcorerevival/ForgeHardcoreRevival.java +++ b/forge/src/main/java/net/blay09/mods/hardcorerevival/ForgeHardcoreRevival.java @@ -4,10 +4,8 @@ import net.blay09.mods.balm.api.EmptyLoadContext; import net.blay09.mods.balm.api.client.BalmClient; import net.blay09.mods.balm.forge.provider.ForgeBalmProviders; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalDataImpl; import net.blay09.mods.hardcorerevival.client.HardcoreRevivalClient; -import net.minecraft.client.Minecraft; import net.minecraft.core.Direction; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.entity.Entity; @@ -21,9 +19,7 @@ import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.Objects; @Mod(HardcoreRevival.MOD_ID) public class ForgeHardcoreRevival { diff --git a/neoforge/src/main/java/net/blay09/mods/hardcorerevival/NeoForgeHardcoreRevival.java b/neoforge/src/main/java/net/blay09/mods/hardcorerevival/NeoForgeHardcoreRevival.java index 2826595..ae26d0b 100644 --- a/neoforge/src/main/java/net/blay09/mods/hardcorerevival/NeoForgeHardcoreRevival.java +++ b/neoforge/src/main/java/net/blay09/mods/hardcorerevival/NeoForgeHardcoreRevival.java @@ -2,33 +2,15 @@ import net.blay09.mods.balm.api.Balm; import net.blay09.mods.balm.neoforge.NeoForgeLoadContext; -import net.blay09.mods.balm.neoforge.provider.NeoForgeBalmProviders; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalData; -import net.blay09.mods.hardcorerevival.capability.HardcoreRevivalDataImpl; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.entity.EntityType; import net.neoforged.bus.api.IEventBus; import net.neoforged.fml.common.Mod; -import net.neoforged.neoforge.capabilities.EntityCapability; -import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent; @Mod(HardcoreRevival.MOD_ID) public class NeoForgeHardcoreRevival { - private final EntityCapability hardcoreRevivalDataCapability = EntityCapability.createVoid(ResourceLocation.fromNamespaceAndPath(HardcoreRevival.MOD_ID, - "entity_data"), HardcoreRevivalData.class); - public NeoForgeHardcoreRevival(IEventBus eventBus) { final var context = new NeoForgeLoadContext(eventBus); Balm.initialize(HardcoreRevival.MOD_ID, context, HardcoreRevival::initialize); - - eventBus.addListener(this::registerCapabilities); - - NeoForgeBalmProviders providers = (NeoForgeBalmProviders) Balm.getProviders(); - providers.registerEntityProvider(HardcoreRevivalData.class, hardcoreRevivalDataCapability); } - private void registerCapabilities(RegisterCapabilitiesEvent event) { - event.registerEntity(hardcoreRevivalDataCapability, EntityType.PLAYER, (player, context) -> new HardcoreRevivalDataImpl()); - } }