Advertisement
Guest User

Transfer Staff

a guest
Apr 7th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.43 KB | None | 0 0
  1. package yesodmod.general.item;
  2.  
  3.  
  4.  
  5. import java.util.List;
  6.  
  7. import net.minecraft.block.Block;
  8. import net.minecraft.client.Minecraft;
  9. import net.minecraft.creativetab.CreativeTabs;
  10. import net.minecraft.entity.player.EntityPlayer;
  11. import net.minecraft.item.Item;
  12. import net.minecraft.item.ItemStack;
  13. import net.minecraft.nbt.NBTTagCompound;
  14. import net.minecraft.nbt.NBTTagList;
  15. import net.minecraft.util.ChatComponentText;
  16. import net.minecraft.util.MovingObjectPosition;
  17. import net.minecraft.util.MovingObjectPosition.MovingObjectType;
  18. import net.minecraft.world.World;
  19.  
  20. public class TransferStaff extends Item {
  21.  
  22.     private final  int MAX_RANGE = 50;
  23.  
  24.  
  25.     public TransferStaff() {
  26.         super();
  27.         maxStackSize = 1;
  28.         setCreativeTab(CreativeTabs.tabTools);
  29.         setUnlocalizedName("TransferStaff");   
  30.         setTextureName("yesodmod:enderstaff");
  31.     }
  32.  
  33.     public void onCreated(ItemStack itemStack, World world, EntityPlayer player) {
  34.         itemStack.stackTagCompound = new NBTTagCompound();
  35.         int[] blockArray = new int[MAX_RANGE * MAX_RANGE * MAX_RANGE];
  36.         itemStack.stackTagCompound.setIntArray("blockArray", blockArray );
  37.         itemStack.stackTagCompound.setBoolean("blockStored", false);       
  38.     }
  39.  
  40.     public void addInformation(ItemStack itemStack, EntityPlayer player, List list, boolean par4)
  41.     {
  42.         if (itemStack.stackTagCompound != null)
  43.         {
  44.             boolean blockStored = itemStack.stackTagCompound.getBoolean("blockStored");
  45.             list.add("Stored Block : " + blockStored);
  46.         }
  47.     }
  48.  
  49.  
  50.     public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
  51.     {
  52.         if (!player.isSneaking())
  53.         {
  54.             MovingObjectPosition selectedBlock = Minecraft.getMinecraft().objectMouseOver;
  55.             if (selectedBlock != null && selectedBlock.typeOfHit == MovingObjectType.BLOCK)
  56.             {
  57.                 int xBlockPos = selectedBlock.blockX;
  58.                 int yBlockPos = selectedBlock.blockY;
  59.                 int zBlockPos = selectedBlock.blockZ;
  60.  
  61.                 if ( itemStack.stackTagCompound != null)
  62.                 {
  63.                     if (!hasSavedBlock(itemStack))
  64.                     {
  65.                         if (!hasFirstBlock(itemStack))
  66.                         {
  67.                             saveBlockPosition(itemStack, player, "FirstBlock", xBlockPos, yBlockPos, zBlockPos);
  68.  
  69.                             if (!world.isRemote)
  70.                                 player.addChatMessage(new ChatComponentText("First block Coordinate setted at: " + xBlockPos + ", " + yBlockPos + ", " + zBlockPos + ", "));
  71.                         }
  72.  
  73.                         else if (!hasLastBlock(itemStack))
  74.                         {
  75.                             saveBlockPosition(itemStack, player, "LastBlock", xBlockPos, yBlockPos, zBlockPos);
  76.  
  77.                             if (!world.isRemote)
  78.                                 player.addChatMessage(new ChatComponentText("Second block Coordinate set at: " + xBlockPos + ", " + yBlockPos + ", " + zBlockPos + ", "));
  79.                         }
  80.  
  81.                         else
  82.                         {
  83.                             itemStack.stackTagCompound.removeTag("FirstBlock");
  84.                             itemStack.stackTagCompound.removeTag("LastBlock");
  85.                             saveBlockPosition(itemStack, player, "FirstBlock", xBlockPos, yBlockPos, zBlockPos);
  86.  
  87.                             if (!world.isRemote)
  88.                                 player.addChatMessage(new ChatComponentText("First block Coordinate set at : " + xBlockPos + ", " + yBlockPos + ", " + zBlockPos + ", "));
  89.                         }
  90.  
  91.  
  92.                     }  
  93.                 }
  94.             }
  95.         }
  96.  
  97.         if (player.isSneaking())
  98.         {
  99.             if ( itemStack.stackTagCompound == null)
  100.             {
  101.                 itemStack.setTagCompound(new NBTTagCompound());
  102.             }
  103.             if (hasSavedBlock(itemStack) == false)
  104.             {
  105.                 storeBlock(itemStack, world);              
  106.                 if (!world.isRemote)
  107.                     player.addChatMessage(new ChatComponentText("Blocks Stored"));
  108.  
  109.             }
  110.  
  111.             else
  112.             {
  113.                 placeBlock(itemStack, world);
  114.                 itemStack.stackTagCompound.setBoolean("blockStored", false);
  115.                 itemStack.stackTagCompound.removeTag("FirstBlock");
  116.                 itemStack.stackTagCompound.removeTag("LastBlock");
  117.                 if (!world.isRemote)
  118.                     player.addChatMessage(new ChatComponentText("Blocks Placed"));
  119.             }
  120.         }
  121.         return itemStack;
  122.     }
  123.  
  124.  
  125.     private boolean hasSavedBlock(ItemStack itemStack)
  126.     {
  127.         return itemStack.stackTagCompound.getBoolean("blockStored");
  128.     }
  129.  
  130.     private boolean hasFirstBlock(ItemStack itemStack)
  131.     {
  132.         return itemStack.stackTagCompound != null && itemStack.stackTagCompound.hasKey("FirstBlock");
  133.     }
  134.  
  135.     private boolean hasLastBlock(ItemStack itemStack)
  136.     {
  137.         return itemStack.stackTagCompound != null && itemStack.stackTagCompound.hasKey("LastBlock");
  138.     }
  139.  
  140.     private void saveBlockPosition(ItemStack itemStack , EntityPlayer player , String block, int xBlockPos, int yBlockPos, int zBlockPos)
  141.     {
  142.         if (itemStack.stackTagCompound == null)
  143.         {
  144.             itemStack.setTagCompound(new NBTTagCompound());
  145.         }
  146.  
  147.         if (!itemStack.stackTagCompound.hasKey(block))
  148.         {
  149.             itemStack.stackTagCompound.setTag(block, new NBTTagList());
  150.         }
  151.  
  152.         NBTTagList positionTagList = (NBTTagList)itemStack.stackTagCompound.getTag(block);
  153.         NBTTagCompound positionTag = new NBTTagCompound();
  154.  
  155.         positionTag.setInteger("x" + block, xBlockPos);
  156.         positionTag.setInteger("y" + block, yBlockPos);
  157.         positionTag.setInteger("z" + block, zBlockPos);
  158.         positionTagList.appendTag(positionTag);
  159.  
  160.     }
  161.  
  162.     private void storeBlock(ItemStack itemStack, World world)
  163.     {   if (itemStack.stackTagCompound.hasKey("FirstBlock") && itemStack.stackTagCompound.hasKey("LastBlock"))
  164.     {
  165.         //acquisizione coordinate
  166.         NBTTagList firstPositionTagList = (NBTTagList)itemStack.stackTagCompound.getTag("FirstBlock");
  167.         NBTTagList lastPositionTagList = (NBTTagList)itemStack.stackTagCompound.getTag("LastBlock");
  168.         NBTTagCompound firstPositionTag = (NBTTagCompound) firstPositionTagList.getCompoundTagAt(0);
  169.         NBTTagCompound lastPositionTag = (NBTTagCompound) lastPositionTagList.getCompoundTagAt(0);
  170.  
  171.         int firstX = firstPositionTag.getInteger("xFirstBlock");
  172.         int firstY = firstPositionTag.getInteger("yFirstBlock");
  173.         int firstZ = firstPositionTag.getInteger("zFirstBlock");
  174.  
  175.         int lastX = lastPositionTag.getInteger("xLastBlock");
  176.         int lastY = lastPositionTag.getInteger("yLastBlock");
  177.         int lastZ = lastPositionTag.getInteger("zLastBlock");
  178.  
  179.         //controllo di quale รจ maggiore e quale minore
  180.         int diffX = lastX - firstX;
  181.         int diffY = lastY - firstY;
  182.         int diffZ = lastZ - firstZ;
  183.  
  184.         if (firstX > lastX)
  185.         {
  186.             diffX = firstX - lastX;
  187.             int tmp = firstX;
  188.             firstX = lastX;
  189.             lastX = tmp;
  190.         }
  191.  
  192.         if (firstY > lastY)
  193.         {
  194.             diffY = firstY - lastY;
  195.             int tmp = firstY;
  196.             firstY = lastY;
  197.             lastY = tmp;
  198.         }
  199.  
  200.         if (firstZ > lastZ)
  201.         {
  202.             diffZ = firstZ - lastZ;
  203.             int tmp = firstZ;
  204.             firstZ = lastZ;
  205.             lastZ = tmp;
  206.         }
  207.  
  208.         //acquisizione blocchi
  209.  
  210.         int [] arrayBlock = new int[(diffX + 1) *( diffY + 1) * (diffZ + 1)];
  211.  
  212.  
  213.         for (int i = firstZ; i <= lastZ; i++)
  214.         {
  215.             for (int j = firstY; j <= lastY ; j++)
  216.             {
  217.                 for (int k = firstX; k <= lastX ; k++)
  218.                 {  
  219.                     for (int l = 0 ; l < arrayBlock.length ; l++)
  220.                     {                      
  221.                         arrayBlock[l] = Block.getIdFromBlock(world.getBlock(k, j, i));
  222.                         world.setBlockToAir(k, j, i);
  223.                     }              
  224.                 }
  225.             }
  226.         }
  227.                
  228.         itemStack.stackTagCompound.setIntArray("blockArray", arrayBlock );
  229.         itemStack.stackTagCompound.setBoolean("blockStored", true);
  230.     }
  231.     }
  232.  
  233.     private void placeBlock(ItemStack itemStack, World world)
  234.     {
  235.         if (itemStack.stackTagCompound.getBoolean("blockStored"))
  236.         {
  237.             MovingObjectPosition selectedBlock = Minecraft.getMinecraft().objectMouseOver;
  238.             if (selectedBlock != null && selectedBlock.typeOfHit == MovingObjectType.BLOCK)
  239.             {
  240.                 int xBlockPos = selectedBlock.blockX;
  241.                 int yBlockPos = selectedBlock.blockY;
  242.                 int zBlockPos = selectedBlock.blockZ;
  243.             }
  244.            
  245.            
  246.         }
  247.        
  248.         itemStack.stackTagCompound.removeTag("blockArray");
  249.     }
  250. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement