From 86014001ba4e3e2f1527f1d1fbac4f33a7c9d472 Mon Sep 17 00:00:00 2001 From: drcrazy Date: Fri, 18 Sep 2020 20:40:42 +0300 Subject: [PATCH] Batteries should be able to charge items when active. Closes #2099 --- .../java/techreborn/items/BatteryItem.java | 44 +++++++++++++++++++ .../techreborn/items/armor/BatpackItem.java | 20 ++------- 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/main/java/techreborn/items/BatteryItem.java b/src/main/java/techreborn/items/BatteryItem.java index 72a07b8e7..be3af452c 100644 --- a/src/main/java/techreborn/items/BatteryItem.java +++ b/src/main/java/techreborn/items/BatteryItem.java @@ -24,10 +24,21 @@ package techreborn.items; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.client.item.TooltipContext; +import net.minecraft.entity.Entity; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraft.item.ItemStack; +import net.minecraft.text.Text; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.TypedActionResult; import net.minecraft.util.collection.DefaultedList; +import net.minecraft.world.World; +import org.jetbrains.annotations.Nullable; import reborncore.common.powerSystem.PowerSystem; import reborncore.common.util.ItemDurabilityExtensions; import reborncore.common.util.ItemUtils; @@ -35,6 +46,9 @@ import team.reborn.energy.EnergyHolder; import team.reborn.energy.EnergyTier; import techreborn.TechReborn; import techreborn.utils.InitUtils; +import techreborn.utils.MessageIDs; + +import java.util.List; public class BatteryItem extends Item implements EnergyHolder, ItemDurabilityExtensions { @@ -48,6 +62,36 @@ public class BatteryItem extends Item implements EnergyHolder, ItemDurabilityExt } // Item + @Override + public TypedActionResult use(final World world, final PlayerEntity player, final Hand hand) { + final ItemStack stack = player.getStackInHand(hand); + if (player.isSneaking()) { + ItemUtils.switchActive(stack, 1, world.isClient, MessageIDs.poweredToolID); + return new TypedActionResult<>(ActionResult.SUCCESS, stack); + } + return new TypedActionResult<>(ActionResult.PASS, stack); + } + + @Override + public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) { + ItemUtils.checkActive(stack, 1, entity.world.isClient, MessageIDs.poweredToolID); + if (world.isClient) { + return; + } + if (!ItemUtils.isActive(stack)){ + return; + } + if (entity instanceof PlayerEntity) { + ItemUtils.distributePowerToInventory((PlayerEntity) entity, stack, tier.getMaxOutput()); + } + } + + @Environment(EnvType.CLIENT) + @Override + public void appendTooltip(ItemStack stack, @Nullable World worldIn, List tooltip, TooltipContext flagIn) { + ItemUtils.buildActiveTooltip(stack, tooltip); + } + @Override public void appendStacks(ItemGroup group, DefaultedList stacks) { if (!isIn(group)) { diff --git a/src/main/java/techreborn/items/armor/BatpackItem.java b/src/main/java/techreborn/items/armor/BatpackItem.java index 92517241d..0915bfa35 100644 --- a/src/main/java/techreborn/items/armor/BatpackItem.java +++ b/src/main/java/techreborn/items/armor/BatpackItem.java @@ -38,7 +38,6 @@ import net.minecraft.world.World; import reborncore.common.powerSystem.PowerSystem; import reborncore.common.util.ItemDurabilityExtensions; import reborncore.common.util.ItemUtils; -import team.reborn.energy.Energy; import team.reborn.energy.EnergyHolder; import team.reborn.energy.EnergyTier; import techreborn.TechReborn; @@ -55,25 +54,14 @@ public class BatpackItem extends ArmorItem implements EnergyHolder, ItemDurabili this.tier = tier; } - private void distributePowerToInventory(World world, PlayerEntity player, ItemStack itemStack, int maxOutput) { - if (world.isClient || !Energy.valid(itemStack)) { - return; - } - - for (int i = 0; i < player.inventory.size(); i++) { - if (Energy.valid(player.inventory.getStack(i))) { - Energy.of(itemStack) - .into(Energy.of(player.inventory.getStack(i))) - .move(maxOutput); - } - } - } - // Item @Override public void inventoryTick(ItemStack stack, World worldIn, Entity entityIn, int itemSlot, boolean isSelected) { + if (worldIn.isClient) { + return; + } if (entityIn instanceof PlayerEntity) { - distributePowerToInventory(worldIn, (PlayerEntity) entityIn, stack, tier.getMaxOutput()); + ItemUtils.distributePowerToInventory((PlayerEntity) entityIn, stack, tier.getMaxOutput()); } }