Batteries should be able to charge items when active. Closes #2099

This commit is contained in:
drcrazy 2020-09-18 20:40:42 +03:00
parent 0b13213489
commit 86014001ba
2 changed files with 48 additions and 16 deletions

View file

@ -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<ItemStack> 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<Text> tooltip, TooltipContext flagIn) {
ItemUtils.buildActiveTooltip(stack, tooltip);
}
@Override
public void appendStacks(ItemGroup group, DefaultedList<ItemStack> stacks) {
if (!isIn(group)) {

View file

@ -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());
}
}