Included charge in totem data.

This commit is contained in:
2026-01-09 05:40:01 +01:00
parent 56f274a81b
commit f41bead3d2
2 changed files with 25 additions and 26 deletions

View File

@ -14,7 +14,8 @@ public class TotemData {
Codec.INT.fieldOf("targetIndex").forGetter(TotemData::getTargetIndex), Codec.INT.fieldOf("targetIndex").forGetter(TotemData::getTargetIndex),
Codec.STRING.fieldOf("targetUUID").forGetter(TotemData::getTargetUUID), Codec.STRING.fieldOf("targetUUID").forGetter(TotemData::getTargetUUID),
Codec.STRING.fieldOf("targetName").forGetter(TotemData::getTargetName), Codec.STRING.fieldOf("targetName").forGetter(TotemData::getTargetName),
Codec.INT.fieldOf("targetDeaths").forGetter(TotemData::getTargetDeaths) Codec.INT.fieldOf("targetDeaths").forGetter(TotemData::getTargetDeaths),
Codec.INT.fieldOf("charge").forGetter(TotemData::getCharge)
).apply(instance, TotemData::new) ).apply(instance, TotemData::new)
); );
public static final StreamCodec<ByteBuf, TotemData> STREAM_CODEC = StreamCodec.composite( public static final StreamCodec<ByteBuf, TotemData> STREAM_CODEC = StreamCodec.composite(
@ -22,6 +23,7 @@ public class TotemData {
ByteBufCodecs.STRING_UTF8, TotemData::getTargetUUID, ByteBufCodecs.STRING_UTF8, TotemData::getTargetUUID,
ByteBufCodecs.STRING_UTF8, TotemData::getTargetName, ByteBufCodecs.STRING_UTF8, TotemData::getTargetName,
ByteBufCodecs.INT, TotemData::getTargetDeaths, ByteBufCodecs.INT, TotemData::getTargetDeaths,
ByteBufCodecs.INT, TotemData::getCharge,
TotemData::new TotemData::new
); );
@ -29,12 +31,14 @@ public class TotemData {
private String targetUUID; private String targetUUID;
private String targetName; private String targetName;
private int targetDeaths; private int targetDeaths;
private int charge;
public TotemData(int targetIndex, String targetUUID, String targetName, int targetDeaths) { public TotemData(int targetIndex, String targetUUID, String targetName, int targetDeaths, int charge) {
this.targetIndex = targetIndex; this.targetIndex = targetIndex;
this.targetUUID = targetUUID; this.targetUUID = targetUUID;
this.targetName = targetName; this.targetName = targetName;
this.targetDeaths = targetDeaths; this.targetDeaths = targetDeaths;
this.charge = charge;
} }
public int getTargetIndex() { public int getTargetIndex() {
@ -65,9 +69,16 @@ public class TotemData {
this.targetDeaths = targetDeaths; this.targetDeaths = targetDeaths;
} }
public int getCharge() {
return charge;
}
public void setCharge(int charge) {
this.charge = charge;
}
@Override @Override
public int hashCode() { public int hashCode() {
return Objects.hash(this.targetIndex, this.targetUUID, this.targetName, this.targetDeaths); return Objects.hash(this.targetIndex, this.targetUUID, this.targetName, this.targetDeaths, this.charge);
} }
@Override @Override
@ -79,7 +90,8 @@ public class TotemData {
&& this.targetIndex == td.targetIndex && this.targetIndex == td.targetIndex
&& Objects.equals(this.targetUUID, td.targetUUID) && Objects.equals(this.targetUUID, td.targetUUID)
&& Objects.equals(this.targetName, td.targetName) && Objects.equals(this.targetName, td.targetName)
&& this.targetDeaths == td.targetDeaths; && this.targetDeaths == td.targetDeaths
&& this.charge == td.charge;
} }
} }
} }

View File

@ -9,9 +9,6 @@ import dev.micle.totemofreviving.setup.Config;
import dev.micle.totemofreviving.setup.ModDataComponents; import dev.micle.totemofreviving.setup.ModDataComponents;
import dev.micle.totemofreviving.setup.ModKeyMappings; import dev.micle.totemofreviving.setup.ModKeyMappings;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.core.component.DataComponentType;
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.stats.Stats; import net.minecraft.stats.Stats;
@ -25,7 +22,6 @@ import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.neoforged.api.distmarker.Dist; import net.neoforged.api.distmarker.Dist;
import net.neoforged.api.distmarker.OnlyIn; import net.neoforged.api.distmarker.OnlyIn;
import net.neoforged.fml.common.Mod;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
@ -34,12 +30,6 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
public abstract class TotemItem extends Item { public abstract class TotemItem extends Item {
private static final String TAG_TARGET_INDEX = "target_index";
private static final String TAG_TARGET_UUID = "target_uuid";
private static final String TAG_TARGET_NAME = "target_name";
private static final String TAG_TARGET_DEATHS = "target_deaths";
private static final String TAG_CHARGE = "charge";
public TotemItem(int durability) { public TotemItem(int durability) {
this(Rarity.COMMON, durability); this(Rarity.COMMON, durability);
} }
@ -120,19 +110,16 @@ public abstract class TotemItem extends Item {
return true; return true;
} }
public static int getCharge(ItemStack stack) { @Nullable
if (!isTotem(stack)) { return -1; } public static Integer getCharge(ItemStack stack) {
int charge = stack.getOrCreateTag().getInt(TAG_CHARGE); return getTotemData(stack).map(TotemData::getCharge).orElse(null);
int maxCharge = getMaxCharge(stack);
if (charge > maxCharge) {
charge = maxCharge;
setCharge(stack, charge);
}
return charge;
} }
public static void setCharge(ItemStack stack, int charge) { public static void setCharge(ItemStack stack, int charge) {
if (!isTotem(stack)) { return; } Optional<TotemData> data = getTotemData(stack);
stack.getOrCreateTag().putInt(TAG_CHARGE, charge); if (data.isEmpty()) { return; }
data.get().setCharge(charge);
stack.set(ModDataComponents.TOTEM_DATA.get(), data.get());
} }
public static int getMaxCharge(ItemStack stack) { public static int getMaxCharge(ItemStack stack) {