Files
CustomNPC-Plus/src/main/java/noppes/npcs/items/ItemNpcTool.java
T
2025-07-23 14:22:52 -04:00

206 lines
6.8 KiB
Java

package noppes.npcs.items;
import cpw.mods.fml.common.registry.GameRegistry;
import kamkeel.npcs.network.PacketClient;
import kamkeel.npcs.network.packets.player.item.GuiMagicBookPacket;
import kamkeel.npcs.network.packets.player.item.GuiPaintbrushPacket;
import kamkeel.npcs.network.packets.request.item.ColorBrushPacket;
import kamkeel.npcs.network.packets.request.item.ColorSetPacket;
import kamkeel.npcs.network.packets.request.item.HammerPacket;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import noppes.npcs.CustomItems;
import noppes.npcs.CustomNpcs;
import noppes.npcs.CustomNpcsPermissions;
import noppes.npcs.blocks.BlockBanner;
import noppes.npcs.blocks.BlockChair;
import noppes.npcs.blocks.BlockTallLamp;
import noppes.npcs.blocks.BlockWallBanner;
import noppes.npcs.blocks.tiles.TileBanner;
import noppes.npcs.blocks.tiles.TileChair;
import noppes.npcs.blocks.tiles.TileColorable;
import noppes.npcs.constants.EnumGuiType;
import java.util.List;
public class ItemNpcTool extends Item {
// Define our tool types based on metadata:
// meta 0: Hammer, meta 1: Paintbrush, meta 2: Wrench
public static final String[] toolTypes = new String[]{"hammer", "paintbrush", "magic_book"};
public static String BRUSH_COLOR_TAG = "BrushColor";
public ItemNpcTool() {
super();
maxStackSize = 1;
setHasSubtypes(true);
setMaxDamage(0);
setCreativeTab(CustomItems.tab);
CustomNpcs.proxy.registerItem(this);
}
@Override
public String getUnlocalizedName(ItemStack stack) {
int meta = stack.getItemDamage();
if (meta < 0 || meta >= toolTypes.length) {
meta = 0;
}
return super.getUnlocalizedName() + "." + toolTypes[meta];
}
@Override
public void getSubItems(Item item, CreativeTabs tab, List subItems) {
for (int i = 0; i < toolTypes.length; i++) {
subItems.add(new ItemStack(item, 1, i));
}
}
@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
if (!world.isRemote)
return stack;
if (isPaintbrush(stack) && player.isSneaking()) {
if (CustomNpcsPermissions.hasPermission(player, CustomNpcsPermissions.PAINTBRUSH_GUI)) {
PacketClient.sendClient(new GuiPaintbrushPacket(EnumGuiType.Paintbrush.ordinal(), 0, 0, 0));
}
return stack;
}
if (isMagicBook(stack)) {
PacketClient.sendClient(new GuiMagicBookPacket(EnumGuiType.MagicBook.ordinal(), 0, 0, 0));
return stack;
}
return stack;
}
@Override
public boolean onItemUseFirst(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) {
if (!world.isRemote)
return false;
if (isPaintbrush(stack)) {
Block block = world.getBlock(x, y, z);
if (block instanceof BlockTallLamp || block instanceof BlockBanner) {
int meta = world.getBlockMetadata(x, y, z);
if (meta >= 7)
y--;
}
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof TileColorable) {
PacketClient.sendClient(new ColorSetPacket(x, y, z));
return true;
}
} else if (isHammer(stack)) {
Block block = player.worldObj.getBlock(x, y, z);
if (block instanceof BlockBanner || block instanceof BlockWallBanner) {
int meta = world.getBlockMetadata(x, y, z);
if (meta >= 7)
y--;
TileEntity tile = player.worldObj.getTileEntity(x, y, z);
if (tile instanceof TileBanner) {
PacketClient.sendClient(new HammerPacket(x, y, z));
return true;
}
} else if (block instanceof BlockChair) {
TileEntity tile = player.worldObj.getTileEntity(x, y, z);
if (tile instanceof TileChair) {
PacketClient.sendClient(new HammerPacket(x, y, z));
return true;
}
}
}
return false;
}
public boolean onBlockStartBreak(ItemStack itemstack, int x, int y, int z, EntityPlayer player) {
if (!player.worldObj.isRemote)
return true;
if (isPaintbrush(itemstack)) {
Block block = player.worldObj.getBlock(x, y, z);
if (block instanceof BlockTallLamp || block instanceof BlockBanner) {
int meta = player.worldObj.getBlockMetadata(x, y, z);
if (meta >= 7)
y--;
}
TileEntity tile = player.worldObj.getTileEntity(x, y, z);
if (tile instanceof TileColorable) {
int color = ((TileColorable) tile).color;
PacketClient.sendClient(new ColorBrushPacket(color));
}
}
return true;
}
public static boolean isPaintbrush(ItemStack itemStack) {
if (itemStack == null)
return false;
if (itemStack.getItem() == null)
return false;
if (!(itemStack.getItem() instanceof ItemNpcTool))
return false;
return itemStack.getItemDamage() == 1;
}
public static boolean isHammer(ItemStack itemStack) {
if (itemStack == null)
return false;
if (itemStack.getItem() == null)
return false;
if (!(itemStack.getItem() instanceof ItemNpcTool))
return false;
return itemStack.getItemDamage() == 0;
}
public static boolean isMagicBook(ItemStack itemStack) {
if (itemStack == null)
return false;
if (itemStack.getItem() == null)
return false;
if (!(itemStack.getItem() instanceof ItemNpcTool))
return false;
return itemStack.getItemDamage() == 2;
}
@Override
public Item setUnlocalizedName(String name) {
GameRegistry.registerItem(this, name);
return super.setUnlocalizedName(name);
}
public static int getColor(NBTTagCompound tagCompound) {
if (tagCompound == null || !tagCompound.hasKey(BRUSH_COLOR_TAG))
return 0xFFFFFF;
return tagCompound.getInteger(BRUSH_COLOR_TAG);
}
public static NBTTagCompound setColor(NBTTagCompound tagCompound, int color) {
if (tagCompound == null) {
tagCompound = new NBTTagCompound();
}
tagCompound.setInteger(BRUSH_COLOR_TAG, color);
return tagCompound;
}
}