Included charge in totem data.
This commit is contained in:
@ -14,7 +14,8 @@ public class TotemData {
|
||||
Codec.INT.fieldOf("targetIndex").forGetter(TotemData::getTargetIndex),
|
||||
Codec.STRING.fieldOf("targetUUID").forGetter(TotemData::getTargetUUID),
|
||||
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)
|
||||
);
|
||||
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::getTargetName,
|
||||
ByteBufCodecs.INT, TotemData::getTargetDeaths,
|
||||
ByteBufCodecs.INT, TotemData::getCharge,
|
||||
TotemData::new
|
||||
);
|
||||
|
||||
@ -29,12 +31,14 @@ public class TotemData {
|
||||
private String targetUUID;
|
||||
private String targetName;
|
||||
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.targetUUID = targetUUID;
|
||||
this.targetName = targetName;
|
||||
this.targetDeaths = targetDeaths;
|
||||
this.charge = charge;
|
||||
}
|
||||
|
||||
public int getTargetIndex() {
|
||||
@ -65,9 +69,16 @@ public class TotemData {
|
||||
this.targetDeaths = targetDeaths;
|
||||
}
|
||||
|
||||
public int getCharge() {
|
||||
return charge;
|
||||
}
|
||||
public void setCharge(int charge) {
|
||||
this.charge = charge;
|
||||
}
|
||||
|
||||
@Override
|
||||
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
|
||||
@ -79,7 +90,8 @@ public class TotemData {
|
||||
&& this.targetIndex == td.targetIndex
|
||||
&& Objects.equals(this.targetUUID, td.targetUUID)
|
||||
&& Objects.equals(this.targetName, td.targetName)
|
||||
&& this.targetDeaths == td.targetDeaths;
|
||||
&& this.targetDeaths == td.targetDeaths
|
||||
&& this.charge == td.charge;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,9 +9,6 @@ import dev.micle.totemofreviving.setup.Config;
|
||||
import dev.micle.totemofreviving.setup.ModDataComponents;
|
||||
import dev.micle.totemofreviving.setup.ModKeyMappings;
|
||||
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.server.level.ServerPlayer;
|
||||
import net.minecraft.stats.Stats;
|
||||
@ -25,7 +22,6 @@ import net.minecraft.world.item.TooltipFlag;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.neoforged.api.distmarker.Dist;
|
||||
import net.neoforged.api.distmarker.OnlyIn;
|
||||
import net.neoforged.fml.common.Mod;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@ -34,12 +30,6 @@ import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
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) {
|
||||
this(Rarity.COMMON, durability);
|
||||
}
|
||||
@ -120,19 +110,16 @@ public abstract class TotemItem extends Item {
|
||||
return true;
|
||||
}
|
||||
|
||||
public static int getCharge(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return -1; }
|
||||
int charge = stack.getOrCreateTag().getInt(TAG_CHARGE);
|
||||
int maxCharge = getMaxCharge(stack);
|
||||
if (charge > maxCharge) {
|
||||
charge = maxCharge;
|
||||
setCharge(stack, charge);
|
||||
}
|
||||
return charge;
|
||||
@Nullable
|
||||
public static Integer getCharge(ItemStack stack) {
|
||||
return getTotemData(stack).map(TotemData::getCharge).orElse(null);
|
||||
}
|
||||
public static void setCharge(ItemStack stack, int charge) {
|
||||
if (!isTotem(stack)) { return; }
|
||||
stack.getOrCreateTag().putInt(TAG_CHARGE, charge);
|
||||
Optional<TotemData> data = getTotemData(stack);
|
||||
if (data.isEmpty()) { return; }
|
||||
|
||||
data.get().setCharge(charge);
|
||||
stack.set(ModDataComponents.TOTEM_DATA.get(), data.get());
|
||||
}
|
||||
|
||||
public static int getMaxCharge(ItemStack stack) {
|
||||
|
||||
Reference in New Issue
Block a user