mirror of
https://github.com/Ellpeck/ActuallyAdditions.git
synced 2024-11-26 08:48:34 +01:00
Back to CustomEnergyStorage
This commit is contained in:
parent
e22b1615a7
commit
555b1ddffb
3 changed files with 8 additions and 5 deletions
|
@ -3,6 +3,7 @@ package de.ellpeck.actuallyadditions.mod;
|
||||||
import de.ellpeck.actuallyadditions.mod.attachments.ActuallyAttachments;
|
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 de.ellpeck.actuallyadditions.mod.tile.CustomEnergyStorage;
|
||||||
import net.minecraft.core.registries.Registries;
|
import net.minecraft.core.registries.Registries;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.world.item.CreativeModeTab;
|
import net.minecraft.world.item.CreativeModeTab;
|
||||||
|
@ -34,7 +35,8 @@ public class ActuallyTabs {
|
||||||
.filter(stack -> stack.getItem() instanceof ItemEnergy).toList();
|
.filter(stack -> stack.getItem() instanceof ItemEnergy).toList();
|
||||||
charged.forEach(stack -> {
|
charged.forEach(stack -> {
|
||||||
if(stack.getItem() instanceof ItemEnergy itemEnergy) {
|
if(stack.getItem() instanceof ItemEnergy itemEnergy) {
|
||||||
EnergyStorage storage = new EnergyStorage(itemEnergy.maxPower, itemEnergy.transfer, itemEnergy.transfer, itemEnergy.maxPower);
|
CustomEnergyStorage storage = new CustomEnergyStorage(itemEnergy.maxPower, itemEnergy.transfer, itemEnergy.transfer);
|
||||||
|
storage.setEnergyStored(itemEnergy.maxPower);
|
||||||
stack.setData(ActuallyAttachments.ENERGY_STORAGE.get(), storage);
|
stack.setData(ActuallyAttachments.ENERGY_STORAGE.get(), storage);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -15,14 +15,14 @@ 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<EnergyStorage>> ENERGY_STORAGE = ATTACHMENT_TYPES.register(
|
public static final Supplier<AttachmentType<CustomEnergyStorage>> ENERGY_STORAGE = ATTACHMENT_TYPES.register(
|
||||||
"energy", ActuallyAttachments.itemEnergyStorageAttachment());
|
"energy", ActuallyAttachments.itemEnergyStorageAttachment());
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This is a supplier for an attachment type that can be used to attach an energy storage to an item.
|
* 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
|
* 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() {
|
public static Supplier<AttachmentType<CustomEnergyStorage>> itemEnergyStorageAttachment() {
|
||||||
return () -> AttachmentType.serializable(holder -> {
|
return () -> AttachmentType.serializable(holder -> {
|
||||||
if (holder instanceof ItemStack itemStack) {
|
if (holder instanceof ItemStack itemStack) {
|
||||||
int capacity = 1000;
|
int capacity = 1000;
|
||||||
|
@ -33,7 +33,7 @@ public class ActuallyAttachments {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
return new EnergyStorage(capacity, maxTransfer, maxTransfer);
|
return new CustomEnergyStorage(capacity, maxTransfer, maxTransfer);
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalStateException("Cannot attach energy handler item to a non-item.");
|
throw new IllegalStateException("Cannot attach energy handler item to a non-item.");
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,7 @@ import net.neoforged.api.distmarker.Dist;
|
||||||
import net.neoforged.api.distmarker.OnlyIn;
|
import net.neoforged.api.distmarker.OnlyIn;
|
||||||
import net.neoforged.fml.loading.FMLEnvironment;
|
import net.neoforged.fml.loading.FMLEnvironment;
|
||||||
import net.neoforged.neoforge.capabilities.Capabilities;
|
import net.neoforged.neoforge.capabilities.Capabilities;
|
||||||
|
import net.neoforged.neoforge.energy.EnergyStorage;
|
||||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||||
|
|
||||||
import javax.annotation.Nullable;
|
import javax.annotation.Nullable;
|
||||||
|
@ -119,7 +120,7 @@ public abstract class ItemEnergy extends ItemBase {
|
||||||
|
|
||||||
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))
|
return Optional.ofNullable(stack.getCapability(Capabilities.EnergyStorage.ITEM))
|
||||||
.map(cap -> cap instanceof CustomEnergyStorage
|
.map(cap -> cap instanceof EnergyStorage
|
||||||
? ((CustomEnergyStorage) cap).extractEnergyInternal(maxExtract, simulate)
|
? ((CustomEnergyStorage) cap).extractEnergyInternal(maxExtract, simulate)
|
||||||
: 0)
|
: 0)
|
||||||
.orElse(0);
|
.orElse(0);
|
||||||
|
|
Loading…
Reference in a new issue