@Override public IMessage onMessage(final MessageCapability message, MessageContext ctx) { IThreadListener mainThread = Minecraft.getMinecraft(); mainThread.addScheduledTask(new Runnable() { @Override public void run() { Minecraft mc = Minecraft.getMinecraft(); EntityPlayerSP player = mc.player; Object capability = player.getCapability(message.capabilityType.getCapability(), null); if(capability != null && capability instanceof INBTSerializable) ((INBTSerializable) capability).deserializeNBT(message.capabilityNBT); //Refresh GuiQuest if open if(message.capabilityType == EnumCapability.QUEST && mc.currentScreen != null && mc.currentScreen instanceof GuiQuest) ((GuiQuest) mc.currentScreen).updateQuest(null); //Refresh GuiQuestList if open if(mc.currentScreen != null && mc.currentScreen instanceof GuiQuestList) ((GuiQuestList) mc.currentScreen).refreshButtons(); } }); return null; }
/** Creates and returns a primitive NBT tag from a value. * If the value already is an NBT tag, it is returned instead. */ public static NBTBase createTag(Object value) { if (value == null) throw new IllegalArgumentException("value is null"); if (value instanceof NBTBase) return (NBTBase)value; if (value instanceof INBTSerializable) return ((INBTSerializable<?>)value).serializeNBT(); if (value instanceof Collection) return ((Collection<?>)value).stream() .map(NbtUtils::createTag).collect(toList()); if (value instanceof Byte) return new NBTTagByte((Byte)value); if (value instanceof Short) return new NBTTagShort((Short)value); if (value instanceof Integer) return new NBTTagInt((Integer)value); if (value instanceof Long) return new NBTTagLong((Long)value); if (value instanceof Float) return new NBTTagFloat((Float)value); if (value instanceof Double) return new NBTTagDouble((Double)value); if (value instanceof String) return new NBTTagString((String)value); if (value instanceof byte[]) return new NBTTagByteArray((byte[])value); if (value instanceof int[]) return new NBTTagIntArray((int[])value); throw new IllegalArgumentException("Can't create an NBT tag of value: " + value); }
@Nullable @Override public NBTBase writeNBT(Capability<T> capability, T instance, EnumFacing side) { if(instance instanceof INBTSerializable) { return ((INBTSerializable)instance).serializeNBT(); } else { throw new IllegalArgumentException("Can not serialize to an instance that isn't a INBTSerializable implementation"); } }
@Override public void readNBT(Capability<T> capability, T instance, EnumFacing side, NBTBase nbt) { if(instance instanceof INBTSerializable) { ((INBTSerializable)instance).deserializeNBT(nbt); } else { throw new IllegalArgumentException("Can not deserialize to an instance that isn't a INBTSerializable implementation"); } }
@SuppressWarnings("unchecked") public CapabilityDispatcher(Map<ResourceLocation, ICapabilityProvider> list, ICapabilityProvider parent) { List<ICapabilityProvider> lstCaps = Lists.newArrayList(); List<INBTSerializable<NBTBase>> lstWriters = Lists.newArrayList(); List<String> lstNames = Lists.newArrayList(); if (parent != null) // Parents go first! { lstCaps.add(parent); if (parent instanceof INBTSerializable) { lstWriters.add((INBTSerializable<NBTBase>)parent); lstNames.add("Parent"); } } for (Map.Entry<ResourceLocation, ICapabilityProvider> entry : list.entrySet()) { ICapabilityProvider prov = entry.getValue(); lstCaps.add(prov); if (prov instanceof INBTSerializable) { lstWriters.add((INBTSerializable<NBTBase>)prov); lstNames.add(entry.getKey().toString()); } } caps = lstCaps.toArray(new ICapabilityProvider[lstCaps.size()]); writers = lstWriters.toArray(new INBTSerializable[lstWriters.size()]); names = lstNames.toArray(new String[lstNames.size()]); }
/** Returns the specified NBT serializable value * instance deserialized from the specified NBT tag. */ public static <N extends NBTBase, T extends INBTSerializable<N>> T getTagValue(N tag, T value) { if (tag == null) throw new IllegalArgumentException("tag is null"); if (value == null) throw new IllegalArgumentException("value is null"); value.deserializeNBT(tag); return value; }
/** Returns a list of NBT serializable values instantiated * using the value supplier from the specified NBT list. */ @SuppressWarnings("unchecked") public static <N extends NBTBase, T extends INBTSerializable<N>> List<T> getTagList( NBTTagList list, Supplier<T> valueSupplier) { return stream(list) .map(tag -> getTagValue((N)tag, valueSupplier.get())) .collect(Collectors.toList()); }
@Override public NBTBase writeNBT(Capability<T> capability, T instance, EnumFacing side) { return instance instanceof INBTSerializable ? ((INBTSerializable) instance).serializeNBT() : null; }
@Override public void readNBT(Capability<T> capability, T instance, EnumFacing side, NBTBase nbt) { if (instance instanceof INBTSerializable) ((INBTSerializable) instance).deserializeNBT(nbt); }