Fix ItemEnergy

This commit is contained in:
Mrbysco 2024-03-07 00:18:32 +01:00
parent 38f4824757
commit 24763a46bc
3 changed files with 61 additions and 25 deletions

View file

@ -1,5 +1,6 @@
package de.ellpeck.actuallyadditions.mod;
import de.ellpeck.actuallyadditions.mod.attachments.ActuallyAttachments;
import de.ellpeck.actuallyadditions.mod.items.ActuallyItems;
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
import net.minecraft.core.registries.Registries;
@ -8,10 +9,15 @@ import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.capabilities.Capabilities;
import net.neoforged.neoforge.energy.EnergyStorage;
import net.neoforged.neoforge.energy.IEnergyStorage;
import net.neoforged.neoforge.registries.DeferredRegister;
import java.util.Iterator;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
public class ActuallyTabs {
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, ActuallyAdditions.MODID);
@ -21,12 +27,19 @@ public class ActuallyTabs {
.withTabsBefore(CreativeModeTabs.SPAWN_EGGS)
.title(Component.translatable("itemGroup.actuallyadditions"))
.displayItems((parameters, output) -> {
List<ItemStack> stacks = ActuallyItems.ITEMS.getEntries().stream().map(reg -> new ItemStack(reg.get())).toList();
stacks.forEach(stack -> {
List<ItemStack> stacks = ActuallyItems.ITEMS.getEntries().stream().map(reg -> new ItemStack(reg.get())).collect(Collectors.toList());
//Add charged versions of all energy items
List<ItemStack> charged = ActuallyItems.ITEMS.getEntries().stream().map(reg -> new ItemStack(reg.get()))
.filter(stack -> stack.getItem() instanceof ItemEnergy).toList();
charged.forEach(stack -> {
if(stack.getItem() instanceof ItemEnergy itemEnergy) {
stack.getOrCreateTag().putDouble("Energy", itemEnergy.getMaxEnergyStored(stack));
EnergyStorage storage = new EnergyStorage(itemEnergy.maxPower, itemEnergy.transfer, itemEnergy.transfer, itemEnergy.maxPower);
stack.setData(ActuallyAttachments.ENERGY_STORAGE.get(), storage);
}
});
stacks.addAll(charged);
output.acceptAll(stacks);
}).build());

View file

@ -1,9 +1,12 @@
package de.ellpeck.actuallyadditions.mod.attachments;
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
import de.ellpeck.actuallyadditions.mod.tile.CustomEnergyStorage;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.energy.EnergyStorage;
import net.neoforged.neoforge.registries.DeferredRegister;
import net.neoforged.neoforge.registries.NeoForgeRegistries;
@ -12,8 +15,30 @@ import java.util.function.Supplier;
public class ActuallyAttachments {
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, ActuallyAdditions.MODID);
public static final Supplier<AttachmentType<CustomEnergyStorage>> ENERGY_STORAGE = ATTACHMENT_TYPES.register(
"energy", () -> AttachmentType.serializable((holder) -> new CustomEnergyStorage(250000, 1000, 1000)).build());
public static final Supplier<AttachmentType<EnergyStorage>> ENERGY_STORAGE = ATTACHMENT_TYPES.register(
"energy", ActuallyAttachments.itemEnergyStorageAttachment());
/*
* This is a supplier for an attachment type that can be used to attach an energy storage to an item.
* Implementation is based on EnderIO's https://github.com/Team-EnderIO/EnderIO/blob/e1f022df745131ed5fea718bd860880a5785d4c7/src/core/java/com/enderio/core/common/attachment/AttachmentUtil.java#L47-L60
*/
public static Supplier<AttachmentType<EnergyStorage>> itemEnergyStorageAttachment() {
return () -> AttachmentType.serializable(holder -> {
if (holder instanceof ItemStack itemStack) {
int capacity = 1000;
int maxTransfer = 1000;
if (itemStack.getItem() instanceof ItemEnergy itemEnergy) {
capacity = itemEnergy.maxPower;
maxTransfer = itemEnergy.transfer;
}
return new EnergyStorage(capacity, maxTransfer, maxTransfer);
} else {
throw new IllegalStateException("Cannot attach energy handler item to a non-item.");
}
}).build();
}
public static void init(IEventBus evt) {
ATTACHMENT_TYPES.register(evt);

View file

@ -33,8 +33,8 @@ import java.util.Optional;
public abstract class ItemEnergy extends ItemBase {
private final int maxPower;
private final int transfer;
public final int maxPower;
public final int transfer;
public ItemEnergy(int maxPower, int transfer) {
super(ActuallyItems.defaultProps().stacksTo(1));
@ -51,12 +51,12 @@ public abstract class ItemEnergy extends ItemBase {
@Override
public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
super.appendHoverText(stack, worldIn, tooltip, flagIn);
int energy = 0;
if (stack.hasTag() && stack.getTag().contains("Energy")) {
energy = stack.getTag().getInt("Energy");
}
IEnergyStorage storage = stack.getCapability(Capabilities.EnergyStorage.ITEM);
if(storage != null) {
int energy = storage.getEnergyStored();
NumberFormat format = NumberFormat.getInstance();
tooltip.add(Component.translatable("misc.actuallyadditions.power_long", format.format(energy), format.format(this.maxPower)));
tooltip.add(Component.translatable("misc.actuallyadditions.power_long", format.format(energy), format.format(storage.getMaxEnergyStored())));
}
}
@Override
@ -83,11 +83,11 @@ public abstract class ItemEnergy extends ItemBase {
@Override
public int getBarWidth(ItemStack stack) {
if (stack.hasData(ActuallyAttachments.ENERGY_STORAGE)) {
CustomEnergyStorage storage = stack.getData(ActuallyAttachments.ENERGY_STORAGE);
return Math.round(13.0F - (float)storage.getEnergyStored() * 13.0F / storage.getMaxEnergyStored());
IEnergyStorage storage = stack.getCapability(Capabilities.EnergyStorage.ITEM);
if (storage != null) {
return Math.round((13.0F / storage.getMaxEnergyStored() * storage.getEnergyStored()));
}
return 1;
return 0;
}
@Override
@ -103,7 +103,7 @@ public abstract class ItemEnergy extends ItemBase {
}
public void setEnergy(ItemStack stack, int energy) {
Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null)).ifPresent(cap -> {
Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM)).ifPresent(cap -> {
if (cap instanceof CustomEnergyStorage) {
((CustomEnergyStorage) cap).setEnergyStored(energy);
}
@ -112,13 +112,13 @@ public abstract class ItemEnergy extends ItemBase {
@Deprecated
public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate) {
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null))
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
.map(cap -> ((CustomEnergyStorage) cap).receiveEnergyInternal(maxReceive, simulate))
.orElse(0);
}
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate) {
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null))
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
.map(cap -> cap instanceof CustomEnergyStorage
? ((CustomEnergyStorage) cap).extractEnergyInternal(maxExtract, simulate)
: 0)
@ -127,32 +127,30 @@ public abstract class ItemEnergy extends ItemBase {
@Deprecated
public int receiveEnergy(ItemStack stack, int maxReceive, boolean simulate) {
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null))
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
.map(cap -> cap.receiveEnergy(maxReceive, simulate))
.orElse(0);
}
public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) {
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null))
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
.map(cap -> cap.extractEnergy(maxExtract, simulate))
.orElse(0);
}
public int getEnergyStored(ItemStack stack) {
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null))
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
.map(IEnergyStorage::getEnergyStored)
.orElse(0);
}
public int getMaxEnergyStored(ItemStack stack) {
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM, null))
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
.map(IEnergyStorage::getMaxEnergyStored)
.orElse(0);
}
public IEnergyStorage getEnergyStorage(ItemStack stack) {
if (!stack.hasData(ActuallyAttachments.ENERGY_STORAGE))
stack.setData(ActuallyAttachments.ENERGY_STORAGE, new CustomEnergyStorage(this.maxPower, this.transfer, this.transfer));
return stack.getData(ActuallyAttachments.ENERGY_STORAGE);
}