mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-28 15:36:13 +00:00
Extract namable stuff from inventory tile entity to new superclass
Want some namable containers that are not traditional mantle inventories Also introduced new writeSynced method to simplify writing clean NBT syncing
This commit is contained in:
1 parent
71c2ae2181
commit
a3430a3adf
3 files changed
+110
-57
No files matched your search
@@ -26,11 +26,12 @@ import slimeknights.mantle.util.ItemStackList;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
// Updated version of InventoryLogic in Mantle. Also contains a few bugfixes DOES NOT OVERRIDE createMenu
|
||||
public abstract class InventoryTileEntity extends MantleTileEntity implements IInventory, INamedContainerProvider, INameable {
|
||||
public abstract class InventoryTileEntity extends NamableTileEntity implements IInventory, INamedContainerProvider, INameable {
|
||||
private static final String TAG_INVENTORY_SIZE = "InventorySize";
|
||||
private static final String TAG_ITEMS = "Items";
|
||||
private static final String TAG_SLOT = "Slot";
|
||||
|
||||
private NonNullList<ItemStack> inventory;
|
||||
protected ITextComponent inventoryTitle;
|
||||
protected boolean hasCustomName;
|
||||
protected int stackSizeLimit;
|
||||
protected IItemHandlerModifiable itemHandler;
|
||||
protected LazyOptional<IItemHandlerModifiable> itemHandlerCap;
|
||||
@@ -46,10 +47,9 @@ public abstract class InventoryTileEntity extends MantleTileEntity implements II
|
||||
* @param name Localization String for the inventory title. Can be overridden through setCustomName
|
||||
*/
|
||||
public InventoryTileEntity(TileEntityType<?> tileEntityTypeIn, ITextComponent name, int inventorySize, int maxStackSize) {
|
||||
super(tileEntityTypeIn);
|
||||
super(tileEntityTypeIn, name);
|
||||
this.inventory = NonNullList.withSize(inventorySize, ItemStack.EMPTY);
|
||||
this.stackSizeLimit = maxStackSize;
|
||||
this.inventoryTitle = name;
|
||||
this.itemHandler = new InvWrapper(this);
|
||||
this.itemHandlerCap = LazyOptional.of(() -> this.itemHandler);
|
||||
}
|
||||
@@ -188,35 +188,6 @@ public abstract class InventoryTileEntity extends MantleTileEntity implements II
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITextComponent getName() {
|
||||
return this.inventoryTitle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCustomName() {
|
||||
return this.getCustomName() != null;
|
||||
}
|
||||
|
||||
public void setCustomName(ITextComponent customName) {
|
||||
this.inventoryTitle = customName;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ITextComponent getCustomName() {
|
||||
return this.inventoryTitle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITextComponent getDisplayName() {
|
||||
if (this.hasCustomName()) {
|
||||
return new StringTextComponent(this.getName().getString());
|
||||
}
|
||||
|
||||
return new TranslationTextComponent(this.getName().getString());
|
||||
}
|
||||
|
||||
/* Supporting methods */
|
||||
@Override
|
||||
public boolean isUsableByPlayer(PlayerEntity entityplayer) {
|
||||
@@ -238,37 +209,25 @@ public abstract class InventoryTileEntity extends MantleTileEntity implements II
|
||||
@Override
|
||||
public void read(BlockState blockState, CompoundNBT tags) {
|
||||
super.read(blockState, tags);
|
||||
this.resizeInternal(tags.getInt("InventorySize"));
|
||||
this.resizeInternal(tags.getInt(TAG_INVENTORY_SIZE));
|
||||
|
||||
this.readInventoryFromNBT(tags);
|
||||
}
|
||||
|
||||
if (tags.contains("CustomName", NBT.TAG_STRING)) {
|
||||
this.inventoryTitle = ITextComponent.Serializer.getComponentFromJson(tags.getString("CustomName"));
|
||||
}
|
||||
@Override
|
||||
public void writeSynced(CompoundNBT tags) {
|
||||
super.writeSynced(tags);
|
||||
// only sync the size to the client by default
|
||||
tags.putInt(TAG_INVENTORY_SIZE, this.inventory.size());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundNBT write(CompoundNBT tags) {
|
||||
super.write(tags);
|
||||
tags.putInt("InventorySize", this.inventory.size());
|
||||
this.writeInventoryToNBT(tags);
|
||||
if (this.hasCustomName()) {
|
||||
tags.putString("CustomName", ITextComponent.Serializer.toJson(this.inventoryTitle));
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundNBT getUpdateTag() {
|
||||
// include inventory size and custom name in the data synced to client
|
||||
CompoundNBT nbt = super.getUpdateTag();
|
||||
nbt.putInt("InventorySize", this.getSizeInventory());
|
||||
if (this.hasCustomName()) {
|
||||
nbt.putString("CustomName", ITextComponent.Serializer.toJson(this.inventoryTitle));
|
||||
}
|
||||
return nbt;
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes the contents of the inventory to the tag
|
||||
*/
|
||||
@@ -279,26 +238,26 @@ public abstract class InventoryTileEntity extends MantleTileEntity implements II
|
||||
for (int i = 0; i < inventory.getSizeInventory(); i++) {
|
||||
if (!inventory.getStackInSlot(i).isEmpty()) {
|
||||
CompoundNBT itemTag = new CompoundNBT();
|
||||
itemTag.putByte("Slot", (byte) i);
|
||||
itemTag.putByte(TAG_SLOT, (byte) i);
|
||||
inventory.getStackInSlot(i).write(itemTag);
|
||||
nbttaglist.add(itemTag);
|
||||
}
|
||||
}
|
||||
|
||||
tag.put("Items", nbttaglist);
|
||||
tag.put(TAG_ITEMS, nbttaglist);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a an inventory from the tag. Overwrites current content
|
||||
*/
|
||||
public void readInventoryFromNBT(CompoundNBT tag) {
|
||||
ListNBT nbttaglist = tag.getList("Items", NBT.TAG_COMPOUND);
|
||||
ListNBT nbttaglist = tag.getList(TAG_ITEMS, NBT.TAG_COMPOUND);
|
||||
|
||||
int limit = this.getInventoryStackLimit();
|
||||
ItemStack stack;
|
||||
for (int i = 0; i < nbttaglist.size(); ++i) {
|
||||
CompoundNBT itemTag = nbttaglist.getCompound(i);
|
||||
int slot = itemTag.getByte("Slot") & 255;
|
||||
int slot = itemTag.getByte(TAG_SLOT) & 255;
|
||||
if (slot < this.inventory.size()) {
|
||||
stack = ItemStack.read(itemTag);
|
||||
if (!stack.isEmpty() && stack.getCount() > limit) {
|
||||
@@ -310,6 +269,7 @@ public abstract class InventoryTileEntity extends MantleTileEntity implements II
|
||||
}
|
||||
|
||||
/* Default implementations of hardly used methods */
|
||||
@Deprecated
|
||||
public ItemStack getStackInSlotOnClosing(int slot) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package slimeknights.mantle.tileentity;
|
||||
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.tileentity.TileEntityType;
|
||||
|
||||
@@ -22,4 +23,26 @@ public class MantleTileEntity extends TileEntity {
|
||||
world.markChunkDirty(pos, this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Syncing */
|
||||
|
||||
/**
|
||||
* Write to NBT that is synced to the client in {@link #getUpdateTag()} and in {@link #write(CompoundNBT)}
|
||||
* @param nbt NBT
|
||||
*/
|
||||
protected void writeSynced(CompoundNBT nbt) {}
|
||||
|
||||
@Override
|
||||
public CompoundNBT getUpdateTag() {
|
||||
CompoundNBT nbt = super.getUpdateTag();
|
||||
writeSynced(nbt);
|
||||
return nbt;
|
||||
}
|
||||
|
||||
public CompoundNBT write(CompoundNBT nbt) {
|
||||
nbt = super.write(nbt);
|
||||
writeSynced(nbt);
|
||||
return nbt;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package slimeknights.mantle.tileentity;
|
||||
|
||||
import net.minecraft.block.BlockState;
|
||||
import net.minecraft.entity.player.PlayerEntity;
|
||||
import net.minecraft.entity.player.PlayerInventory;
|
||||
import net.minecraft.inventory.container.Container;
|
||||
import net.minecraft.inventory.container.INamedContainerProvider;
|
||||
import net.minecraft.nbt.CompoundNBT;
|
||||
import net.minecraft.tileentity.TileEntityType;
|
||||
import net.minecraft.util.INameable;
|
||||
import net.minecraft.util.text.ITextComponent;
|
||||
import net.minecraftforge.common.util.Constants.NBT;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* Extension of tile entity to make it namable
|
||||
*/
|
||||
public abstract class NamableTileEntity extends MantleTileEntity implements INamedContainerProvider, INameable {
|
||||
private static final String TAG_CUSTOM_NAME = "CustomName";
|
||||
|
||||
/** Default title for this tile entity */
|
||||
private final ITextComponent defaultTitle;
|
||||
/** Title set to this tile entity */
|
||||
private ITextComponent customTitle;
|
||||
|
||||
public NamableTileEntity(TileEntityType<?> tileEntityTypeIn, ITextComponent defaultTitle) {
|
||||
super(tileEntityTypeIn);
|
||||
this.defaultTitle = defaultTitle;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITextComponent getName() {
|
||||
return customTitle != null ? customTitle : defaultTitle;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public ITextComponent getCustomName() {
|
||||
return customTitle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the name for this tile entity
|
||||
* @param customName New custom name
|
||||
*/
|
||||
public void setCustomName(ITextComponent customName) {
|
||||
this.customTitle = customName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ITextComponent getDisplayName() {
|
||||
return getName();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(BlockState blockState, CompoundNBT tags) {
|
||||
super.read(blockState, tags);
|
||||
if (tags.contains(TAG_CUSTOM_NAME, NBT.TAG_STRING)) {
|
||||
this.customTitle = ITextComponent.Serializer.getComponentFromJson(tags.getString(TAG_CUSTOM_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeSynced(CompoundNBT tags) {
|
||||
super.writeSynced(tags);
|
||||
if (this.hasCustomName()) {
|
||||
tags.putString(TAG_CUSTOM_NAME, ITextComponent.Serializer.toJson(this.customTitle));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user