11h - BlockState Inventory, for chests and future ShulkerBoxes.
This commit is contained in:
parent
db59c7b2a1
commit
77b50651dc
@ -82,6 +82,11 @@
|
|||||||
}
|
}
|
||||||
...
|
...
|
||||||
]
|
]
|
||||||
|
Inventory:
|
||||||
|
[
|
||||||
|
*ItemStack*
|
||||||
|
....
|
||||||
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,12 +2,14 @@ package com.massivecraft.massivecore.adapter;
|
|||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Map.Entry;
|
||||||
|
|
||||||
import org.bukkit.inventory.Inventory;
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.PlayerInventory;
|
import org.bukkit.inventory.PlayerInventory;
|
||||||
|
|
||||||
import com.massivecraft.massivecore.MassiveCore;
|
import com.massivecraft.massivecore.MassiveCore;
|
||||||
|
import com.massivecraft.massivecore.item.DataItemStack;
|
||||||
import com.massivecraft.massivecore.mixin.MixinInventory;
|
import com.massivecraft.massivecore.mixin.MixinInventory;
|
||||||
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
|
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
|
||||||
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
|
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
|
||||||
@ -84,6 +86,7 @@ public class AdapterInventory implements JsonDeserializer<Inventory>, JsonSerial
|
|||||||
// These variables are used in loops and repetitive logic.
|
// These variables are used in loops and repetitive logic.
|
||||||
ItemStack itemStack = null;
|
ItemStack itemStack = null;
|
||||||
JsonElement jsonItemStack = null;
|
JsonElement jsonItemStack = null;
|
||||||
|
String index = null;
|
||||||
|
|
||||||
// Every inventory has a content part.
|
// Every inventory has a content part.
|
||||||
ItemStack[] itemStacks = src.getContents();
|
ItemStack[] itemStacks = src.getContents();
|
||||||
@ -147,12 +150,11 @@ public class AdapterInventory implements JsonDeserializer<Inventory>, JsonSerial
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the content at the end since we like to have it at the bottom of return json.
|
// Add the content at the end since we like to have it at the bottom of return json.
|
||||||
for (int i = 0; i < itemStacks.length; i++)
|
for (Entry<Integer, DataItemStack> entry : DataItemStack.fromBukkitContents(itemStacks).entrySet())
|
||||||
{
|
{
|
||||||
itemStack = itemStacks[i];
|
index = String.valueOf(entry.getKey());
|
||||||
jsonItemStack = MassiveCore.gson.toJsonTree(itemStack, ItemStack.class);
|
jsonItemStack = MassiveCore.gson.toJsonTree(entry.getValue());
|
||||||
if (jsonItemStack == null) continue;
|
jsonInventory.add(index, jsonItemStack);
|
||||||
jsonInventory.add(String.valueOf(i), jsonItemStack);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return jsonInventory;
|
return jsonInventory;
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.massivecraft.massivecore.item;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
public class ConverterFromInventoryContents extends Converter<ItemStack[], Map<Integer, DataItemStack>>
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// INSTANCE & CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private static final ConverterFromInventoryContents i = new ConverterFromInventoryContents();
|
||||||
|
public static ConverterFromInventoryContents get() { return i; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// OVERRIDE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Integer, DataItemStack> convert(ItemStack[] x)
|
||||||
|
{
|
||||||
|
if (x == null) return null;
|
||||||
|
return DataItemStack.fromBukkitContents(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.massivecraft.massivecore.item;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
public class ConverterToInventoryContents extends Converter<Map<Integer, DataItemStack>, ItemStack[]>
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// INSTANCE & CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private static final ConverterToInventoryContents i = new ConverterToInventoryContents();
|
||||||
|
public static ConverterToInventoryContents get() { return i; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// OVERRIDE
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack[] convert(Map<Integer, DataItemStack> x)
|
||||||
|
{
|
||||||
|
if (x == null) return null;
|
||||||
|
return DataItemStack.toBukkitContents(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -19,6 +19,7 @@ import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
|
|||||||
import com.massivecraft.massivecore.command.editor.annotation.EditorMethods;
|
import com.massivecraft.massivecore.command.editor.annotation.EditorMethods;
|
||||||
import com.massivecraft.massivecore.command.editor.annotation.EditorType;
|
import com.massivecraft.massivecore.command.editor.annotation.EditorType;
|
||||||
import com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner;
|
import com.massivecraft.massivecore.command.editor.annotation.EditorTypeInner;
|
||||||
|
import com.massivecraft.massivecore.command.editor.annotation.EditorVisible;
|
||||||
import com.massivecraft.massivecore.command.type.TypeMaterialId;
|
import com.massivecraft.massivecore.command.type.TypeMaterialId;
|
||||||
import com.massivecraft.massivecore.command.type.convert.TypeConverterColor;
|
import com.massivecraft.massivecore.command.type.convert.TypeConverterColor;
|
||||||
import com.massivecraft.massivecore.command.type.convert.TypeConverterDyeColor;
|
import com.massivecraft.massivecore.command.type.convert.TypeConverterDyeColor;
|
||||||
@ -27,6 +28,7 @@ import com.massivecraft.massivecore.command.type.convert.TypeConverterItemFlag;
|
|||||||
import com.massivecraft.massivecore.command.type.primitive.TypeInteger;
|
import com.massivecraft.massivecore.command.type.primitive.TypeInteger;
|
||||||
import com.massivecraft.massivecore.command.type.primitive.TypeStringParsed;
|
import com.massivecraft.massivecore.command.type.primitive.TypeStringParsed;
|
||||||
import com.massivecraft.massivecore.comparator.ComparatorSmart;
|
import com.massivecraft.massivecore.comparator.ComparatorSmart;
|
||||||
|
import com.massivecraft.massivecore.util.InventoryUtil;
|
||||||
import com.massivecraft.massivecore.util.MUtil;
|
import com.massivecraft.massivecore.util.MUtil;
|
||||||
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
|
import com.massivecraft.massivecore.xlib.gson.annotations.SerializedName;
|
||||||
|
|
||||||
@ -78,6 +80,7 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
public static final transient Integer DEFAULT_BANNER_BASE = null;
|
public static final transient Integer DEFAULT_BANNER_BASE = null;
|
||||||
public static final transient List<DataBannerPattern> DEFAULT_BANNER_PATTERNS = Collections.emptyList();
|
public static final transient List<DataBannerPattern> DEFAULT_BANNER_PATTERNS = Collections.emptyList();
|
||||||
public static final transient String DEFAULT_POTION = "water";
|
public static final transient String DEFAULT_POTION = "water";
|
||||||
|
public static final transient Map<Integer, DataItemStack> DEFAULT_INVENTORY = Collections.emptyMap();
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// FIELDS > BASIC
|
// FIELDS > BASIC
|
||||||
@ -263,6 +266,16 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
public String getPotion() { return get(this.potion, DEFAULT_POTION); }
|
public String getPotion() { return get(this.potion, DEFAULT_POTION); }
|
||||||
public DataItemStack setPotion(String potion) { this.potion = set(potion, DEFAULT_POTION); return this; }
|
public DataItemStack setPotion(String potion) { this.potion = set(potion, DEFAULT_POTION); return this; }
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// FIELDS > INVENTORY
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// SINCE: 1.8
|
||||||
|
|
||||||
|
@EditorVisible(false)
|
||||||
|
private Map<Integer, DataItemStack> inventory = null;
|
||||||
|
public Map<Integer, DataItemStack> getInventory() { return get(this.inventory, DEFAULT_INVENTORY); }
|
||||||
|
public DataItemStack setInventory(Map<Integer, DataItemStack> inventory) { this.inventory = set(inventory, DEFAULT_INVENTORY); return this; }
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// CONSTRUCT
|
// CONSTRUCT
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
@ -378,6 +391,48 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<Integer, DataItemStack> fromBukkitContents(ItemStack[] contents)
|
||||||
|
{
|
||||||
|
// Catch NullEmpty
|
||||||
|
if (contents == null || contents.length == 0) return null;
|
||||||
|
|
||||||
|
// Create
|
||||||
|
Map<Integer, DataItemStack> ret = new MassiveMap<>();
|
||||||
|
|
||||||
|
// Fill
|
||||||
|
for (int i = 0; i < contents.length; i++)
|
||||||
|
{
|
||||||
|
ItemStack itemStack = contents[i];
|
||||||
|
if (InventoryUtil.isNothing(itemStack)) continue;
|
||||||
|
|
||||||
|
ret.put(i, DataItemStack.fromBukkit(itemStack));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ItemStack[] toBukkitContents(Map<Integer, DataItemStack> contents)
|
||||||
|
{
|
||||||
|
// Catch NullEmpty
|
||||||
|
if (contents == null || contents.isEmpty()) return null;
|
||||||
|
|
||||||
|
// Create
|
||||||
|
int max = Collections.max(contents.keySet());
|
||||||
|
ItemStack[] ret = new ItemStack[max+1];
|
||||||
|
|
||||||
|
// Fill
|
||||||
|
for (Entry<Integer, DataItemStack> entry: contents.entrySet())
|
||||||
|
{
|
||||||
|
int index = entry.getKey();
|
||||||
|
DataItemStack item = entry.getValue();
|
||||||
|
ret[index] = item.toBukkit();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
// UTILITY
|
// UTILITY
|
||||||
// -------------------------------------------- //
|
// -------------------------------------------- //
|
||||||
@ -425,7 +480,8 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
this.getFlags(), that.getFlags(),
|
this.getFlags(), that.getFlags(),
|
||||||
this.getBannerBase(), that.getBannerBase(),
|
this.getBannerBase(), that.getBannerBase(),
|
||||||
this.getBannerPatterns(), that.getBannerPatterns(),
|
this.getBannerPatterns(), that.getBannerPatterns(),
|
||||||
this.getPotion(), that.getPotion()
|
this.getPotion(), that.getPotion(),
|
||||||
|
this.getInventory(), that.getInventory()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -457,7 +513,8 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
this.getFlags(), that.getFlags(),
|
this.getFlags(), that.getFlags(),
|
||||||
this.getBannerBase(), that.getBannerBase(),
|
this.getBannerBase(), that.getBannerBase(),
|
||||||
this.getBannerPatterns(), that.getBannerPatterns(),
|
this.getBannerPatterns(), that.getBannerPatterns(),
|
||||||
this.getPotion(), that.getPotion()
|
this.getPotion(), that.getPotion(),
|
||||||
|
this.getInventory(), that.getInventory()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,7 +553,8 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
this.getFlags(), that.getFlags(),
|
this.getFlags(), that.getFlags(),
|
||||||
this.getBannerBase(), that.getBannerBase(),
|
this.getBannerBase(), that.getBannerBase(),
|
||||||
this.getBannerPatterns(), that.getBannerPatterns(),
|
this.getBannerPatterns(), that.getBannerPatterns(),
|
||||||
this.getPotion(), that.getPotion()
|
this.getPotion(), that.getPotion(),
|
||||||
|
this.getInventory(), that.getInventory()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -533,7 +591,8 @@ public class DataItemStack implements Comparable<DataItemStack>
|
|||||||
this.getFlags(),
|
this.getFlags(),
|
||||||
this.getBannerBase(),
|
this.getBannerBase(),
|
||||||
this.getBannerPatterns(),
|
this.getBannerPatterns(),
|
||||||
this.getPotion()
|
this.getPotion(),
|
||||||
|
this.getInventory()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +59,10 @@ public class WriterItemStackMeta extends WriterAbstractItemStackMetaMorph<Object
|
|||||||
WriterItemStackMetaShieldPatterns.class,
|
WriterItemStackMetaShieldPatterns.class,
|
||||||
|
|
||||||
// POTION
|
// POTION
|
||||||
WriterItemStackMetaPotion.class
|
WriterItemStackMetaPotion.class,
|
||||||
|
|
||||||
|
// INVENTORY
|
||||||
|
WriterItemStackMetaInventory.class
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,102 @@
|
|||||||
|
package com.massivecraft.massivecore.item;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.block.BlockState;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
|
import org.bukkit.inventory.InventoryHolder;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BlockStateMeta;
|
||||||
|
|
||||||
|
import com.massivecraft.massivecore.util.InventoryUtil;
|
||||||
|
|
||||||
|
// NOTE: We take ItemStack[] (inventory contents) instead of Inventories to accommodate the different sizes of inventories better.
|
||||||
|
// It is not possible to create an inventory with less then 9 slots, but hoppers or brewing stands only have 5.
|
||||||
|
// By using arrays, we avoid huge efforts to retain inventory order, because that happens relatively naturally.
|
||||||
|
public class WriterItemStackMetaInventory extends WriterAbstractItemStackMetaField<BlockStateMeta, Map<Integer, DataItemStack>, ItemStack[]>
|
||||||
|
{
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// INSTANCE & CONSTRUCT
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
private static final WriterItemStackMetaInventory i = new WriterItemStackMetaInventory();
|
||||||
|
public static WriterItemStackMetaInventory get() { return i; }
|
||||||
|
public WriterItemStackMetaInventory()
|
||||||
|
{
|
||||||
|
super(BlockStateMeta.class);
|
||||||
|
this.setMaterial(Material.CHEST);
|
||||||
|
this.setConverterTo(ConverterToInventoryContents.get());
|
||||||
|
this.setConverterFrom(ConverterFromInventoryContents.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
// -------------------------------------------- //
|
||||||
|
// ACCESS
|
||||||
|
// -------------------------------------------- //
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<Integer, DataItemStack> getA(DataItemStack ca, ItemStack d)
|
||||||
|
{
|
||||||
|
return ca.getInventory();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setA(DataItemStack ca, Map<Integer, DataItemStack> fa, ItemStack d)
|
||||||
|
{
|
||||||
|
ca.setInventory(fa);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack[] getB(BlockStateMeta cb, ItemStack d)
|
||||||
|
{
|
||||||
|
// Null
|
||||||
|
if (cb == null) return null;
|
||||||
|
|
||||||
|
// Creative
|
||||||
|
if (!cb.hasBlockState()) return null;
|
||||||
|
|
||||||
|
// Try
|
||||||
|
try
|
||||||
|
{
|
||||||
|
BlockState ret = cb.getBlockState();
|
||||||
|
if (!(ret instanceof InventoryHolder)) return null;
|
||||||
|
return ((InventoryHolder) ret).getInventory().getContents();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// Catch errors such as: throw new IllegalStateException("Missing blockState for " + material);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setB(BlockStateMeta blockStateMeta, ItemStack[] storedInventory, ItemStack d)
|
||||||
|
{
|
||||||
|
// Null
|
||||||
|
if (blockStateMeta == null) return;
|
||||||
|
if (storedInventory == null || storedInventory.length == 0) return;
|
||||||
|
|
||||||
|
// Try
|
||||||
|
BlockState ret;
|
||||||
|
Inventory inventory;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
ret = blockStateMeta.getBlockState();
|
||||||
|
if (!(ret instanceof InventoryHolder)) return;
|
||||||
|
inventory = ((InventoryHolder)ret).getInventory();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
// Catch errors such as: throw new IllegalStateException("Missing blockState for " + material);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply
|
||||||
|
InventoryUtil.setContentsAll(inventory, storedInventory);
|
||||||
|
|
||||||
|
// Set
|
||||||
|
blockStateMeta.setBlockState(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user