mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-22 21:20:12 +00:00
1 parent
e67bde9e9e
commit
286777b824
8 files changed
+240
-4
No files matched your search
@@ -0,0 +1,58 @@
|
||||
/*
|
||||
* Minecraft Forge
|
||||
* Copyright (c) 2016.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation version 2.1
|
||||
* of the License.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
package net.minecraftforge.common.util;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
|
||||
public class PacketUtil
|
||||
{
|
||||
private PacketUtil() {}
|
||||
|
||||
/**
|
||||
* Most ItemStack serialization is Server to Client, and must go through PacketBuffer.writeItemStack which uses Item.getNBTShareTag.
|
||||
* One exception is items from the creative menu, which must be sent from Client to Server with their full NBT.
|
||||
* <br/>
|
||||
* This method matches PacketBuffer.writeItemStack but without the Item.getNBTShareTag patch.
|
||||
* It is compatible with PacketBuffer.readItemStack.
|
||||
*/
|
||||
public static void writeItemStackFromClientToServer(PacketBuffer buffer, ItemStack stack)
|
||||
{
|
||||
if (stack.isEmpty())
|
||||
{
|
||||
buffer.writeShort(-1);
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.writeShort(Item.getIdFromItem(stack.getItem()));
|
||||
buffer.writeByte(stack.getCount());
|
||||
buffer.writeShort(stack.getMetadata());
|
||||
NBTTagCompound nbttagcompound = null;
|
||||
|
||||
if (stack.getItem().isDamageable() || stack.getItem().getShareTag())
|
||||
{
|
||||
nbttagcompound = stack.getTagCompound();
|
||||
}
|
||||
|
||||
buffer.writeCompoundTag(nbttagcompound);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package net.minecraftforge.debug;
|
||||
|
||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
|
||||
import net.minecraft.creativetab.CreativeTabs;
|
||||
import net.minecraft.init.Items;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.crafting.CraftingManager;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.util.NonNullList;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.client.model.ModelLoader;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
@Mod(modid = NBTShareTagItemTest.MOD_ID, name = "NBTShareTag Item Test", version = "1.0.0", acceptableRemoteVersions = "*")
|
||||
public class NBTShareTagItemTest
|
||||
{
|
||||
public static final String MOD_ID = "nbtsharetagitemtest";
|
||||
private static final ResourceLocation itemName = new ResourceLocation(MOD_ID, "nbt_share_tag_item");
|
||||
|
||||
@Mod.EventHandler
|
||||
public void preInit(FMLPreInitializationEvent event)
|
||||
{
|
||||
ShareTagItem item = new ShareTagItem();
|
||||
item.setRegistryName(itemName);
|
||||
GameRegistry.register(item);
|
||||
|
||||
if (event.getSide() == Side.CLIENT)
|
||||
{
|
||||
ModelLoader.setCustomModelResourceLocation(item, 0, new ModelResourceLocation(itemName, "inventory"));
|
||||
}
|
||||
|
||||
ItemStack crafted = new ItemStack(item);
|
||||
NBTTagCompound craftedNBT = new NBTTagCompound();
|
||||
craftedNBT.setBoolean("crafted", true);
|
||||
crafted.setTagCompound(craftedNBT);
|
||||
CraftingManager.getInstance().addShapelessRecipe(crafted, new ItemStack(Items.STICK));
|
||||
}
|
||||
|
||||
public static class ShareTagItem extends Item
|
||||
{
|
||||
public ShareTagItem()
|
||||
{
|
||||
setCreativeTab(CreativeTabs.MISC);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public NBTTagCompound getNBTShareTag(ItemStack stack)
|
||||
{
|
||||
NBTTagCompound networkNBT = stack.getTagCompound();
|
||||
if (networkNBT == null)
|
||||
networkNBT = new NBTTagCompound();
|
||||
networkNBT.setBoolean("nbtShareTag", true);
|
||||
return networkNBT;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void getSubItems(Item itemIn, CreativeTabs tab, NonNullList<ItemStack> subItems)
|
||||
{
|
||||
ItemStack creativeMenuItem = new ItemStack(itemIn);
|
||||
NBTTagCompound creativeMenuNBT = new NBTTagCompound();
|
||||
creativeMenuNBT.setBoolean("creativeMenu", true);
|
||||
creativeMenuItem.setTagCompound(creativeMenuNBT);
|
||||
subItems.add(creativeMenuItem);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getItemStackDisplayName(ItemStack stack)
|
||||
{
|
||||
NBTTagCompound tagCompound = stack.getTagCompound();
|
||||
String name = "NBTShareTagItem: ";
|
||||
if (tagCompound == null)
|
||||
{
|
||||
name += "From /give. ";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (tagCompound.getBoolean("nbtShareTag"))
|
||||
name += "Sent from server. ";
|
||||
|
||||
if (tagCompound.getBoolean("creativeMenu"))
|
||||
name += "From creative menu. ";
|
||||
|
||||
if (tagCompound.getBoolean("crafted"))
|
||||
name += "From crafting. ";
|
||||
}
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"parent": "item/generated",
|
||||
"textures": {
|
||||
"layer0": "items/stick"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user