Advertisement
jayhillx

BugHabitatItem

Jan 6th, 2023
1,159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.94 KB | None | 0 0
  1. package com.mysticsbiomes.common.item;
  2.  
  3. import com.mysticsbiomes.client.entity.Butterfly;
  4. import com.mysticsbiomes.common.util.CreativeTabUtils;
  5. import net.minecraft.ChatFormatting;
  6. import net.minecraft.Util;
  7. import net.minecraft.client.resources.language.I18n;
  8. import net.minecraft.core.BlockPos;
  9. import net.minecraft.core.Direction;
  10. import net.minecraft.nbt.CompoundTag;
  11. import net.minecraft.nbt.DoubleTag;
  12. import net.minecraft.nbt.ListTag;
  13. import net.minecraft.nbt.Tag;
  14. import net.minecraft.network.chat.Component;
  15. import net.minecraft.network.chat.Style;
  16. import net.minecraft.network.chat.TranslatableComponent;
  17. import net.minecraft.world.InteractionHand;
  18. import net.minecraft.world.InteractionResult;
  19. import net.minecraft.world.entity.Entity;
  20. import net.minecraft.world.entity.EntityType;
  21. import net.minecraft.world.entity.LivingEntity;
  22. import net.minecraft.world.entity.Mob;
  23. import net.minecraft.world.entity.animal.Bee;
  24. import net.minecraft.world.entity.monster.Monster;
  25. import net.minecraft.world.entity.player.Player;
  26. import net.minecraft.world.item.Item;
  27. import net.minecraft.world.item.ItemStack;
  28. import net.minecraft.world.item.TooltipFlag;
  29. import net.minecraft.world.item.context.UseOnContext;
  30. import net.minecraft.world.level.Level;
  31.  
  32. import java.util.List;
  33.  
  34. /**
  35.  * Item used to hold & contain bees & butterflies.
  36.  */
  37. public class BugHabitatItem extends Item {
  38.  
  39.     public BugHabitatItem() {
  40.         super(new Properties().stacksTo(1).tab(CreativeTabUtils.TAB));
  41.     }
  42.  
  43.     @Override
  44.     public InteractionResult interactLivingEntity(ItemStack stack, Player player, LivingEntity entity, InteractionHand hand) {
  45.         if (NBTHelper.hasMob(stack) || !entity.isAlive()) {
  46.             return InteractionResult.FAIL;
  47.         }
  48.  
  49.         if (!(entity instanceof Bee || entity instanceof Butterfly)) {
  50.             return InteractionResult.FAIL;
  51.         }
  52.  
  53.         CompoundTag nbt = NBTHelper.getBaseTag(stack);
  54.  
  55.         if (!player.level.isClientSide) {
  56.             CompoundTag data = entity.serializeNBT();
  57.  
  58.             nbt.put(NBTHelper.MOB_DATA, data);
  59.             nbt.putString(NBTHelper.MOB_NAME, entity.getType().getDescriptionId());
  60.             nbt.putFloat(NBTHelper.MOB_HEALTH, Math.round((entity.getHealth() * 10) / 10.0));
  61.             nbt.putFloat(NBTHelper.MOB_MAX_HEALTH, entity.getMaxHealth());
  62.  
  63.             if (player.isCreative()) {
  64.                 ItemStack newLasso = new ItemStack(this);
  65.                 NBTHelper.setBaseTag(newLasso, nbt);
  66.                 player.addItem(newLasso);
  67.             }
  68.  
  69.             entity.discard();
  70.             player.inventoryMenu.broadcastChanges();
  71.         }
  72.         return InteractionResult.SUCCESS;
  73.     }
  74.  
  75.     @Override
  76.     public InteractionResult useOn(UseOnContext context) {
  77.         Player player = context.getPlayer();
  78.         ItemStack stack = context.getItemInHand();
  79.         Direction facing = context.getClickedFace();
  80.         BlockPos pos = context.getClickedPos().offset(facing.getStepX(), facing.getStepY(), facing.getStepZ());
  81.         Level world = context.getLevel();
  82.  
  83.         if (!NBTHelper.hasMob(stack)) return InteractionResult.FAIL;
  84.  
  85.         if (!player.mayUseItemAt(pos, facing, stack)) return InteractionResult.FAIL;
  86.  
  87.         if (!context.getLevel().isClientSide()) {
  88.             CompoundTag nbt = NBTHelper.getBaseTag(stack);
  89.             CompoundTag mobData = nbt.getCompound(NBTHelper.MOB_DATA);
  90.  
  91.             DoubleTag x = DoubleTag.valueOf(pos.getX() + 0.5);
  92.             DoubleTag y = DoubleTag.valueOf(pos.getY());
  93.             DoubleTag z = DoubleTag.valueOf(pos.getZ() + 0.5);
  94.             ListTag mobPos = NBTHelper.createNBTList(x, y, z);
  95.             mobData.put("Pos", mobPos);
  96.  
  97.             Entity mob = EntityType.loadEntityRecursive(mobData, world, entity -> {
  98.                 return entity;
  99.             });
  100.             if (mob != null) world.addFreshEntity(mob);
  101.  
  102.             stack.removeTagKey(NBTHelper.MOB);
  103.             stack.hurtAndBreak(1, player, wutTheFak -> {});
  104.         }
  105.  
  106.         return InteractionResult.SUCCESS;
  107.     }
  108.  
  109.     @Override
  110.     public Component getName(ItemStack stack) {
  111.         return new TranslatableComponent(this.getDescriptionId(stack)).withStyle(ChatFormatting.AQUA);
  112.     }
  113.  
  114.     @Override
  115.     public void appendHoverText(ItemStack stack, Level world, List<Component> tooltip, TooltipFlag flag) {
  116.         if (NBTHelper.hasMob(stack)) {
  117.             CompoundTag nbt = NBTHelper.getBaseTag(stack);
  118.             String name = nbt.getString(NBTHelper.MOB_NAME);
  119.             double health = nbt.getDouble(NBTHelper.MOB_HEALTH);
  120.             double maxHealth = nbt.getDouble(NBTHelper.MOB_MAX_HEALTH);
  121.  
  122.             tooltip.add(new TranslatableComponent(name).withStyle(ChatFormatting.GRAY));
  123.             tooltip.add(new TranslatableComponent(health + " / " + maxHealth).withStyle(ChatFormatting.GRAY));
  124.         } else {
  125.             tooltip.add(new TranslatableComponent("Right click a bug to capture it!").withStyle(ChatFormatting.GRAY));
  126.         }
  127.     }
  128.  
  129.     public static class NBTHelper {
  130.         // base
  131.         public static final String MOB = "capturedMob";
  132.         // tags under capturedMob tag
  133.         public static final String MOB_NAME = "mobName";
  134.         public static final String MOB_DATA = "mobData";
  135.         public static final String MOB_HEALTH = "mobHealth";
  136.         public static final String MOB_MAX_HEALTH = "mobMaxHealth";
  137.  
  138.         public static CompoundTag getBaseTag(ItemStack stack) {
  139.             return stack.getOrCreateTagElement(MOB);
  140.         }
  141.  
  142.         public static void setBaseTag(ItemStack stack, CompoundTag nbt) {
  143.             stack.getOrCreateTag().put(MOB, nbt);
  144.         }
  145.  
  146.         public static boolean hasMob(ItemStack stack) {
  147.             return stack.getOrCreateTag().contains(MOB);
  148.         }
  149.  
  150.         public static ListTag createNBTList(Tag... tags) {
  151.             ListTag list = new ListTag();
  152.             for (Tag i: tags) list.add(i);
  153.  
  154.             return list;
  155.         }
  156.     }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement