package portables.common.items; import net.minecraft.block.Block; import net.minecraft.entity.Entity; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.ChatComponentText; import net.minecraft.world.World; import portables.common.core.SimplePortables; import portables.common.util.Util; import java.util.List; public class PortableBlock extends Item { public static int durability; public PortableBlock() { super(); setUnlocalizedName("portableBlock"); setTextureName("simpleportables:remoteBlock"); setCreativeTab(SimplePortables.creativeTab); setMaxStackSize(1); setFull3D(); setMaxDamage(durability); } @Override public boolean onItemUse(ItemStack item, EntityPlayer player, World world, int x, int y, int z, int side, float hitX, float hitY, float hitZ) { if (player.isSneaking()) { if (world.getTileEntity(x, y, z) != null || world.getBlock(x, y, z) == Blocks.lever || world.getBlock(x, y, z) == Blocks.trapdoor || world.getBlock(x, y, z) == Blocks.fence_gate || world.getBlock(x, y, z) == Blocks.stone_button || world.getBlock(x, y, z) == Blocks.wooden_button || world.getBlock(x, y, z) == Blocks.jukebox || world.getBlock(x, y, z) == Blocks.unpowered_comparator || world.getBlock(x, y, z) == Blocks.unpowered_repeater || world.getBlock(x, y, z) == Blocks.powered_comparator || world.getBlock(x, y, z) == Blocks.powered_repeater || world.getBlock(x, y, z) == Blocks.wooden_door) { if (item.stackTagCompound.getCompoundTag("info").hasKey("locked")) { if (item.stackTagCompound.getCompoundTag("info").getString("locked").equals(player.getGameProfile().getName())) { linkBlock(world, item, player, x, y, z, side, hitZ, hitZ, hitZ); return true; } else { if (world.isRemote) { player.addChatMessage(new ChatComponentText(("This Remote Block is owned by: " + item.stackTagCompound.getCompoundTag("info").getString("locked")))); return true; } } } else { linkBlock(world, item, player, x, y, z, side, hitZ, hitZ, hitZ); return true; } } } return false; } @Override public ItemStack onItemRightClick(ItemStack item, World world, EntityPlayer player) { if (!player.isSneaking()) { if (!world.isRemote) { if (item.stackTagCompound.getCompoundTag("info").hasKey("locked")) { if (item.stackTagCompound.getCompoundTag("info").getString("locked").equals(player.getGameProfile().getName())) { Util.handleRemoteGUI(item, player, world); } else { player.addChatMessage(new ChatComponentText("This Remote Block is owned by: " + item.stackTagCompound.getCompoundTag("info").getString("locked"))); } } else { Util.handleRemoteGUI(item, player, world); } } } else { if (item.stackTagCompound.getCompoundTag("info").hasKey("locked")) { if (item.stackTagCompound.getCompoundTag("info").getString("locked").equals(player.getGameProfile().getName())) { player.openGui(SimplePortables.instance, 1, world, 0, 0, 0); } else { if (world.isRemote) { player.addChatMessage(new ChatComponentText("This Remote Block is owned by: " + item.stackTagCompound.getCompoundTag("info").getString("locked"))); } } } else { player.openGui(SimplePortables.instance, 1, world, 0, 0, 0); } } return item; } @Override public boolean hasEffect(ItemStack item) { Util.updateNBT(item); if (item.stackTagCompound.getCompoundTag("info") != null) { if (item.stackTagCompound.getCompoundTag("info").getBoolean("linked")) { return true; } else { return false; } } return false; } @Override public void onUpdate(ItemStack item, World world, Entity entity, int par4, boolean par5) { if (item != null && item.stackTagCompound != null && item.stackTagCompound.getCompoundTag("info") != null && item.stackTagCompound.getCompoundTag("info").getBoolean("linked")) { if (Block.getIdFromBlock((world.getBlock(item.stackTagCompound.getCompoundTag("info").getInteger("tileX"), item.stackTagCompound.getCompoundTag("info").getInteger("tileY"), item.stackTagCompound.getCompoundTag("info").getInteger("tileZ")))) != item.stackTagCompound.getCompoundTag("info").getInteger("blockID")) { item.stackTagCompound.getCompoundTag("info").setBoolean("linked", false); item.stackTagCompound.getCompoundTag("info").setString("type", ""); item.stackTagCompound.getCompoundTag("info").setInteger("tileX", 0); item.stackTagCompound.getCompoundTag("info").setInteger("tileY", 0); item.stackTagCompound.getCompoundTag("info").setInteger("tileZ", 0); } } } @Override public boolean getIsRepairable(ItemStack par1ItemStack, ItemStack par2ItemStack) { return Block.getIdFromBlock(Blocks.gold_block) == Item.getIdFromItem(par2ItemStack.getItem()) ? true : super.getIsRepairable(par1ItemStack, par2ItemStack); } @SuppressWarnings({ "unchecked", "rawtypes" }) @Override public void addInformation(ItemStack item, EntityPlayer player, List list, boolean par4) { Util.updateNBT(item); if (item.stackTagCompound.getCompoundTag("info").hasKey("locked")) { list.add("Locked to: " + item.stackTagCompound.getCompoundTag("info").getString("locked")); } } public void linkBlock(World world, ItemStack item, EntityPlayer player, int x, int y, int z, int side, float hitX, float hitY, float hitZ) { item.stackTagCompound.getCompoundTag("info").setInteger("blockID", Block.getIdFromBlock(world.getBlock(x, y, z))); item.stackTagCompound.getCompoundTag("info").setString("blockName", world.getBlock(x, y, z).getLocalizedName()); item.stackTagCompound.getCompoundTag("info").setInteger("tileX", x); item.stackTagCompound.getCompoundTag("info").setInteger("tileY", y); item.stackTagCompound.getCompoundTag("info").setInteger("tileZ", z); item.stackTagCompound.getCompoundTag("info").setInteger("side", side); item.stackTagCompound.getCompoundTag("info").setFloat("hitX", hitX); item.stackTagCompound.getCompoundTag("info").setFloat("hitY", hitY); item.stackTagCompound.getCompoundTag("info").setFloat("hitZ", hitZ); item.stackTagCompound.getCompoundTag("info").setBoolean("linked", true); for (int l = 0; l < 50; ++l) { double d1 = (double) ((float) y + world.rand.nextFloat()); double d2 = 10.0D; double d3 = 10.0D; double d4 = 10.0D; int i1 = world.rand.nextInt(2) * 2 - 1; int j1 = world.rand.nextInt(2) * 2 - 1; d2 = ((double) world.rand.nextFloat() - 0.5D) * 0.125D; d3 = ((double) world.rand.nextFloat() - 0.5D) * 0.125D; d4 = ((double) world.rand.nextFloat() - 0.5D) * 0.125D; double d5 = (double) z + 0.5D + 0.25D * (double) j1; d4 = (double) (world.rand.nextFloat() * 1.0F * (float) j1); double d6 = (double) x + 0.5D + 0.25D * (double) i1; d2 = (double) (world.rand.nextFloat() * 1.0F * (float) i1); world.spawnParticle("portal", d6, d1, d5, d2, d3, d4); } world.playSoundAtEntity(player, "simpleportables:Magic", 1, 1); item.damageItem(1, player); } }