generated from NeoForgeMDKs/MDK-1.21-NeoGradle
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1 parent
8eac042
commit 2455b89
Showing
12 changed files
with
407 additions
and
26 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
src/main/java/com/portingdeadmods/bagz/api/BagzCapabilities.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.portingdeadmods.bagz.api; | ||
|
||
import com.portingdeadmods.bagz.Bagz; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.neoforged.neoforge.capabilities.CapabilityRegistry; | ||
import net.neoforged.neoforge.capabilities.EntityCapability; | ||
|
||
public class BagzCapabilities { | ||
public static final EntityCapability<IPersonalBagProvider, Void> PERS_BAG_CAPABILITY = EntityCapability.create( | ||
ResourceLocation.fromNamespaceAndPath(Bagz.MODID, "personal_bag_capability"), | ||
IPersonalBagProvider.class, | ||
Void.class | ||
); | ||
|
||
public static final EntityCapability<IBagProvider, Void> BAG_CAPABILITY = EntityCapability.create( | ||
ResourceLocation.fromNamespaceAndPath(Bagz.MODID, "bag_capability"), | ||
IBagProvider.class, | ||
Void.class | ||
); | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/portingdeadmods/bagz/api/IBagProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.portingdeadmods.bagz.api; | ||
|
||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.item.DyeColor; | ||
import net.neoforged.neoforge.common.util.INBTSerializable; | ||
import net.neoforged.neoforge.items.IItemHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface IBagProvider extends INBTSerializable<CompoundTag> { | ||
/** | ||
* Note: modifying this clientside is not advised | ||
* | ||
* @param color The bag color to acquire | ||
* | ||
* @return The inventory representing this bag | ||
*/ | ||
@NotNull | ||
IItemHandler getBag(@NotNull DyeColor color); | ||
|
||
/** | ||
* Syncs the bag inventory associated with this color to the player provided (usually the owner of this capability instance) | ||
* | ||
* @param color The bag color to sync. If null, syncs every color. | ||
* @param player The player to sync the bags to. | ||
*/ | ||
void sync(DyeColor color, @NotNull ServerPlayer player); | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/portingdeadmods/bagz/api/IPersonalBagProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.portingdeadmods.bagz.api; | ||
|
||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.item.DyeColor; | ||
import net.neoforged.neoforge.common.util.INBTSerializable; | ||
import net.neoforged.neoforge.items.IItemHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public interface IPersonalBagProvider extends INBTSerializable<CompoundTag> { | ||
@NotNull IItemHandler getBag(@NotNull int color); | ||
|
||
/** | ||
* Note: modifying this clientside is not advised | ||
* | ||
* @param color The bag color (hex code) to acquire | ||
* | ||
* @return The inventory representing this alchemical bag | ||
*/ | ||
@NotNull | ||
IItemHandler getBag(@NotNull Integer color); | ||
|
||
/** | ||
* Syncs the bag inventory associated with this color to the player provided (usually the owner of this capability instance) | ||
* | ||
* @param color The bag color to sync. If null, syncs every color. | ||
* @param player The player to sync the bags to. | ||
*/ | ||
void sync(int color, @NotNull ServerPlayer player); | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/com/portingdeadmods/bagz/content/items/BagImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.portingdeadmods.bagz.content.items; | ||
|
||
import com.portingdeadmods.bagz.api.BagzCapabilities; | ||
import com.portingdeadmods.bagz.api.IBagProvider; | ||
import com.portingdeadmods.bagz.networking.SyncBagzPayload; | ||
import net.minecraft.core.Holder; | ||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.item.DyeColor; | ||
import net.neoforged.neoforge.capabilities.EntityCapability; | ||
import net.neoforged.neoforge.items.IItemHandler; | ||
import net.neoforged.neoforge.items.ItemStackHandler; | ||
import net.neoforged.neoforge.network.PacketDistributor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.UnknownNullability; | ||
|
||
import java.util.EnumMap; | ||
import java.util.Map; | ||
|
||
public final class BagImplementation { | ||
|
||
public static IBagProvider getDefault() { | ||
return new DefaultImpl(); | ||
} | ||
|
||
public static class DefaultImpl implements IBagProvider { | ||
|
||
private final Map<DyeColor, ItemStackHandler> inventories = new EnumMap<>(DyeColor.class); | ||
|
||
@Override | ||
public @NotNull IItemHandler getBag(@NotNull DyeColor color) { | ||
if (!inventories.containsKey(color)) { | ||
inventories.put(color, new ItemStackHandler(9 * 15)); | ||
} | ||
return inventories.get(color); | ||
} | ||
|
||
@Override | ||
public void sync(DyeColor color, @NotNull ServerPlayer player) { | ||
PacketDistributor.sendToPlayer(player, new SyncBagzPayload(writeNBT(player.level().registryAccess(), color))); | ||
} | ||
|
||
public CompoundTag writeNBT(HolderLookup.Provider provider, DyeColor color) { | ||
CompoundTag ret = new CompoundTag(); | ||
DyeColor[] colors = color == null ? DyeColor.values() : new DyeColor[]{color}; | ||
for (DyeColor c : colors) { | ||
if (inventories.containsKey(c)) { | ||
ret.put(c.getSerializedName(), inventories.get(c).serializeNBT(provider)); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
|
||
@Override | ||
public @UnknownNullability CompoundTag serializeNBT(HolderLookup.Provider provider) { | ||
return writeNBT(provider, null); | ||
} | ||
|
||
@Override | ||
public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) { | ||
for (DyeColor e : DyeColor.values()) { | ||
if (nbt.contains(e.getSerializedName())) { | ||
ItemStackHandler inv = new ItemStackHandler(104); | ||
inv.deserializeNBT(provider, nbt.getCompound(e.getSerializedName())); | ||
inventories.put(e, inv); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/com/portingdeadmods/bagz/content/items/PersonalBagImplementation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package com.portingdeadmods.bagz.content.items; | ||
|
||
import com.portingdeadmods.bagz.api.IPersonalBagProvider; | ||
import com.portingdeadmods.bagz.networking.SyncBagzPayload; | ||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.item.DyeColor; | ||
import net.neoforged.neoforge.items.IItemHandler; | ||
import net.neoforged.neoforge.items.ItemStackHandler; | ||
import net.neoforged.neoforge.network.PacketDistributor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.UnknownNullability; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public final class PersonalBagImplementation { | ||
|
||
public static IPersonalBagProvider getDefault() { | ||
return new DefaultImpl(); | ||
} | ||
|
||
public static class DefaultImpl implements IPersonalBagProvider { | ||
|
||
private final Map<Integer, ItemStackHandler> inventories = new HashMap<Integer, ItemStackHandler>(); | ||
|
||
@Override | ||
public @NotNull IItemHandler getBag(@NotNull int color) { | ||
if (!inventories.containsKey(color)) { | ||
inventories.put(color, new ItemStackHandler(9 * 15)); | ||
} | ||
return inventories.get(color); | ||
} | ||
|
||
@Override | ||
public void sync(int color, @NotNull ServerPlayer player) { | ||
PacketDistributor.sendToPlayer(player, new SyncBagzPayload(writeNBT(player.level().registryAccess(), color))); | ||
} | ||
|
||
public CompoundTag writeNBT(HolderLookup.Provider provider, Integer color) { | ||
CompoundTag ret = new CompoundTag(); | ||
DyeColor[] colors = color == null ? DyeColor.values() : new DyeColor[]{color}; | ||
for (DyeColor c : colors) { | ||
if (inventories.containsKey(c)) { | ||
ret.put(c.getSerializedName(), inventories.get(c).serializeNBT(provider)); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
|
||
@Override | ||
public @UnknownNullability CompoundTag serializeNBT(HolderLookup.Provider provider) { | ||
return writeNBT(provider, null); | ||
} | ||
|
||
@Override | ||
public void deserializeNBT(HolderLookup.Provider provider, CompoundTag nbt) { | ||
for (DyeColor e : DyeColor.values()) { | ||
if (nbt.contains(e.getSerializedName())) { | ||
ItemStackHandler inv = new ItemStackHandler(104); | ||
inv.deserializeNBT(provider, nbt.getCompound(e.getSerializedName())); | ||
inventories.put(e, inv); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/portingdeadmods/bagz/content/menus/BagSlot.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.portingdeadmods.bagz.content.menus; | ||
|
||
import net.minecraft.world.Container; | ||
import net.minecraft.world.inventory.Slot; | ||
|
||
public class BagSlot extends Slot { | ||
public BagSlot(Container container, int index, int x, int y) { | ||
super(container, index, x, y); | ||
} | ||
|
||
@Override | ||
public boolean setChanged(ItemStack stack) { | ||
return false; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/portingdeadmods/bagz/data/DataAttachments.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.portingdeadmods.bagz.data; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import com.portingdeadmods.bagz.Bagz; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.core.RegistryAccess; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.nbt.NbtOps; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.DyeColor; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.neoforged.neoforge.attachment.AttachmentType; | ||
import net.neoforged.neoforge.items.ItemStackHandler; | ||
import net.neoforged.neoforge.registries.DeferredRegister; | ||
import net.neoforged.neoforge.registries.NeoForgeRegistries; | ||
|
||
import java.util.*; | ||
import java.util.function.Supplier; | ||
|
||
public class DataAttachments { | ||
|
||
public static final DeferredRegister<AttachmentType<?>> ATTACHMENTS = DeferredRegister.create(NeoForgeRegistries.ATTACHMENT_TYPES, Bagz.MODID); | ||
|
||
// TODO: Codecs | ||
public static final Codec<DyeColor> DYE_COLOR_CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Codec.INT.fieldOf("color").forGetter(DyeColor::getId) | ||
).apply(instance, DyeColor::byId)); | ||
public static final Codec<ItemStackHandler> ITEM_STACK_HANDLER_CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Codec.INT.fieldOf("size").forGetter(ItemStackHandler::getSlots), | ||
Codec.list(CompoundTag.CODEC).fieldOf("items").forGetter(handler -> { | ||
List<CompoundTag> tags = new ArrayList<>(); | ||
for (int i = 0; i < handler.getSlots(); i++) { | ||
ItemStack stack = handler.getStackInSlot(i); | ||
if (!stack.isEmpty()) { | ||
CompoundTag tag = new CompoundTag(); | ||
tag.putInt("Slot", i); | ||
stack.save(Minecraft.getInstance().level.registryAccess(), tag); | ||
tags.add(tag); | ||
} | ||
} | ||
return tags; | ||
}) | ||
).apply(instance, (size, items) -> { | ||
ItemStackHandler handler = new ItemStackHandler(size); | ||
for (CompoundTag tag : items) { | ||
int slot = tag.getInt("Slot"); | ||
if (slot >= 0 && slot < size) { | ||
ItemStack stack = ItemStack.parseOptional(Minecraft.getInstance().level.registryAccess(), tag); | ||
handler.setStackInSlot(slot, stack); | ||
} | ||
} | ||
return handler; | ||
})); | ||
|
||
public static final Codec<Map<DyeColor, ItemStackHandler>> BAG_INVENTORY_MAP_CODEC = | ||
Codec.unboundedMap(DYE_COLOR_CODEC, ITEM_STACK_HANDLER_CODEC); | ||
|
||
public static final Codec<Map<Integer, ItemStackHandler>> PERSONAL_BAG_INVENTORY_MAP_CODEC = | ||
Codec.unboundedMap(Codec.INT, ITEM_STACK_HANDLER_CODEC); | ||
|
||
|
||
public static final Supplier<AttachmentType<Map<DyeColor, ItemStackHandler>>> BAG_ATTACHMENT = ATTACHMENTS.register( | ||
"bag_attachment", () -> AttachmentType.<Map<DyeColor, ItemStackHandler>> builder(Collections::emptyMap) | ||
.serialize(BAG_INVENTORY_MAP_CODEC).copyOnDeath().build() | ||
); | ||
|
||
public static final Supplier<AttachmentType<Map<Integer, ItemStackHandler>>> PERSONAL_BAG_ATTACHMENT = ATTACHMENTS.register( | ||
"personal_bag_attachment", () -> AttachmentType.<Map<Integer, ItemStackHandler>> builder(Collections::emptyMap) | ||
.serialize(PERSONAL_BAG_INVENTORY_MAP_CODEC).copyOnDeath().build() | ||
); | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/portingdeadmods/bagz/networking/NetworkEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.portingdeadmods.bagz.networking; | ||
|
||
import com.portingdeadmods.bagz.Bagz; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.fml.common.EventBusSubscriber; | ||
import net.neoforged.neoforge.network.event.RegisterPayloadHandlersEvent; | ||
import net.neoforged.neoforge.network.registration.PayloadRegistrar; | ||
|
||
|
||
@EventBusSubscriber(modid = Bagz.MODID, bus = EventBusSubscriber.Bus.MOD) | ||
public class NetworkEvents { | ||
@SubscribeEvent | ||
public static void registerPayloads(final RegisterPayloadHandlersEvent event) { | ||
final PayloadRegistrar registrar = event.registrar(Bagz.MODID); | ||
registrar.playToClient( | ||
SyncBagzPayload.TYPE, | ||
SyncBagzPayload.STREAM_CODEC, | ||
SyncBagzPayload::syncBagzAction | ||
); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/portingdeadmods/bagz/networking/SyncBagzPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.portingdeadmods.bagz.networking; | ||
|
||
import com.portingdeadmods.bagz.Bagz; | ||
import com.portingdeadmods.bagz.api.BagzCapabilities; | ||
import net.minecraft.core.RegistryAccess; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.RegistryFriendlyByteBuf; | ||
import net.minecraft.network.codec.ByteBufCodecs; | ||
import net.minecraft.network.codec.StreamCodec; | ||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.level.Level; | ||
import net.neoforged.neoforge.network.handling.IPayloadContext; | ||
|
||
public record SyncBagzPayload (CompoundTag nbt) implements CustomPacketPayload { | ||
public static final Type<SyncBagzPayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath(Bagz.MODID, "sync_bagz_payload")); | ||
|
||
public static final StreamCodec<RegistryFriendlyByteBuf, SyncBagzPayload> STREAM_CODEC = StreamCodec.composite( | ||
ByteBufCodecs.COMPOUND_TAG, | ||
SyncBagzPayload::nbt, | ||
SyncBagzPayload::new | ||
); | ||
|
||
@Override | ||
public Type<? extends CustomPacketPayload> type() { | ||
return TYPE; | ||
} | ||
|
||
public static void syncBagzAction(SyncBagzPayload payload, IPayloadContext context) { | ||
context.enqueueWork(() -> { | ||
Player player = context.player(); | ||
Level level = player.level(); | ||
RegistryAccess registryAccess = level.registryAccess(); | ||
|
||
if (player.getCapability(BagzCapabilities.BAG_CAPABILITY) != null) { | ||
player.getCapability(BagzCapabilities.BAG_CAPABILITY).deserializeNBT(registryAccess, payload.nbt); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters