From 171c2f4a801576197d88aa713e7139412ccdee7f Mon Sep 17 00:00:00 2001 From: Buuz135 Date: Thu, 6 Jan 2022 13:55:12 +0100 Subject: [PATCH] Added tooltip for the drawers to show its contents --- src/generated/resources/.cache/cache | 6 +- .../assets/functionalstorage/lang/en_us.json | 1 + .../functionalstorage/tags/items/drawer.json | 18 +- .../minecraft/tags/blocks/mineable/axe.json | 18 +- .../functionalstorage/FunctionalStorage.java | 25 ++- .../functionalstorage/block/DrawerBlock.java | 41 +++- .../block/EnderDrawerBlock.java | 20 -- .../data/FunctionalStorageLangProvider.java | 1 + .../inventory/BigInventoryHandler.java | 3 +- .../item/DrawerCapabilityProvider.java | 39 ++++ .../item/DrawerStackItemHandler.java | 185 ++++++++++++++++++ .../item/LinkingToolItem.java | 17 +- .../functionalstorage/util/TooltipUtil.java | 53 ++++- 13 files changed, 357 insertions(+), 70 deletions(-) create mode 100644 src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerCapabilityProvider.java create mode 100644 src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerStackItemHandler.java diff --git a/src/generated/resources/.cache/cache b/src/generated/resources/.cache/cache index 044f112..3c86e8a 100644 --- a/src/generated/resources/.cache/cache +++ b/src/generated/resources/.cache/cache @@ -26,7 +26,7 @@ a50aeb3877dd4003bb3ed359fccde1f3674bc028 assets/functionalstorage/blockstates/sp fbf8ebd85350f3b4ad0bf6faf51f9ecf0677bf5b assets/functionalstorage/blockstates/warped_1.json bcd4b5e128d5175bea44ecae340aeec86cd5a123 assets/functionalstorage/blockstates/warped_2.json ebc4aebffb44baa4dd507078b9fd3c74e881f4c1 assets/functionalstorage/blockstates/warped_4.json -904f5686edc535271e730aaaa27811f689efbfb4 assets/functionalstorage/lang/en_us.json +74b41817ead04f33efc61a7716871e43427de99a assets/functionalstorage/lang/en_us.json 68c3f6a8fe55202f83c0fcdd6c8ca10ef88019ef assets/functionalstorage/models/block/acacia_1_locked.json c7d0376ba2a080ba12c344369ad11538899db176 assets/functionalstorage/models/block/acacia_2_locked.json 79b86baeec4aa6784aa52028d2f3bc98ddab8c14 assets/functionalstorage/models/block/acacia_4_locked.json @@ -130,7 +130,7 @@ d6b02c345edd356b8e4b7421bdd453f454c6ed7f data/functionalstorage/recipes/storage_ bcb281904eac23183c45786e3d703d24bba92be6 data/functionalstorage/recipes/warped_1.json 8fc3f76a2c57eb4d80ce86947fabebe48fa6f692 data/functionalstorage/recipes/warped_2.json 7510a8ca1f1e3bb63f4c4f4add0bb6b713feaa0b data/functionalstorage/recipes/warped_4.json -c90035bcbbce8f048618fbdd442779eb16c37cc0 data/functionalstorage/tags/items/drawer.json +f37e620a26ceb158507c607cee6ba3b51f14c6d6 data/functionalstorage/tags/items/drawer.json 12ec935226bf5a6a1493d353ef6dc6c224c256dd data/functionalstorage/tags/items/ignore_crafting_check.json -c90035bcbbce8f048618fbdd442779eb16c37cc0 data/minecraft/tags/blocks/mineable/axe.json +f37e620a26ceb158507c607cee6ba3b51f14c6d6 data/minecraft/tags/blocks/mineable/axe.json 2c1ca62408eae8963178024550b0771101e01d53 data/minecraft/tags/blocks/mineable/pickaxe.json diff --git a/src/generated/resources/assets/functionalstorage/lang/en_us.json b/src/generated/resources/assets/functionalstorage/lang/en_us.json index 930f8f1..d88ddb5 100644 --- a/src/generated/resources/assets/functionalstorage/lang/en_us.json +++ b/src/generated/resources/assets/functionalstorage/lang/en_us.json @@ -33,6 +33,7 @@ "configurationtool.configmode.toggle_render": "Hide/Show Item Renders", "configurationtool.configmode.toggle_upgrades": "Hide/Show Upgrade Renders", "configurationtool.use": "Sneak + Right Click in the air to change modes. Right click a drawer to toggle the option.", + "drawer.block.contents": "Contents: ", "item.functionalstorage.collector_upgrade": "Collector Upgrade", "item.functionalstorage.configuration_tool": "Configuration Tool", "item.functionalstorage.copper_upgrade": "Copper Upgrade", diff --git a/src/generated/resources/data/functionalstorage/tags/items/drawer.json b/src/generated/resources/data/functionalstorage/tags/items/drawer.json index 56bc599..dd31495 100644 --- a/src/generated/resources/data/functionalstorage/tags/items/drawer.json +++ b/src/generated/resources/data/functionalstorage/tags/items/drawer.json @@ -9,14 +9,6 @@ "functionalstorage:dark_oak_2", "functionalstorage:crimson_2", "functionalstorage:warped_2", - "functionalstorage:oak_1", - "functionalstorage:spruce_1", - "functionalstorage:birch_1", - "functionalstorage:jungle_1", - "functionalstorage:acacia_1", - "functionalstorage:dark_oak_1", - "functionalstorage:crimson_1", - "functionalstorage:warped_1", "functionalstorage:oak_4", "functionalstorage:spruce_4", "functionalstorage:birch_4", @@ -24,6 +16,14 @@ "functionalstorage:acacia_4", "functionalstorage:dark_oak_4", "functionalstorage:crimson_4", - "functionalstorage:warped_4" + "functionalstorage:warped_4", + "functionalstorage:oak_1", + "functionalstorage:spruce_1", + "functionalstorage:birch_1", + "functionalstorage:jungle_1", + "functionalstorage:acacia_1", + "functionalstorage:dark_oak_1", + "functionalstorage:crimson_1", + "functionalstorage:warped_1" ] } \ No newline at end of file diff --git a/src/generated/resources/data/minecraft/tags/blocks/mineable/axe.json b/src/generated/resources/data/minecraft/tags/blocks/mineable/axe.json index 56bc599..dd31495 100644 --- a/src/generated/resources/data/minecraft/tags/blocks/mineable/axe.json +++ b/src/generated/resources/data/minecraft/tags/blocks/mineable/axe.json @@ -9,14 +9,6 @@ "functionalstorage:dark_oak_2", "functionalstorage:crimson_2", "functionalstorage:warped_2", - "functionalstorage:oak_1", - "functionalstorage:spruce_1", - "functionalstorage:birch_1", - "functionalstorage:jungle_1", - "functionalstorage:acacia_1", - "functionalstorage:dark_oak_1", - "functionalstorage:crimson_1", - "functionalstorage:warped_1", "functionalstorage:oak_4", "functionalstorage:spruce_4", "functionalstorage:birch_4", @@ -24,6 +16,14 @@ "functionalstorage:acacia_4", "functionalstorage:dark_oak_4", "functionalstorage:crimson_4", - "functionalstorage:warped_4" + "functionalstorage:warped_4", + "functionalstorage:oak_1", + "functionalstorage:spruce_1", + "functionalstorage:birch_1", + "functionalstorage:jungle_1", + "functionalstorage:acacia_1", + "functionalstorage:dark_oak_1", + "functionalstorage:crimson_1", + "functionalstorage:warped_1" ] } \ No newline at end of file diff --git a/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java b/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java index bd3a770..93d9576 100644 --- a/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java +++ b/src/main/java/com/buuz135/functionalstorage/FunctionalStorage.java @@ -9,14 +9,14 @@ import com.buuz135.functionalstorage.data.FunctionalStorageBlockTagsProvider; import com.buuz135.functionalstorage.data.FunctionalStorageBlockstateProvider; import com.buuz135.functionalstorage.data.FunctionalStorageItemTagsProvider; import com.buuz135.functionalstorage.data.FunctionalStorageLangProvider; +import com.buuz135.functionalstorage.inventory.BigInventoryHandler; +import com.buuz135.functionalstorage.inventory.item.DrawerStackItemHandler; import com.buuz135.functionalstorage.item.ConfigurationToolItem; import com.buuz135.functionalstorage.item.LinkingToolItem; import com.buuz135.functionalstorage.item.StorageUpgradeItem; import com.buuz135.functionalstorage.item.UpgradeItem; import com.buuz135.functionalstorage.network.EnderDrawerSyncMessage; -import com.buuz135.functionalstorage.util.DrawerWoodType; -import com.buuz135.functionalstorage.util.IWoodType; -import com.buuz135.functionalstorage.util.StorageTags; +import com.buuz135.functionalstorage.util.*; import com.hrznstudio.titanium.block.BasicBlock; import com.hrznstudio.titanium.block.BasicTileBlock; import com.hrznstudio.titanium.datagenerator.loot.TitaniumLootTableProvider; @@ -44,6 +44,7 @@ import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.client.event.ColorHandlerEvent; import net.minecraftforge.client.event.EntityRenderersEvent; +import net.minecraftforge.client.event.RenderTooltipEvent; import net.minecraftforge.client.model.generators.BlockModelProvider; import net.minecraftforge.client.model.generators.ItemModelProvider; import net.minecraftforge.common.Tags; @@ -52,6 +53,7 @@ import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.forge.event.lifecycle.GatherDataEvent; +import net.minecraftforge.items.CapabilityItemHandler; import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.RegistryObject; import org.apache.logging.log4j.LogManager; @@ -97,6 +99,23 @@ public class FunctionalStorage extends ModuleController { public FunctionalStorage() { DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> this::onClient); + EventManager.forge(RenderTooltipEvent.Pre.class).process(itemTooltipEvent -> { + if (itemTooltipEvent.getItemStack().getItem().equals(FunctionalStorage.ENDER_DRAWER.get().asItem()) && itemTooltipEvent.getItemStack().hasTag()) { + TooltipUtil.renderItems(itemTooltipEvent.getPoseStack(), EnderDrawerBlock.getFrequencyDisplay(itemTooltipEvent.getItemStack().getTag().getCompound("BlockEntityTag").getString("frequency")), itemTooltipEvent.getX() + 14, itemTooltipEvent.getY() + 11); + } + if (itemTooltipEvent.getItemStack().is(FunctionalStorage.LINKING_TOOL.get()) && itemTooltipEvent.getItemStack().getOrCreateTag().contains(LinkingToolItem.NBT_ENDER)) { + TooltipUtil.renderItems(itemTooltipEvent.getPoseStack(), EnderDrawerBlock.getFrequencyDisplay(itemTooltipEvent.getItemStack().getOrCreateTag().getString(LinkingToolItem.NBT_ENDER)), itemTooltipEvent.getX() + 14, itemTooltipEvent.getY() + 11); + } + itemTooltipEvent.getItemStack().getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(iItemHandler -> { + if (iItemHandler instanceof DrawerStackItemHandler) { + int i = 0; + for (BigInventoryHandler.BigStack storedStack : ((DrawerStackItemHandler) iItemHandler).getStoredStacks()) { + TooltipUtil.renderItemAdvanced(itemTooltipEvent.getPoseStack(), storedStack.getStack(), itemTooltipEvent.getX() + 20 + 26 * i, itemTooltipEvent.getY() + 11, 512, NumberUtils.getFormatedBigNumber(storedStack.getAmount()) + "/" + NumberUtils.getFormatedBigNumber(iItemHandler.getSlotLimit(i))); + ++i; + } + } + }); + }).subscribe(); } diff --git a/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java index 45a7930..80e3bdc 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/DrawerBlock.java @@ -3,6 +3,7 @@ package com.buuz135.functionalstorage.block; import com.buuz135.functionalstorage.FunctionalStorage; import com.buuz135.functionalstorage.block.tile.DrawerControllerTile; import com.buuz135.functionalstorage.block.tile.DrawerTile; +import com.buuz135.functionalstorage.inventory.item.DrawerCapabilityProvider; import com.buuz135.functionalstorage.item.LinkingToolItem; import com.buuz135.functionalstorage.util.IWoodType; import com.google.common.collect.Multimap; @@ -13,19 +14,24 @@ import com.hrznstudio.titanium.module.DeferredRegistryHelper; import com.hrznstudio.titanium.recipe.generator.TitaniumShapedRecipeBuilder; import com.hrznstudio.titanium.util.RayTraceUtils; import com.hrznstudio.titanium.util.TileUtil; +import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; import net.minecraft.data.recipes.FinishedRecipe; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.network.chat.TextComponent; +import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.TooltipFlag; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; @@ -41,15 +47,16 @@ import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; import net.minecraftforge.common.Tags; +import net.minecraftforge.common.capabilities.ICapabilityProvider; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; -import java.awt.*; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.function.Consumer; -import java.util.function.Predicate; +import java.util.function.Supplier; public class DrawerBlock extends RotatableBlock { @@ -114,6 +121,17 @@ public class DrawerBlock extends RotatableBlock { registerDefaultState(defaultBlockState().setValue(RotatableBlock.FACING_HORIZONTAL, Direction.NORTH).setValue(LOCKED, false)); } + @Override + public Supplier getItemBlockFactory() { + return () -> new BlockItem(this, new Item.Properties().tab(this.getItemGroup())) { + @Nullable + @Override + public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) { + return new DrawerCapabilityProvider(stack, type); + } + }; + } + @Override protected void createBlockStateDefinition(StateDefinition.Builder p_206840_1_) { super.createBlockStateDefinition(p_206840_1_); @@ -244,7 +262,7 @@ public class DrawerBlock extends RotatableBlock { public void onRemove(BlockState state, Level worldIn, BlockPos pos, BlockState newState, boolean isMoving) { if (!state.is(newState.getBlock())){ TileUtil.getTileEntity(worldIn, pos, DrawerTile.class).ifPresent(tile -> { - if (tile.getControllerPos() != null){ + if (tile.getControllerPos() != null) { TileUtil.getTileEntity(worldIn, tile.getControllerPos(), DrawerControllerTile.class).ifPresent(drawerControllerTile -> { drawerControllerTile.addConnectedDrawers(LinkingToolItem.ActionMode.REMOVE, pos); }); @@ -253,4 +271,17 @@ public class DrawerBlock extends RotatableBlock { } super.onRemove(state, worldIn, pos, newState, isMoving); } + + @Override + public void appendHoverText(ItemStack p_49816_, @Nullable BlockGetter p_49817_, List tooltip, TooltipFlag p_49819_) { + super.appendHoverText(p_49816_, p_49817_, tooltip, p_49819_); + if (p_49816_.hasTag()) { + TranslatableComponent text = new TranslatableComponent("drawer.block.contents"); + tooltip.add(text.withStyle(ChatFormatting.GRAY)); + tooltip.add(new TextComponent("")); + tooltip.add(new TextComponent("")); + } + } + + } diff --git a/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java b/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java index 4c20fde..d1dc241 100644 --- a/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java +++ b/src/main/java/com/buuz135/functionalstorage/block/EnderDrawerBlock.java @@ -5,15 +5,9 @@ import com.buuz135.functionalstorage.block.tile.DrawerControllerTile; import com.buuz135.functionalstorage.block.tile.DrawerTile; import com.buuz135.functionalstorage.block.tile.EnderDrawerTile; import com.buuz135.functionalstorage.item.LinkingToolItem; -import com.buuz135.functionalstorage.util.IWoodType; -import com.buuz135.functionalstorage.util.TooltipUtil; -import com.google.common.collect.Multimap; -import com.google.common.collect.MultimapBuilder; import com.hrznstudio.titanium.block.RotatableBlock; import com.hrznstudio.titanium.datagenerator.loot.block.BasicBlockLootTables; -import com.hrznstudio.titanium.event.handler.EventManager; import com.hrznstudio.titanium.module.DeferredRegistryHelper; -import com.hrznstudio.titanium.recipe.generator.TitaniumShapedRecipeBuilder; import com.hrznstudio.titanium.util.RayTraceUtils; import com.hrznstudio.titanium.util.TileUtil; import net.minecraft.ChatFormatting; @@ -22,7 +16,6 @@ import net.minecraft.core.Direction; import net.minecraft.core.NonNullList; import net.minecraft.data.recipes.FinishedRecipe; import net.minecraft.network.chat.Component; -import net.minecraft.network.chat.Style; import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TranslatableComponent; import net.minecraft.world.InteractionHand; @@ -33,25 +26,18 @@ import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; -import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; -import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.storage.loot.LootTable; import net.minecraft.world.level.storage.loot.functions.CopyNbtFunction; import net.minecraft.world.level.storage.loot.providers.nbt.ContextNbtProvider; -import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.HitResult; import net.minecraft.world.phys.shapes.BooleanOp; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.VoxelShape; -import net.minecraftforge.client.event.RenderTooltipEvent; -import net.minecraftforge.common.Tags; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -68,12 +54,6 @@ import static com.buuz135.functionalstorage.block.DrawerBlock.LOCKED; public class EnderDrawerBlock extends RotatableBlock { - static { - EventManager.forge(RenderTooltipEvent.Pre.class).filter(itemTooltipEvent -> itemTooltipEvent.getItemStack().getItem().equals(FunctionalStorage.ENDER_DRAWER.get().asItem()) && itemTooltipEvent.getItemStack().hasTag()).process(itemTooltipEvent -> { - TooltipUtil.renderItems(itemTooltipEvent.getPoseStack(), EnderDrawerBlock.getFrequencyDisplay(itemTooltipEvent.getItemStack().getTag().getCompound("BlockEntityTag").getString("frequency")) , itemTooltipEvent.getX() + 14, itemTooltipEvent.getY() + 11); - }).subscribe(); - } - public EnderDrawerBlock() { super("ender_drawer", Properties.copy(Blocks.ENDER_CHEST), EnderDrawerTile.class); setItemGroup(FunctionalStorage.TAB); diff --git a/src/main/java/com/buuz135/functionalstorage/data/FunctionalStorageLangProvider.java b/src/main/java/com/buuz135/functionalstorage/data/FunctionalStorageLangProvider.java index f9e0a24..40f7158 100644 --- a/src/main/java/com/buuz135/functionalstorage/data/FunctionalStorageLangProvider.java +++ b/src/main/java/com/buuz135/functionalstorage/data/FunctionalStorageLangProvider.java @@ -64,5 +64,6 @@ public class FunctionalStorageLangProvider extends LanguageProvider { this.add("upgrade.type.utility", "Utility"); this.add("linkingtool.ender.frequency", "Frequency: "); this.add("linkingtool.ender.clear", "Sneak + Right Click in the air to clear frequency."); + this.add("drawer.block.contents", "Contents: "); } } diff --git a/src/main/java/com/buuz135/functionalstorage/inventory/BigInventoryHandler.java b/src/main/java/com/buuz135/functionalstorage/inventory/BigInventoryHandler.java index 49bf12e..c9758a5 100644 --- a/src/main/java/com/buuz135/functionalstorage/inventory/BigInventoryHandler.java +++ b/src/main/java/com/buuz135/functionalstorage/inventory/BigInventoryHandler.java @@ -13,7 +13,6 @@ import java.util.List; public abstract class BigInventoryHandler implements IItemHandler, INBTSerializable { - public static String VOID = "Void"; public static String BIG_ITEMS = "BigItems"; public static String STACK = "Stack"; public static String AMOUNT = "Amount"; @@ -144,7 +143,7 @@ public abstract class BigInventoryHandler implements IItemHandler, INBTSerializa return storedStacks; } - public class BigStack{ + public static class BigStack { private ItemStack stack; private int amount; diff --git a/src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerCapabilityProvider.java b/src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerCapabilityProvider.java new file mode 100644 index 0000000..62f1e65 --- /dev/null +++ b/src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerCapabilityProvider.java @@ -0,0 +1,39 @@ +package com.buuz135.functionalstorage.inventory.item; + +import com.buuz135.functionalstorage.FunctionalStorage; +import net.minecraft.core.Direction; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.common.util.LazyOptional; +import net.minecraftforge.items.CapabilityItemHandler; +import net.minecraftforge.items.IItemHandler; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class DrawerCapabilityProvider implements ICapabilityProvider { + + private final ItemStack stack; + private final DrawerStackItemHandler drawerStackItemHandler; + private final LazyOptional itemHandler; + + public DrawerCapabilityProvider(ItemStack stack, FunctionalStorage.DrawerType type) { + this.stack = stack; + this.drawerStackItemHandler = new DrawerStackItemHandler(stack, type); + this.itemHandler = LazyOptional.of(() -> this.drawerStackItemHandler); + } + + @NotNull + @Override + public LazyOptional getCapability(@NotNull Capability cap, @Nullable Direction side) { + if (cap.equals(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)) return this.itemHandler.cast(); + return LazyOptional.empty(); + } + + @NotNull + @Override + public LazyOptional getCapability(@NotNull Capability cap) { + if (cap.equals(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY)) return this.itemHandler.cast(); + return LazyOptional.empty(); + } +} diff --git a/src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerStackItemHandler.java b/src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerStackItemHandler.java new file mode 100644 index 0000000..78ceb7e --- /dev/null +++ b/src/main/java/com/buuz135/functionalstorage/inventory/item/DrawerStackItemHandler.java @@ -0,0 +1,185 @@ +package com.buuz135.functionalstorage.inventory.item; + +import com.buuz135.functionalstorage.FunctionalStorage; +import com.buuz135.functionalstorage.inventory.BigInventoryHandler; +import com.buuz135.functionalstorage.item.StorageUpgradeItem; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.Tag; +import net.minecraft.world.item.ItemStack; +import net.minecraftforge.common.util.INBTSerializable; +import net.minecraftforge.items.IItemHandler; +import net.minecraftforge.items.ItemHandlerHelper; + +import javax.annotation.Nonnull; +import java.util.ArrayList; +import java.util.List; + +import static com.buuz135.functionalstorage.inventory.BigInventoryHandler.*; + +public class DrawerStackItemHandler implements IItemHandler, INBTSerializable { + + private List storedStacks; + private ItemStack stack; + private FunctionalStorage.DrawerType type; + private int multiplier; + private boolean downgrade; + private boolean isVoid; + + public DrawerStackItemHandler(ItemStack stack, FunctionalStorage.DrawerType drawerType) { + this.stack = stack; + this.storedStacks = new ArrayList<>(); + this.type = drawerType; + this.multiplier = 1; + this.downgrade = false; + this.isVoid = false; + for (int i = 0; i < drawerType.getSlots(); i++) { + this.storedStacks.add(i, new BigInventoryHandler.BigStack(ItemStack.EMPTY, 0)); + } + if (stack.hasTag()) { + deserializeNBT(stack.getTag().getCompound("BlockEntityTag").getCompound("handler")); + for (Tag tag : stack.getOrCreateTag().getCompound("BlockEntityTag").getCompound("storageUpgrades").getList("Items", Tag.TAG_COMPOUND)) { + ItemStack itemStack = ItemStack.of((CompoundTag) tag); + if (itemStack.getItem() instanceof StorageUpgradeItem) { + if (multiplier == 1) multiplier = ((StorageUpgradeItem) itemStack.getItem()).getStorageMultiplier(); + else multiplier *= ((StorageUpgradeItem) itemStack.getItem()).getStorageMultiplier(); + } + if (itemStack.getItem().equals(FunctionalStorage.STORAGE_UPGRADES.get(StorageUpgradeItem.StorageTier.IRON).get())) { + this.downgrade = true; + } + } + for (Tag tag : stack.getOrCreateTag().getCompound("BlockEntityTag").getCompound("utilityUpgrades").getList("Items", Tag.TAG_COMPOUND)) { + ItemStack itemStack = ItemStack.of((CompoundTag) tag); + if (itemStack.getItem().equals(FunctionalStorage.VOID_UPGRADE.get())) { + this.isVoid = true; + } + } + } + } + + @Override + public CompoundTag serializeNBT() { + CompoundTag compoundTag = new CompoundTag(); + CompoundTag items = new CompoundTag(); + for (int i = 0; i < this.storedStacks.size(); i++) { + CompoundTag bigStack = new CompoundTag(); + bigStack.put(STACK, this.storedStacks.get(i).getStack().serializeNBT()); + bigStack.putInt(AMOUNT, this.storedStacks.get(i).getAmount()); + items.put(i + "", bigStack); + } + compoundTag.put(BIG_ITEMS, items); + return compoundTag; + } + + @Override + public void deserializeNBT(CompoundTag nbt) { + for (String allKey : nbt.getCompound(BIG_ITEMS).getAllKeys()) { + this.storedStacks.get(Integer.parseInt(allKey)).setStack(ItemStack.of(nbt.getCompound(BIG_ITEMS).getCompound(allKey).getCompound(STACK))); + this.storedStacks.get(Integer.parseInt(allKey)).setAmount(nbt.getCompound(BIG_ITEMS).getCompound(allKey).getInt(AMOUNT)); + } + } + + @Override + public int getSlots() { + return type.getSlots(); + } + + @Nonnull + @Override + public ItemStack getStackInSlot(int slot) { + BigStack bigStack = this.storedStacks.get(slot); + ItemStack copied = bigStack.getStack().copy(); + copied.setCount(bigStack.getAmount()); + return copied; + } + + @Nonnull + @Override + public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { + if (isValid(slot, stack)) { + BigStack bigStack = this.storedStacks.get(slot); + int inserted = Math.min(getSlotLimit(slot) - bigStack.getAmount(), stack.getCount()); + if (!simulate) { + bigStack.setStack(stack); + bigStack.setAmount(Math.min(bigStack.getAmount() + inserted, getSlotLimit(slot))); + onChange(); + } + if (inserted == stack.getCount() || isVoid()) return ItemStack.EMPTY; + return ItemHandlerHelper.copyStackWithSize(stack, stack.getCount() - inserted); + } + return stack; + } + + private boolean isVoid() { + return true; + } + + private void onChange() { + if (stack.getOrCreateTag().contains("BlockEntityTag")) + stack.getOrCreateTag().put("BlockEntityTag", new CompoundTag()); + stack.getOrCreateTag().getCompound("BlockEntityTag").put("handler", serializeNBT()); + } + + private boolean isValid(int slot, @Nonnull ItemStack stack) { + if (slot < type.getSlots()) { + BigStack bigStack = this.storedStacks.get(slot); + ItemStack fl = bigStack.getStack(); + return fl.isEmpty() || (fl.sameItem(stack) && ItemStack.tagMatches(fl, stack)); + } + return false; + } + + @Nonnull + @Override + public ItemStack extractItem(int slot, int amount, boolean simulate) { + if (amount == 0) return ItemStack.EMPTY; + if (slot < type.getSlots()) { + BigStack bigStack = this.storedStacks.get(slot); + if (bigStack.getStack().isEmpty()) return ItemStack.EMPTY; + if (bigStack.getAmount() <= amount) { + ItemStack out = bigStack.getStack().copy(); + int newAmount = bigStack.getAmount(); + if (!simulate) { + if (!isLocked()) bigStack.setStack(ItemStack.EMPTY); + bigStack.setAmount(0); + onChange(); + } + out.setCount(newAmount); + return out; + } else { + if (!simulate) { + bigStack.setAmount(bigStack.getAmount() - amount); + onChange(); + } + return ItemHandlerHelper.copyStackWithSize(bigStack.getStack(), amount); + } + } + return ItemStack.EMPTY; + } + + public boolean isLocked() { + return true; + } + + @Override + public int getSlotLimit(int slot) { + if (hasDowngrade()) return 64; + return (int) Math.min(Integer.MAX_VALUE, type.getSlotAmount() * (long) getMultiplier()); + } + + private long getMultiplier() { + return multiplier; + } + + private boolean hasDowngrade() { + return downgrade; + } + + @Override + public boolean isItemValid(int slot, @Nonnull ItemStack stack) { + return !stack.isEmpty(); + } + + public List getStoredStacks() { + return storedStacks; + } +} diff --git a/src/main/java/com/buuz135/functionalstorage/item/LinkingToolItem.java b/src/main/java/com/buuz135/functionalstorage/item/LinkingToolItem.java index 3722a31..40a2ab3 100644 --- a/src/main/java/com/buuz135/functionalstorage/item/LinkingToolItem.java +++ b/src/main/java/com/buuz135/functionalstorage/item/LinkingToolItem.java @@ -1,16 +1,13 @@ package com.buuz135.functionalstorage.item; import com.buuz135.functionalstorage.FunctionalStorage; -import com.buuz135.functionalstorage.block.EnderDrawerBlock; import com.buuz135.functionalstorage.block.tile.ControllableDrawerTile; import com.buuz135.functionalstorage.block.tile.DrawerControllerTile; import com.buuz135.functionalstorage.block.tile.EnderDrawerTile; import com.buuz135.functionalstorage.inventory.EnderInventoryHandler; -import com.buuz135.functionalstorage.util.TooltipUtil; import com.buuz135.functionalstorage.world.EnderSavedData; import com.hrznstudio.titanium.event.handler.EventManager; import com.hrznstudio.titanium.item.BasicItem; -import com.hrznstudio.titanium.util.InventoryUtil; import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; import net.minecraft.core.NonNullList; @@ -22,25 +19,22 @@ import net.minecraft.sounds.SoundEvents; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; -import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; -import net.minecraft.world.item.*; +import net.minecraft.world.item.CreativeModeTab; +import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.phys.AABB; -import net.minecraftforge.client.event.RenderTooltipEvent; -import net.minecraftforge.event.entity.player.ItemTooltipEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; import net.minecraftforge.fml.LogicalSide; -import net.minecraftforge.registries.ForgeRegistries; import org.jetbrains.annotations.Nullable; import java.awt.*; -import java.util.*; +import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; +import java.util.Locale; public class LinkingToolItem extends BasicItem { @@ -66,9 +60,6 @@ public class LinkingToolItem extends BasicItem { } static { - EventManager.forge(RenderTooltipEvent.Pre.class).filter(itemTooltipEvent -> itemTooltipEvent.getItemStack().is(FunctionalStorage.LINKING_TOOL.get()) && itemTooltipEvent.getItemStack().getOrCreateTag().contains(NBT_ENDER)).process(itemTooltipEvent -> { - TooltipUtil.renderItems(itemTooltipEvent.getPoseStack(), EnderDrawerBlock.getFrequencyDisplay(itemTooltipEvent.getItemStack().getOrCreateTag().getString(NBT_ENDER)) , itemTooltipEvent.getX() + 14, itemTooltipEvent.getY() + 11); - }).subscribe(); EventManager.forge(PlayerInteractEvent.LeftClickBlock.class).filter(leftClickBlock -> leftClickBlock.getSide() == LogicalSide.SERVER && leftClickBlock.getItemStack().is(FunctionalStorage.LINKING_TOOL.get())).process(leftClickBlock -> { ItemStack stack = leftClickBlock.getItemStack(); BlockEntity blockEntity = leftClickBlock.getWorld().getBlockEntity(leftClickBlock.getPos()); diff --git a/src/main/java/com/buuz135/functionalstorage/util/TooltipUtil.java b/src/main/java/com/buuz135/functionalstorage/util/TooltipUtil.java index ced96cb..5f270bb 100644 --- a/src/main/java/com/buuz135/functionalstorage/util/TooltipUtil.java +++ b/src/main/java/com/buuz135/functionalstorage/util/TooltipUtil.java @@ -4,7 +4,9 @@ import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.Lighting; import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.block.model.ItemTransforms; import net.minecraft.client.renderer.texture.OverlayTexture; @@ -12,25 +14,64 @@ import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.client.resources.model.BakedModel; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.ItemUtils; -import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; +import javax.annotation.Nullable; import java.util.List; public class TooltipUtil { - public static void renderItems(PoseStack stack, List items, int x, int y){ + public static void renderItems(PoseStack stack, List items, int x, int y) { for (int i = 0; i < items.size(); i++) { - renderItemIntoGUI(stack, items.get(i), x + 18 * i , y, 512); + renderItemIntoGUI(stack, items.get(i), x + 18 * i, y, 512); } } public static void renderItemIntoGUI(PoseStack matrixStack, ItemStack stack, int x, int y, int z) { - renderItemModelIntoGUI(matrixStack, stack, x, y, z, Minecraft.getInstance().getItemRenderer().getModel(stack, (Level)null, (LivingEntity)null, 0)); + renderItemModelIntoGUI(matrixStack, stack, x, y, z, Minecraft.getInstance().getItemRenderer().getModel(stack, (Level) null, (LivingEntity) null, 0)); + } + + public static void renderItemAdvanced(PoseStack matrixStack, ItemStack stack, int x, int y, int z, String amount) { + renderItemModelIntoGUI(matrixStack, stack, x, y, z, Minecraft.getInstance().getItemRenderer().getModel(stack, (Level) null, (LivingEntity) null, 0)); + renderItemStackOverlay(matrixStack, Minecraft.getInstance().font, stack, x, y, amount, amount.length() - 2); + } + + public static void renderItemStackOverlay(PoseStack matrixStack, Font fr, ItemStack stack, int xPosition, int yPosition, @Nullable String text, int scaled) { + matrixStack.pushPose(); + matrixStack.translate(0, 0, 512 + 32); + if (!stack.isEmpty()) { + if (stack.getCount() != 1 || text != null) { + String s = text == null ? String.valueOf(stack.getCount()) : text; + if (text == null && stack.getCount() < 1) { + ChatFormatting var10000 = ChatFormatting.RED; + s = var10000 + String.valueOf(stack.getCount()); + } + + matrixStack.translate(0.0D, 0.0D, (double) (Minecraft.getInstance().getItemRenderer().blitOffset + 200.0F)); + RenderSystem.disableDepthTest(); + RenderSystem.disableBlend(); + if (scaled >= 2) { + matrixStack.pushPose(); + matrixStack.scale(0.5F, 0.5F, 0.5F); + fr.drawShadow(matrixStack, s, (float) ((xPosition + 19 - 2) * 2 - 1 - fr.width(s)), (float) (yPosition * 2 + 24), 16777215); + matrixStack.popPose(); + } else if (scaled == 1) { + matrixStack.pushPose(); + matrixStack.scale(0.75F, 0.75F, 0.75F); + fr.drawShadow(matrixStack, s, (float) (xPosition - 2) * 1.34F + 24.0F - (float) fr.width(s), (float) yPosition * 1.34F + 14.0F, 16777215); + matrixStack.popPose(); + } else { + fr.drawShadow(matrixStack, s, (float) (xPosition + 19 - 2 - fr.width(s)), (float) (yPosition + 6 + 3), 16777215); + } + + RenderSystem.enableDepthTest(); + RenderSystem.enableBlend(); + } + } + matrixStack.popPose(); } @OnlyIn(Dist.CLIENT) @@ -41,7 +82,7 @@ public class TooltipUtil { RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); posestack.pushPose(); - posestack.translate((double)x, (double)y, (double)z); + posestack.translate((double) x, (double) y, (double) z); posestack.translate(8.0D, 8.0D, 0.0D); posestack.scale(1.0F, -1.0F, 1.0F); posestack.scale(16.0F, 16.0F, 16.0F);