Make energy tools usable while charging, fixes #2232 (#2733)

This commit is contained in:
vladislemon 2022-01-08 23:39:17 +03:00 committed by GitHub
parent a856fb0a8a
commit a2ea684c57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View file

@ -1,5 +1,10 @@
package reborncore.common.powerSystem; package reborncore.common.powerSystem;
import net.fabricmc.fabric.api.item.v1.FabricItem;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Hand;
import reborncore.common.util.ItemUtils;
import team.reborn.energy.api.base.SimpleBatteryItem; import team.reborn.energy.api.base.SimpleBatteryItem;
/** /**
@ -10,7 +15,7 @@ import team.reborn.energy.api.base.SimpleBatteryItem;
* </ul> * </ul>
* TODO: consider moving this functionality to the energy API? * TODO: consider moving this functionality to the energy API?
*/ */
public interface RcEnergyItem extends SimpleBatteryItem { public interface RcEnergyItem extends SimpleBatteryItem, FabricItem {
long getEnergyCapacity(); long getEnergyCapacity();
/** /**
@ -25,4 +30,14 @@ public interface RcEnergyItem extends SimpleBatteryItem {
default long getEnergyMaxOutput() { default long getEnergyMaxOutput() {
return getTier().getMaxOutput(); return getTier().getMaxOutput();
} }
@Override
default boolean allowNbtUpdateAnimation(PlayerEntity player, Hand hand, ItemStack oldStack, ItemStack newStack) {
return !ItemUtils.isEqualIgnoreEnergy(oldStack, newStack);
}
@Override
default boolean allowContinuingBlockBreaking(PlayerEntity player, ItemStack oldStack, ItemStack newStack) {
return ItemUtils.isEqualIgnoreEnergy(oldStack, newStack);
}
} }

View file

@ -38,6 +38,7 @@ import reborncore.common.powerSystem.RcEnergyItem;
import reborncore.common.recipes.IRecipeInput; import reborncore.common.recipes.IRecipeInput;
import team.reborn.energy.api.EnergyStorage; import team.reborn.energy.api.EnergyStorage;
import team.reborn.energy.api.EnergyStorageUtil; import team.reborn.energy.api.EnergyStorageUtil;
import team.reborn.energy.api.base.SimpleBatteryItem;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -76,6 +77,29 @@ public class ItemUtils {
return false; return false;
} }
public static boolean isEqualIgnoreEnergy(ItemStack stack1, ItemStack stack2) {
if (stack1 == stack2) {
return true;
}
if (!stack1.isOf(stack2.getItem())) {
return false;
}
if (stack1.getCount() != stack2.getCount()) {
return false;
}
if (stack1.getNbt() == stack2.getNbt()) {
return true;
}
if (stack1.getNbt() == null || stack2.getNbt() == null) {
return false;
}
NbtCompound nbt1Copy = stack1.getNbt().copy();
NbtCompound nbt2Copy = stack2.getNbt().copy();
nbt1Copy.remove(SimpleBatteryItem.ENERGY_KEY);
nbt2Copy.remove(SimpleBatteryItem.ENERGY_KEY);
return nbt1Copy.equals(nbt2Copy);
}
//TODO tags //TODO tags
public static boolean isInputEqual(Object input, ItemStack other, boolean matchNBT, public static boolean isInputEqual(Object input, ItemStack other, boolean matchNBT,
boolean useTags) { boolean useTags) {