mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-26 08:48:34 +01:00
Fix ItemEnergy
This commit is contained in:
parent
38f4824757
commit
24763a46bc
3 changed files with 61 additions and 25 deletions
|
@ -1,5 +1,6 @@
|
||||||
package de.ellpeck.actuallyadditions.mod;
|
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.ActuallyItems;
|
||||||
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
|
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
|
||||||
import net.minecraft.core.registries.Registries;
|
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.CreativeModeTabs;
|
||||||
import net.minecraft.world.item.ItemStack;
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.neoforged.bus.api.IEventBus;
|
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 net.neoforged.neoforge.registries.DeferredRegister;
|
||||||
|
|
||||||
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Supplier;
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class ActuallyTabs {
|
public class ActuallyTabs {
|
||||||
public static final DeferredRegister<CreativeModeTab> CREATIVE_MODE_TABS = DeferredRegister.create(Registries.CREATIVE_MODE_TAB, ActuallyAdditions.MODID);
|
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)
|
.withTabsBefore(CreativeModeTabs.SPAWN_EGGS)
|
||||||
.title(Component.translatable("itemGroup.actuallyadditions"))
|
.title(Component.translatable("itemGroup.actuallyadditions"))
|
||||||
.displayItems((parameters, output) -> {
|
.displayItems((parameters, output) -> {
|
||||||
List<ItemStack> stacks = ActuallyItems.ITEMS.getEntries().stream().map(reg -> new ItemStack(reg.get())).toList();
|
List<ItemStack> stacks = ActuallyItems.ITEMS.getEntries().stream().map(reg -> new ItemStack(reg.get())).collect(Collectors.toList());
|
||||||
stacks.forEach(stack -> {
|
|
||||||
|
//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) {
|
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);
|
output.acceptAll(stacks);
|
||||||
}).build());
|
}).build());
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,12 @@
|
||||||
package de.ellpeck.actuallyadditions.mod.attachments;
|
package de.ellpeck.actuallyadditions.mod.attachments;
|
||||||
|
|
||||||
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
|
||||||
|
import de.ellpeck.actuallyadditions.mod.items.base.ItemEnergy;
|
||||||
import de.ellpeck.actuallyadditions.mod.tile.CustomEnergyStorage;
|
import de.ellpeck.actuallyadditions.mod.tile.CustomEnergyStorage;
|
||||||
|
import net.minecraft.world.item.ItemStack;
|
||||||
import net.neoforged.bus.api.IEventBus;
|
import net.neoforged.bus.api.IEventBus;
|
||||||
import net.neoforged.neoforge.attachment.AttachmentType;
|
import net.neoforged.neoforge.attachment.AttachmentType;
|
||||||
|
import net.neoforged.neoforge.energy.EnergyStorage;
|
||||||
import net.neoforged.neoforge.registries.DeferredRegister;
|
import net.neoforged.neoforge.registries.DeferredRegister;
|
||||||
import net.neoforged.neoforge.registries.NeoForgeRegistries;
|
import net.neoforged.neoforge.registries.NeoForgeRegistries;
|
||||||
|
|
||||||
|
@ -12,8 +15,30 @@ import java.util.function.Supplier;
|
||||||
public class ActuallyAttachments {
|
public class ActuallyAttachments {
|
||||||
private static final DeferredRegister<AttachmentType<?>> ATTACHMENT_TYPES = DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, ActuallyAdditions.MODID);
|
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(
|
public static final Supplier<AttachmentType<EnergyStorage>> ENERGY_STORAGE = ATTACHMENT_TYPES.register(
|
||||||
"energy", () -> AttachmentType.serializable((holder) -> new CustomEnergyStorage(250000, 1000, 1000)).build());
|
"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) {
|
public static void init(IEventBus evt) {
|
||||||
ATTACHMENT_TYPES.register(evt);
|
ATTACHMENT_TYPES.register(evt);
|
||||||
|
|
|
@ -33,8 +33,8 @@ import java.util.Optional;
|
||||||
|
|
||||||
public abstract class ItemEnergy extends ItemBase {
|
public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
private final int maxPower;
|
public final int maxPower;
|
||||||
private final int transfer;
|
public final int transfer;
|
||||||
|
|
||||||
public ItemEnergy(int maxPower, int transfer) {
|
public ItemEnergy(int maxPower, int transfer) {
|
||||||
super(ActuallyItems.defaultProps().stacksTo(1));
|
super(ActuallyItems.defaultProps().stacksTo(1));
|
||||||
|
@ -51,12 +51,12 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
@Override
|
@Override
|
||||||
public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
|
public void appendHoverText(ItemStack stack, @Nullable Level worldIn, List<Component> tooltip, TooltipFlag flagIn) {
|
||||||
super.appendHoverText(stack, worldIn, tooltip, flagIn);
|
super.appendHoverText(stack, worldIn, tooltip, flagIn);
|
||||||
int energy = 0;
|
IEnergyStorage storage = stack.getCapability(Capabilities.EnergyStorage.ITEM);
|
||||||
if (stack.hasTag() && stack.getTag().contains("Energy")) {
|
if(storage != null) {
|
||||||
energy = stack.getTag().getInt("Energy");
|
int energy = storage.getEnergyStored();
|
||||||
|
NumberFormat format = NumberFormat.getInstance();
|
||||||
|
tooltip.add(Component.translatable("misc.actuallyadditions.power_long", format.format(energy), format.format(storage.getMaxEnergyStored())));
|
||||||
}
|
}
|
||||||
NumberFormat format = NumberFormat.getInstance();
|
|
||||||
tooltip.add(Component.translatable("misc.actuallyadditions.power_long", format.format(energy), format.format(this.maxPower)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -83,11 +83,11 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getBarWidth(ItemStack stack) {
|
public int getBarWidth(ItemStack stack) {
|
||||||
if (stack.hasData(ActuallyAttachments.ENERGY_STORAGE)) {
|
IEnergyStorage storage = stack.getCapability(Capabilities.EnergyStorage.ITEM);
|
||||||
CustomEnergyStorage storage = stack.getData(ActuallyAttachments.ENERGY_STORAGE);
|
if (storage != null) {
|
||||||
return Math.round(13.0F - (float)storage.getEnergyStored() * 13.0F / storage.getMaxEnergyStored());
|
return Math.round((13.0F / storage.getMaxEnergyStored() * storage.getEnergyStored()));
|
||||||
}
|
}
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -103,7 +103,7 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setEnergy(ItemStack stack, int energy) {
|
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) {
|
if (cap instanceof CustomEnergyStorage) {
|
||||||
((CustomEnergyStorage) cap).setEnergyStored(energy);
|
((CustomEnergyStorage) cap).setEnergyStored(energy);
|
||||||
}
|
}
|
||||||
|
@ -112,13 +112,13 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public int receiveEnergyInternal(ItemStack stack, int maxReceive, boolean simulate) {
|
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))
|
.map(cap -> ((CustomEnergyStorage) cap).receiveEnergyInternal(maxReceive, simulate))
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int extractEnergyInternal(ItemStack stack, int maxExtract, boolean simulate) {
|
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
|
.map(cap -> cap instanceof CustomEnergyStorage
|
||||||
? ((CustomEnergyStorage) cap).extractEnergyInternal(maxExtract, simulate)
|
? ((CustomEnergyStorage) cap).extractEnergyInternal(maxExtract, simulate)
|
||||||
: 0)
|
: 0)
|
||||||
|
@ -127,32 +127,30 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public int receiveEnergy(ItemStack stack, int maxReceive, boolean simulate) {
|
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))
|
.map(cap -> cap.receiveEnergy(maxReceive, simulate))
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int extractEnergy(ItemStack stack, int maxExtract, boolean simulate) {
|
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))
|
.map(cap -> cap.extractEnergy(maxExtract, simulate))
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getEnergyStored(ItemStack stack) {
|
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)
|
.map(IEnergyStorage::getEnergyStored)
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getMaxEnergyStored(ItemStack stack) {
|
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)
|
.map(IEnergyStorage::getMaxEnergyStored)
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnergyStorage getEnergyStorage(ItemStack stack) {
|
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);
|
return stack.getData(ActuallyAttachments.ENERGY_STORAGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue