WIP: Creating DataComponent for totem items.
This commit is contained in:
@ -0,0 +1,49 @@
|
||||
package dev.micle.totemofreviving.component;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.codecs.RecordCodecBuilder;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class TotemData {
|
||||
public static final Codec<TotemData> CODEC = RecordCodecBuilder.create(instance ->
|
||||
instance.group(
|
||||
Codec.INT.fieldOf("targetIndex").forGetter(TotemData::getTargetIndex)
|
||||
).apply(instance, TotemData::new)
|
||||
);
|
||||
public static final StreamCodec<ByteBuf, TotemData> STREAM_CODEC = StreamCodec.composite(
|
||||
ByteBufCodecs.INT, TotemData::getTargetIndex,
|
||||
TotemData::new
|
||||
);
|
||||
|
||||
private int targetIndex;
|
||||
|
||||
public TotemData(int targetIndex) {
|
||||
this.targetIndex = targetIndex;
|
||||
}
|
||||
|
||||
public int getTargetIndex() {
|
||||
return targetIndex;
|
||||
}
|
||||
public void setTargetIndex(int newTargetIndex) {
|
||||
targetIndex = newTargetIndex;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.targetIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj == this) {
|
||||
return true;
|
||||
} else {
|
||||
return obj instanceof TotemData td
|
||||
&& this.targetIndex == td.targetIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,17 @@
|
||||
package dev.micle.totemofreviving.item.totem;
|
||||
|
||||
import dev.micle.totemofreviving.component.TotemData;
|
||||
import dev.micle.totemofreviving.network.NetworkManager;
|
||||
import dev.micle.totemofreviving.network.client.ChangeTargetPacket;
|
||||
import dev.micle.totemofreviving.network.client.ChargeTotemPacket;
|
||||
import dev.micle.totemofreviving.network.client.ReviveTargetPacket;
|
||||
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;
|
||||
@ -18,12 +23,14 @@ import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Rarity;
|
||||
import net.minecraft.world.item.TooltipFlag;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.api.distmarker.OnlyIn;
|
||||
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;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
public abstract class TotemItem extends Item {
|
||||
@ -38,18 +45,24 @@ public abstract class TotemItem extends Item {
|
||||
}
|
||||
|
||||
public TotemItem(Rarity rarity, int durability) {
|
||||
super(new Properties().stacksTo(1).rarity(rarity).defaultDurability(durability));
|
||||
super(new Properties().stacksTo(1).rarity(rarity).durability(durability));
|
||||
}
|
||||
|
||||
public abstract boolean isCharge(ItemStack stack);
|
||||
|
||||
private static Optional<TotemData> getTotemData(ItemStack stack) {
|
||||
return Optional.ofNullable(stack.getComponents().get(ModDataComponents.TOTEM_DATA.get()));
|
||||
}
|
||||
|
||||
public static int getTargetIndex(ItemStack stack) {
|
||||
if (!isTotem(stack)) { return -1; }
|
||||
return stack.getOrCreateTag().getInt(TAG_TARGET_INDEX);
|
||||
return getTotemData(stack).map(TotemData::getTargetIndex).orElse(-1);
|
||||
}
|
||||
public static void setTargetIndex(ItemStack stack, int targetIndex) {
|
||||
if (!isTotem(stack)) { return; }
|
||||
stack.getOrCreateTag().putInt(TAG_TARGET_INDEX, targetIndex);
|
||||
Optional<TotemData> data = getTotemData(stack);
|
||||
if (data.isEmpty()) { return; }
|
||||
|
||||
data.get().setTargetIndex(targetIndex);
|
||||
stack.set(ModDataComponents.TOTEM_DATA.get(), data.get());
|
||||
}
|
||||
|
||||
public static UUID getTargetUUID(ItemStack stack) {
|
||||
|
||||
@ -0,0 +1,16 @@
|
||||
package dev.micle.totemofreviving.setup;
|
||||
|
||||
import dev.micle.totemofreviving.component.TotemData;
|
||||
import net.minecraft.core.component.DataComponentType;
|
||||
import net.neoforged.neoforge.registries.DeferredHolder;
|
||||
|
||||
public class ModDataComponents {
|
||||
public static final DeferredHolder<DataComponentType<?>, DataComponentType<TotemData>> TOTEM_DATA = Registration.DATA_COMPONENTS.registerComponentType(
|
||||
"basic",
|
||||
builder -> builder
|
||||
.persistent(TotemData.CODEC)
|
||||
.networkSynchronized(TotemData.STREAM_CODEC)
|
||||
);
|
||||
|
||||
public static void register() {}
|
||||
}
|
||||
@ -1,6 +1,7 @@
|
||||
package dev.micle.totemofreviving.setup;
|
||||
|
||||
import dev.micle.totemofreviving.TotemOfReviving;
|
||||
import net.minecraft.core.component.DataComponentType;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.world.item.CreativeModeTab;
|
||||
import net.neoforged.bus.api.IEventBus;
|
||||
@ -8,6 +9,9 @@ import net.neoforged.neoforge.registries.DeferredRegister;
|
||||
|
||||
public class Registration {
|
||||
// Initialize variables
|
||||
public static final DeferredRegister.DataComponents DATA_COMPONENTS =
|
||||
DeferredRegister.createDataComponents(Registries.DATA_COMPONENT_TYPE, TotemOfReviving.MOD_ID);
|
||||
|
||||
public static final DeferredRegister.Items ITEMS =
|
||||
DeferredRegister.createItems(TotemOfReviving.MOD_ID);
|
||||
|
||||
@ -15,6 +19,9 @@ public class Registration {
|
||||
DeferredRegister.create(Registries.CREATIVE_MODE_TAB, TotemOfReviving.MOD_ID);
|
||||
|
||||
public static void register(IEventBus modEventBus) {
|
||||
DATA_COMPONENTS.register(modEventBus);
|
||||
ModDataComponents.register();
|
||||
|
||||
ITEMS.register(modEventBus);
|
||||
ModItems.register();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user