Advertisement
Guest User

ItemVacpack.java

a guest
Dec 7th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.97 KB | None | 0 0
  1. package com.basset.slimerancher.items;
  2.  
  3. import java.util.List;
  4.  
  5. import com.basset.slimerancher.SlimeRancher;
  6. import com.basset.slimerancher.entities.VacpackShotItem;
  7. import com.basset.slimerancher.gui.VacpackOverlay;
  8. import com.basset.slimerancher.inventories.VacpackInventory;
  9.  
  10. import net.minecraft.block.state.IBlockState;
  11. import net.minecraft.client.renderer.block.model.ModelResourceLocation;
  12. import net.minecraft.creativetab.CreativeTabs;
  13. import net.minecraft.entity.Entity;
  14. import net.minecraft.entity.EntityLivingBase;
  15. import net.minecraft.entity.item.EntityItem;
  16. import net.minecraft.entity.player.EntityPlayer;
  17. import net.minecraft.init.Items;
  18. import net.minecraft.item.ItemStack;
  19. import net.minecraft.item.ItemSword;
  20. import net.minecraft.nbt.NBTTagCompound;
  21. import net.minecraft.nbt.NBTTagList;
  22. import net.minecraft.util.ActionResult;
  23. import net.minecraft.util.EnumActionResult;
  24. import net.minecraft.util.EnumHand;
  25. import net.minecraft.util.math.AxisAlignedBB;
  26. import net.minecraft.util.math.BlockPos;
  27. import net.minecraft.world.World;
  28. import net.minecraft.world.WorldServer;
  29. import net.minecraftforge.client.model.ModelLoader;
  30. import net.minecraftforge.fluids.FluidStack;
  31. import net.minecraftforge.fluids.capability.IFluidHandler;
  32. import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
  33. import net.minecraftforge.fml.common.gameevent.TickEvent;
  34. import net.minecraftforge.items.IItemHandler;
  35. import net.minecraftforge.items.ItemStackHandler;
  36.  
  37. public class ItemVacpack extends SlimeRancherItem {
  38.    
  39.     public static int range = 5;
  40.     public static double speed = 0.02;
  41.  
  42.     private VacpackInventory inventory;
  43.     private boolean suck = false;
  44.    
  45.     public ItemVacpack() {
  46.         super("vacpack", CreativeTabs.TOOLS, 1);
  47.     }
  48.    
  49.     @Override
  50.     public void onCreated(ItemStack stack, World world, EntityPlayer player) {
  51.         // initialize a new inventory for the itemstack's item
  52.         if (((ItemVacpack) stack.getItem()).getInventory() == null) {
  53.             ((ItemVacpack) stack.getItem()).setInventory(new VacpackInventory(stack));
  54.         }
  55.     }
  56.    
  57.     public VacpackInventory getInventory() {
  58.         return this.inventory;
  59.     }
  60.    
  61.     public boolean getSuck() {
  62.         return this.suck;
  63.     }
  64.    
  65.     public void toggleSuck() {
  66.         this.suck = ! this.suck;
  67.     }
  68.    
  69.     private void setInventory(VacpackInventory inv) {
  70.         this.inventory = inv;
  71.     }
  72.    
  73.     @Override
  74.     public int getMaxItemUseDuration(ItemStack stack) {
  75.         return 1; // return any value greater than zero
  76.     }
  77.    
  78.    
  79.  
  80.     /**
  81.      * Called when the equipped item is right clicked.
  82.      */
  83.     @Override
  84.     public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand handIn) {
  85.         // we can't use `this` because its inventory is not updated
  86.         // we have to get the specific inventory of the rightclicked Vacpack
  87.         ItemStack stackThis = player.getHeldItemMainhand(); // rightclicked stack
  88.         VacpackInventory invThis = new VacpackInventory(stackThis); // REAL inventory
  89.         // we don't want to shoot if the player is opening the vacpack's inventory
  90.         if (player.isSneaking()) {
  91.             if (!world.isRemote) {
  92.                 player.openGui(SlimeRancher.instance, SlimeRancher.GUI_ITEM_INV, world, 0, 0, 0);
  93.             }
  94.         } else {
  95.             // consumeItemInventory returns null if there's no itemstack in the selected slot
  96.             ItemStack item = invThis.consumeInventoryItem();
  97.             // if item is null, the EntityItem will not be created
  98.             // same if the item.getItem() is null
  99.             if (item != null) {
  100.                 //world.playSoundAtEntity(player, SlimeRancher.MODID + ":vacpack_shoot", 0.5F, 1.0F);
  101.                 SlimeRancher.debug("shooting " + item.getItem().getUnlocalizedName() + ", count " + item.getCount());
  102.                 if (!world.isRemote) {
  103.                     world.spawnEntity(new VacpackShotItem(world, player, item));
  104.                 }
  105.             } else {
  106.                 SlimeRancher.debug("cannot shoot");
  107.                 //world.playSoundAtEntity(player, SlimeRancher.MODID + ":vacpack_empty", 0.5F, 1.0F);
  108.             }
  109.         }
  110.        
  111.         return new ActionResult<ItemStack>(EnumActionResult.PASS, player.getHeldItem(handIn));
  112.     }
  113.  
  114.     @Override
  115.     public void onUsingTick(ItemStack stack, EntityLivingBase player, int count) {
  116.     }
  117.  
  118.     @Override
  119.     public boolean onEntitySwing(EntityLivingBase entityLiving, ItemStack stack) {
  120.         ((ItemVacpack) stack.getItem()).toggleSuck();
  121.         return true; // dont do anything else when we swing the item
  122.     }
  123.  
  124.     public boolean onLeftClickEntity(ItemStack stack, EntityPlayer player, Entity entity) {
  125.         return true; // dont attack the entity when we left click it
  126.     }
  127.  
  128.     public boolean onBlockStartBreak(ItemStack itemstack, BlockPos pos, EntityPlayer player) {
  129.         return true; // dont break the block we start to left click
  130.     }
  131.    
  132.     public boolean canDestroyBlockInCreative(World world, BlockPos pos, ItemStack stack, EntityPlayer player) {
  133.         return false;
  134.     }
  135.  
  136.     /**
  137.      * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
  138.      * update it's contents.
  139.      *
  140.      * method content stolen from Just-a-Magnet-Mod
  141.      */
  142.     public void onUpdate(ItemStack stack, World worldIn, Entity player, int itemSlot, boolean isSelected) {
  143.         // Grab a list of the items around the player
  144.         if (! isSelected || ! ((ItemVacpack) stack.getItem()).getSuck()) { return; }
  145.        
  146.         List<EntityItem> floatingItems = worldIn.getEntitiesWithinAABB(
  147.                 EntityItem.class,
  148.                 new AxisAlignedBB(
  149.                         player.posX - range,
  150.                         player.posY - range,
  151.                         player.posZ - range,
  152.                         player.posX + range,
  153.                         player.posY + range,
  154.                         player.posZ + range )
  155.         );
  156.         if( floatingItems.isEmpty() ) { return; }
  157.  
  158.         for( EntityItem item : floatingItems ) {
  159.             item.addVelocity(
  160.                     (player.posX - item.posX)*speed,
  161.                     ((player.posY + 2)- item.posY)*speed,
  162.                     (player.posZ - item.posZ)*speed );
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement