aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <william@herges.fr>2025-10-13 13:34:43 +0200
committerAnhgelus Morhtuuzh <william@herges.fr>2025-10-13 14:39:33 +0200
commit9f0ec65c646f7b2d34a6740173f577092680de74 (patch)
treebb98aaabf46e69b16822e854eee18c12f17dfd6d /src/main/java
parentc5c04af3d53543fd497d44b191f1b52e8cea5bc3 (diff)
feat(player): disable waypoint
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/world/anhgelus/lifesteal/LifeSteal.java14
-rw-r--r--src/main/java/world/anhgelus/lifesteal/LifeStealer.java29
2 files changed, 31 insertions, 12 deletions
diff --git a/src/main/java/world/anhgelus/lifesteal/LifeSteal.java b/src/main/java/world/anhgelus/lifesteal/LifeSteal.java
index d495792..cc1b4eb 100644
--- a/src/main/java/world/anhgelus/lifesteal/LifeSteal.java
+++ b/src/main/java/world/anhgelus/lifesteal/LifeSteal.java
@@ -30,12 +30,12 @@ import static world.anhgelus.lifesteal.LifeStealer.Manager.getLifeStealer;
public class LifeSteal implements ModInitializer {
public static final String MOD_ID = "lifesteal";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
- public static float MIN_HEALTH = 0;
- public static float MAX_HEALTH = 40;
+ public static int MIN_HEALTH = 0;
+ public static int MAX_HEALTH = 40;
public static final GameRules.Key<GameRules.IntRule> RULE_MAX_HEARTS = GameRuleRegistry.register(
MOD_ID + ":maxHearts",
GameRules.Category.MISC,
- GameRuleFactory.createIntRule((int) MAX_HEALTH / 2, (server, rule) -> {
+ GameRuleFactory.createIntRule(MAX_HEALTH / 2, (server, rule) -> {
MAX_HEALTH = rule.get() * 2;
})
);
@@ -63,6 +63,14 @@ public class LifeSteal implements ModInitializer {
CAN_STORE_HEARTS_IN_EC = rule.get();
})
);
+ public static boolean ENABLE_WAYPOINT = false;
+ public static final GameRules.Key<GameRules.BooleanRule> RULE_ENABLE_WAYPOINT = GameRuleRegistry.register(
+ MOD_ID + ":enableWaypoint",
+ GameRules.Category.MISC,
+ GameRuleFactory.createBooleanRule(ENABLE_WAYPOINT, (server, rule) -> {
+ ENABLE_WAYPOINT = rule.get();
+ })
+ );
@Override
public void onInitialize() {
diff --git a/src/main/java/world/anhgelus/lifesteal/LifeStealer.java b/src/main/java/world/anhgelus/lifesteal/LifeStealer.java
index ca8a34e..2d1268a 100644
--- a/src/main/java/world/anhgelus/lifesteal/LifeStealer.java
+++ b/src/main/java/world/anhgelus/lifesteal/LifeStealer.java
@@ -4,12 +4,14 @@ import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.NbtComponent;
+import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.entity.attribute.EntityAttributeModifier;
import net.minecraft.entity.attribute.EntityAttributes;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
+import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.server.BannedPlayerEntry;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
@@ -22,6 +24,7 @@ import java.util.UUID;
public class LifeStealer {
public static final Identifier HEALTH_MODIFIER = Identifier.of(LifeSteal.MOD_ID, "health_modifier");
+ public static final Identifier WAYPOINT_MODIFIER = Identifier.of(LifeSteal.MOD_ID, "waypoint_modifier");
public static final String PLAYER_KEY = "player";
private final UUID uuid;
private ServerPlayerEntity player;
@@ -43,14 +46,19 @@ public class LifeStealer {
return MathHelper.clamp(healthModifier, LifeSteal.MIN_HEALTH - baseHealth, LifeSteal.MAX_HEALTH - baseHealth);
}
- private void updateHealth() {
- final var attr = player.getAttributeInstance(EntityAttributes.MAX_HEALTH);
- if (attr == null) throw new IllegalStateException("No health attribute assigned to LifeStealer");
- if (attr.hasModifier(HEALTH_MODIFIER)) attr.removeModifier(HEALTH_MODIFIER);
- final var modifier = new EntityAttributeModifier(HEALTH_MODIFIER, healthModifier, EntityAttributeModifier.Operation.ADD_VALUE);
+ private void updateModifier(RegistryEntry<EntityAttribute> attribute, Identifier id, float val, EntityAttributeModifier.Operation op) {
+ final var attr = player.getAttributeInstance(attribute);
+ if (attr == null)
+ throw new IllegalStateException("No attribute " + attribute.getIdAsString() + " assigned to LifeStealer");
+ if (attr.hasModifier(id)) attr.removeModifier(id);
+ final var modifier = new EntityAttributeModifier(id, val, op);
attr.addTemporaryModifier(modifier);
}
+ private void updateHealth() {
+ updateModifier(EntityAttributes.MAX_HEALTH, HEALTH_MODIFIER, healthModifier, EntityAttributeModifier.Operation.ADD_VALUE);
+ }
+
public ItemStack getHeart() {
final var nbt = new NbtCompound();
nbt.putString(PLAYER_KEY, player.getUuid().toString());
@@ -64,6 +72,9 @@ public class LifeStealer {
if (!newPlayer.getUuid().equals(uuid)) throw new IllegalArgumentException("Player does not have the same UUID");
this.player = newPlayer;
updateHealth();
+ if (LifeSteal.ENABLE_WAYPOINT) return;
+ updateModifier(EntityAttributes.WAYPOINT_RECEIVE_RANGE, WAYPOINT_MODIFIER.withSuffixedPath("/receive"), -60_000_000, EntityAttributeModifier.Operation.ADD_VALUE);
+ updateModifier(EntityAttributes.WAYPOINT_TRANSMIT_RANGE, WAYPOINT_MODIFIER.withSuffixedPath("/transmit"), -60_000_000, EntityAttributeModifier.Operation.ADD_VALUE);
}
public boolean kill() {
@@ -90,6 +101,7 @@ public class LifeStealer {
.add(entry);
return;
}
+ // technically, this is useless
updateHealth();
}
@@ -128,10 +140,9 @@ public class LifeStealer {
public static boolean validHeart(ItemStack is) {
if (is.isEmpty()) return false;
if (!is.isOf(Items.NETHER_STAR)) return false;
- final var nbtComponent = is.get(DataComponentTypes.CUSTOM_DATA);
- if (nbtComponent == null || nbtComponent.isEmpty()) return false;
- final var nbt = nbtComponent.copyNbt();
- return nbt.contains(PLAYER_KEY);
+ final var nbt = is.get(DataComponentTypes.CUSTOM_DATA);
+ if (nbt == null || nbt.isEmpty()) return false;
+ return nbt.copyNbt().contains(PLAYER_KEY);
}
}
}