Guest User

ItemVacpack.java

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