Skip to content

Commit

Permalink
fix: Stop using capability because they're no longer persistent on Ne…
Browse files Browse the repository at this point in the history
…oForge which caused the mod to make you invulnerable
  • Loading branch information
BlayTheNinth committed Oct 30, 2024
1 parent 3499dbb commit f2b823a
Show file tree
Hide file tree
Showing 30 changed files with 472 additions and 429 deletions.
Original file line number Diff line number Diff line change
@@ -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<HardcoreRevivalManager> 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() {
Expand All @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,40 +30,34 @@ public class HardcoreRevivalManager {
public static final ResourceKey<DamageType> 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;
}

player.stopUsingItem();
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
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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));
}
Expand All @@ -160,69 +150,63 @@ 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);
}

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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}
}
Loading

0 comments on commit f2b823a

Please sign in to comment.