Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sforzando.fiveyears;
- public class FiveYearsItems {
- public static final Item BUNDLE = register("bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item WHITE_BUNDLE = register("white_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item ORANGE_BUNDLE = register("orange_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item MAGENTA_BUNDLE = register("magenta_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item LIGHT_BLUE_BUNDLE = register("light_blue_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item YELLOW_BUNDLE = register("yellow_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item LIGHT_GREEN_BUNDLE = register("light_green_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item PINK_BUNDLE = register("pink_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item GRAY_BUNDLE = register("gray_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item LIGHT_GRAY_BUNDLE = register("light_gray_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item CYAN_BUNDLE = register("cyan_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item PURPLE_BUNDLE = register("purple_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item BLUE_BUNDLE = register("blue_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item BROWN_BUNDLE = register("brown_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item GREEN_BUNDLE = register("green_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item RED_BUNDLE = register("red_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public static final Item BLACK_BUNDLE = register("black_bundle", new BundleItem(new Item.Settings().maxCount(1)));
- public Item register (String path, Item i) {
- return Registry.register(Registries.ITEM, Identifier.of("five_years", path), i);
- }
- }
- public class BundleItem extends Item {
- private Identifier fullModel = null;
- private SoundEvent successSound = new SoundEvent(Identifier.of("five_years", "sounds/bundle_success"), Optional.<Float>empty());
- private SoundEvent failureSound = new SoundEvent(Identifier.of("five_years", "sounds/bundle_failure"), Optional.<Float>empty());
- public BundleItem (Item.Settings settings) {
- super (settings);
- }
- public BundleItem (Item.Settings settings, Identifier full, SoundEvent success, SoundEvent failure) {
- super (settings);
- this.fullModel = full;
- this.successSound = success;
- this.failureSound = failure;
- }
- private void checkModel (ItemStack item) {
- if (!item.getComponents().contains(CONTENTS)) {
- ((PatchedDataComponentMap)item.getComponents()).set(CONTENTS, CustomData.of(new NbtCompound()));
- return;
- }
- if (getTotalCount(parseNbt(item.getComponents().get(CONTENTS).copyNbt())) >= 64)
- this.setFullModel(item);
- else
- this.setEmptyModel(item);
- }
- private static DefaultedList<ItemStack> parseNbt (NbtCompound tag) {
- DefaultedList<ItemStack> items = DefaultedList.<ItemStack>create();
- Inventories.readNbt(tag, items, BuiltinRegistries.createWrapperLookup());
- return items;
- }
- private static int getTotalCount (DefaultedList<ItemStack> items) {
- int i = 0;
- for (ItemStack i : items)
- i += (int) (((float) i.getCount() / i.getMaxStackSize()) * 64);
- return i;
- }
- protected void setFullModel (ItemStack item) {
- ResourceLocation model;
- if (this.fullModel != null) {
- model = this.fullModel;
- } else {
- model = Identifier.of(Registries.ITEM.getId(this).getNamespace(), "model/".concat(Registries.ITEM.getId(this).getPath().concat("_full")));
- }
- ((PatchedDataComponentMap)item.getComponents()).set(DataComponentTypes.ITEM_MODEL, model);
- }
- protected void setEmptyModel (ItemStack item) {
- ((PatchedDataComponentMap)item.getComponents()).set(DataComponentTypes.ITEM_MODEL, "model/".concat(Registries.ITEM.getId(this)));
- }
- private static DefaultedList<ItemStack> getItems (ItemStack item) {
- if (!item.getComponents().contains(CONTENTS)) {
- ((PatchedDataComponentMap)item.getComponents()).set(CONTENTS, CustomData.of(new NbtCompound()));
- return DefaultedList.<ItemStack>of();
- }
- return parseNbt(item.getComponents().get(CONTENTS).copyNbt());
- }
- public ActionResult use (World world, PlayerEntity user, Hand hand) {
- DefaultedList<ItemStack> items = getItems(user.getStackInHand(hand));
- if (items.getSize() == 0) return super.use(world, user, hand);
- for (ItemStack i : items) user.getInventory().offerOrDrop(i);
- return ActionResult.sidedSuccess(true);
- }
- public boolean isItemBarVisible (ItemStack i) {
- return getTotalCount(getItems(i)) != 0;
- }
- public int getItemBarStep (ItemStack i) {
- return getTotalCount(getItems(i)) / 4;
- }
- public int getItemBarColor (ItemStack i) {
- if (getTotalCount(this.getItems(i)) == 64)
- return COLOR_FULL;
- else
- return COLOR_EMPTY;
- }
- public void onStackClicked (ItemStack stack, Slot slot, ClickType clickType, PlayerEntity player) {
- if (slot.getStack().isEmpty()) {
- super.onStackClicked(stack, slot, clickType, player);
- return;
- }
- if (clickType == ClickType.LEFT) {
- if (fits(stack, slot.getItem()) > 0) {
- add(stack, slot.getItem().copy().setCount(fits(stack, slot.getItem())));
- slot.setStackNoCallbacks(slot.getStack().copy().shrink(fits(stack, slot.getItem())));
- player.getWorld().playSound(player, player.getBlockPos(), this.successSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
- } else {
- player.getWorld().playSound(player, player.getBlockPos(), this.failureSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
- }
- } else {
- if (fits(stack, slot.getItem()) > 0) {
- add(stack, slot.getItem().copy().setCount(1));
- slot.setStackNoCallbacks(slot.getStack().copy().shrink(1));
- player.getWorld().playSound(player, player.getBlockPos(), this.successSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
- } else {
- player.getWorld().playSound(player, player.getBlockPos(), this.failureSound, SoundCategory.PLAYERS, 1.0F, ((float) player.getWorld().getRandom().nextInt(4)) / 10 + 0.8F);
- }
- }
- }
- public void onClicked (ItemStack stack, ItemStack otherStack, Slot slot, ClickType clickType, PlayerEntity player, StackReference ref) {
- if (otherStack == ItemStack.EMPTY) {
- super.onClicked(stack, otherStack, slot, clickType, player, ref);
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement