Advertisement
SforzandoCF

better bundels

Oct 25th, 2024 (edited)
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.48 KB | None | 0 0
  1. package com.sforzando.fiveyears;
  2.  
  3. public class FiveYearsItems {
  4.     public static final Item BUNDLE = register("bundle", new BundleItem(new Item.Settings().maxCount(1)));
  5.     public static final Item WHITE_BUNDLE = register("white_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  6.     public static final Item ORANGE_BUNDLE = register("orange_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  7.     public static final Item MAGENTA_BUNDLE = register("magenta_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  8.     public static final Item LIGHT_BLUE_BUNDLE = register("light_blue_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  9.     public static final Item YELLOW_BUNDLE = register("yellow_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  10.     public static final Item LIGHT_GREEN_BUNDLE = register("light_green_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  11.     public static final Item PINK_BUNDLE = register("pink_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  12.     public static final Item GRAY_BUNDLE = register("gray_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  13.     public static final Item LIGHT_GRAY_BUNDLE = register("light_gray_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  14.     public static final Item CYAN_BUNDLE = register("cyan_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  15.     public static final Item PURPLE_BUNDLE = register("purple_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  16.     public static final Item BLUE_BUNDLE = register("blue_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  17.     public static final Item BROWN_BUNDLE = register("brown_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  18.     public static final Item GREEN_BUNDLE = register("green_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  19.     public static final Item RED_BUNDLE = register("red_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  20.     public static final Item BLACK_BUNDLE = register("black_bundle", new BundleItem(new Item.Settings().maxCount(1)));
  21.    
  22.     public Item register (String path, Item i) {
  23.         return Registry.register(Registries.ITEM, Identifier.of("five_years", path), i);
  24.     }
  25. }
  26.  
  27. public class BundleItem extends Item {
  28.     private Identifier fullModel = null;
  29.    
  30.     private SoundEvent successSound = new SoundEvent(Identifier.of("five_years", "sounds/bundle_success"), Optional.<Float>empty());
  31.     private SoundEvent failureSound = new SoundEvent(Identifier.of("five_years", "sounds/bundle_failure"), Optional.<Float>empty());
  32.    
  33.     public BundleItem (Item.Settings settings) {
  34.         super (settings);
  35.     }
  36.    
  37.     public BundleItem (Item.Settings settings, Identifier full, SoundEvent success, SoundEvent failure) {
  38.         super (settings);
  39.         this.fullModel = full;
  40.         this.successSound = success;
  41.         this.failureSound = failure;
  42.     }
  43.    
  44.     private void checkModel (ItemStack item) {
  45.         if (!item.getComponents().contains(CONTENTS)) {
  46.             ((PatchedDataComponentMap)item.getComponents()).set(CONTENTS, CustomData.of(new NbtCompound()));
  47.             return;
  48.         }
  49.         if (getTotalCount(parseNbt(item.getComponents().get(CONTENTS).copyNbt())) >= 64)
  50.             this.setFullModel(item);
  51.         else
  52.             this.setEmptyModel(item);
  53.     }
  54.    
  55.     private static DefaultedList<ItemStack> parseNbt (NbtCompound tag) {
  56.         DefaultedList<ItemStack> items = DefaultedList.<ItemStack>create();
  57.         Inventories.readNbt(tag, items, BuiltinRegistries.createWrapperLookup());
  58.         return items;
  59.     }
  60.    
  61.     private static int getTotalCount (DefaultedList<ItemStack> items) {
  62.         int i = 0;
  63.         for (ItemStack i : items)
  64.             i += (int) (((float) i.getCount() / i.getMaxStackSize()) * 64);
  65.         return i;
  66.     }
  67.    
  68.     protected void setFullModel (ItemStack item) {
  69.         ResourceLocation model;
  70.         if (this.fullModel != null) {
  71.             model = this.fullModel;
  72.         } else {
  73.             model = Identifier.of(Registries.ITEM.getId(this).getNamespace(), "model/".concat(Registries.ITEM.getId(this).getPath().concat("_full")));
  74.         }
  75.         ((PatchedDataComponentMap)item.getComponents()).set(DataComponentTypes.ITEM_MODEL, model);
  76.     }
  77.    
  78.     protected void setEmptyModel (ItemStack item) {
  79.         ((PatchedDataComponentMap)item.getComponents()).set(DataComponentTypes.ITEM_MODEL, "model/".concat(Registries.ITEM.getId(this)));
  80.     }
  81.    
  82.     private static DefaultedList<ItemStack> getItems (ItemStack item) {
  83.         if (!item.getComponents().contains(CONTENTS)) {
  84.             ((PatchedDataComponentMap)item.getComponents()).set(CONTENTS, CustomData.of(new NbtCompound()));
  85.             return DefaultedList.<ItemStack>of();
  86.         }
  87.         return parseNbt(item.getComponents().get(CONTENTS).copyNbt());
  88.     }
  89.    
  90.     public ActionResult use (World world, PlayerEntity user, Hand hand) {
  91.         DefaultedList<ItemStack> items = getItems(user.getStackInHand(hand));
  92.         if (items.getSize() == 0) return super.use(world, user, hand);
  93.         for (ItemStack i : items) user.getInventory().offerOrDrop(i);
  94.         return ActionResult.sidedSuccess(true);
  95.     }
  96.    
  97.     public boolean isItemBarVisible (ItemStack i) {
  98.         return getTotalCount(getItems(i)) != 0;
  99.     }
  100.    
  101.     public int getItemBarStep (ItemStack i) {
  102.         return getTotalCount(getItems(i)) / 4;
  103.     }
  104.    
  105.     public int getItemBarColor (ItemStack i) {
  106.         if (getTotalCount(this.getItems(i)) == 64)
  107.             return COLOR_FULL;
  108.         else
  109.             return COLOR_EMPTY;
  110.     }
  111.    
  112.     public void onStackClicked (ItemStack stack, Slot slot, ClickType clickType, PlayerEntity player) {
  113.         if (slot.getStack().isEmpty()) {
  114.             super.onStackClicked(stack, slot, clickType, player);
  115.             return;
  116.         }
  117.         if (clickType == ClickType.LEFT) {
  118.             if (fits(stack, slot.getItem()) > 0) {
  119.                 add(stack, slot.getItem().copy().setCount(fits(stack, slot.getItem())));
  120.                 slot.setStackNoCallbacks(slot.getStack().copy().shrink(fits(stack, slot.getItem())));
  121.                 player.getWorld().playSound(player, player.getBlockPos(), this.successSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
  122.             } else {
  123.                 player.getWorld().playSound(player, player.getBlockPos(), this.failureSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
  124.             }
  125.         } else {
  126.             if (fits(stack, slot.getItem()) > 0) {
  127.                 add(stack, slot.getItem().copy().setCount(1));
  128.                 slot.setStackNoCallbacks(slot.getStack().copy().shrink(1));
  129.                 player.getWorld().playSound(player, player.getBlockPos(), this.successSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
  130.             } else {
  131.                 player.getWorld().playSound(player, player.getBlockPos(), this.failureSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
  132.             }
  133.         }
  134.     }
  135.    
  136.     public void onClicked (ItemStack stack, ItemStack otherStack, Slot slot, ClickType clickType, PlayerEntity player, StackReference ref) {
  137.         if (otherStack == ItemStack.EMPTY) {
  138.             super.onClicked(stack, otherStack, slot, clickType, player, ref);
  139.             return;
  140.         }
  141.        
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement