正确注册部分Capability
This commit is contained in:
parent
314f461d2c
commit
4e560d22ed
7 changed files with 120 additions and 166 deletions
|
@ -13,7 +13,6 @@ import net.minecraft.nbt.CompoundTag;
|
|||
import net.minecraft.nbt.IntTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.Container;
|
||||
import net.minecraft.world.ContainerHelper;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
|
@ -30,7 +29,6 @@ import net.minecraft.world.level.Level;
|
|||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.neoforged.neoforge.capabilities.BlockCapabilityCache;
|
||||
import net.neoforged.neoforge.capabilities.Capabilities;
|
||||
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
|
||||
import net.neoforged.neoforge.energy.EnergyStorage;
|
||||
|
@ -61,23 +59,6 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
protected NonNullList<ItemStack> items = NonNullList.withSize(2, ItemStack.EMPTY);
|
||||
|
||||
|
||||
private BlockCapabilityCache<IEnergyStorage, @Nullable Direction> capCache;
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
super.onLoad();
|
||||
|
||||
if (level != null && !level.isClientSide) {
|
||||
this.capCache = BlockCapabilityCache.create(
|
||||
Capabilities.EnergyStorage.BLOCK,
|
||||
(ServerLevel) level,
|
||||
this.getBlockPos(),
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public int fuelTick = 0;
|
||||
public int maxFuelTick = DEFAULT_FUEL_TIME;
|
||||
public boolean showRange = false;
|
||||
|
@ -139,7 +120,7 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
setChanged(pLevel, pPos, pState);
|
||||
}
|
||||
|
||||
var handler = blockEntity.capCache.getCapability();
|
||||
var handler = blockEntity.getEnergyStorage(null);
|
||||
if (handler == null) return;
|
||||
|
||||
int energy = handler.getEnergyStored();
|
||||
|
@ -256,10 +237,10 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
|
||||
for (Direction direction : Direction.values()) {
|
||||
var blockEntity = this.level.getBlockEntity(this.getBlockPos().relative(direction));
|
||||
if (blockEntity == null) return;
|
||||
if (blockEntity == null) continue;
|
||||
|
||||
var energy = level.getCapability(Capabilities.EnergyStorage.BLOCK, blockEntity.getBlockPos(), direction);
|
||||
if (energy == null || !(blockEntity instanceof ChargingStationBlockEntity)) return;
|
||||
if (energy == null || blockEntity instanceof ChargingStationBlockEntity) return;
|
||||
|
||||
if (energy.canReceive() && energy.getEnergyStored() < energy.getMaxEnergyStored()) {
|
||||
int receiveEnergy = energy.receiveEnergy(Math.min(handler.getEnergyStored(), CHARGE_OTHER_SPEED), false);
|
||||
|
@ -279,10 +260,10 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
protected void loadAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) {
|
||||
super.loadAdditional(tag, registries);
|
||||
|
||||
if (tag.contains("Energy") && this.capCache.getCapability() != null) {
|
||||
if (tag.contains("Energy")) {
|
||||
var energy = tag.get("Energy");
|
||||
if (energy instanceof IntTag) {
|
||||
((EnergyStorage) this.capCache.getCapability()).deserializeNBT(registries, energy);
|
||||
((EnergyStorage) this.energyStorage).deserializeNBT(registries, energy);
|
||||
}
|
||||
}
|
||||
this.fuelTick = tag.getInt("FuelTick");
|
||||
|
@ -296,9 +277,8 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
protected void saveAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) {
|
||||
super.saveAdditional(tag, registries);
|
||||
|
||||
if (this.capCache != null && this.capCache.getCapability() != null) {
|
||||
tag.put("Energy", IntTag.valueOf(this.capCache.getCapability().getEnergyStored()));
|
||||
}
|
||||
tag.putInt("Energy", this.energyStorage.getEnergyStored());
|
||||
|
||||
tag.putInt("FuelTick", this.fuelTick);
|
||||
tag.putInt("MaxFuelTick", this.maxFuelTick);
|
||||
tag.putBoolean("ShowRange", this.showRange);
|
||||
|
@ -399,17 +379,6 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
return compoundtag;
|
||||
}
|
||||
|
||||
public static class EnergyStorageProvider implements ICapabilityProvider<ChargingStationBlockEntity, Direction, IEnergyStorage> {
|
||||
|
||||
private final IEnergyStorage energy = new EnergyStorage(MAX_ENERGY);
|
||||
|
||||
@Override
|
||||
public @Nullable IEnergyStorage getCapability(@NotNull ChargingStationBlockEntity object, Direction context) {
|
||||
return energy;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static class ItemHandlerProvider implements ICapabilityProvider<ChargingStationBlockEntity, Direction, IItemHandler> {
|
||||
|
||||
private IItemHandler[] itemHandlers;
|
||||
|
@ -439,12 +408,15 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
public void saveToItem(ItemStack stack, HolderLookup.Provider registries) {
|
||||
CompoundTag tag = new CompoundTag();
|
||||
if (this.level != null) {
|
||||
var cap = level.getCapability(Capabilities.EnergyStorage.BLOCK, this.getBlockPos(), null);
|
||||
if (cap instanceof EnergyStorage energy) {
|
||||
tag.put("Energy", energy.serializeNBT(registries));
|
||||
}
|
||||
tag.put("Energy", ((EnergyStorage) energyStorage).serializeNBT(registries));
|
||||
}
|
||||
BlockItem.setBlockEntityData(stack, this.getType(), tag);
|
||||
}
|
||||
|
||||
private final IEnergyStorage energyStorage = new EnergyStorage(MAX_ENERGY);
|
||||
|
||||
@Nullable
|
||||
public IEnergyStorage getEnergyStorage(@Nullable Direction side) {
|
||||
return energyStorage;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,16 +5,12 @@ import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
|||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
import net.minecraft.world.entity.Entity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.phys.AABB;
|
||||
import net.neoforged.neoforge.capabilities.BlockCapabilityCache;
|
||||
import net.neoforged.neoforge.capabilities.Capabilities;
|
||||
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
|
||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -26,21 +22,6 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
|
||||
public static final int CHARGE_RADIUS = 8;
|
||||
|
||||
private BlockCapabilityCache<IEnergyStorage, @Nullable Direction> capCache;
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
super.onLoad();
|
||||
|
||||
if (level != null && !level.isClientSide) {
|
||||
this.capCache = BlockCapabilityCache.create(
|
||||
Capabilities.EnergyStorage.BLOCK,
|
||||
(ServerLevel) level,
|
||||
this.getBlockPos(),
|
||||
null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public CreativeChargingStationBlockEntity(BlockPos pos, BlockState state) {
|
||||
super(ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pos, state);
|
||||
|
@ -49,11 +30,9 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
public static void serverTick(CreativeChargingStationBlockEntity blockEntity) {
|
||||
if (blockEntity.level == null) return;
|
||||
|
||||
if (blockEntity.capCache != null) {
|
||||
blockEntity.chargeEntity();
|
||||
blockEntity.chargeBlock();
|
||||
}
|
||||
}
|
||||
|
||||
private void chargeEntity() {
|
||||
if (this.level == null) return;
|
||||
|
@ -73,7 +52,7 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
|
||||
for (Direction direction : Direction.values()) {
|
||||
var blockEntity = this.level.getBlockEntity(this.getBlockPos().relative(direction));
|
||||
if (blockEntity == null) return;
|
||||
if (blockEntity == null) continue;
|
||||
|
||||
var energy = level.getCapability(Capabilities.EnergyStorage.BLOCK, blockEntity.getBlockPos(), direction);
|
||||
if (energy == null || blockEntity instanceof CreativeChargingStationBlockEntity) continue;
|
||||
|
@ -90,14 +69,10 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
|
|||
return ClientboundBlockEntityDataPacket.create(this);
|
||||
}
|
||||
|
||||
public static class EnergyStorageProvider implements ICapabilityProvider<CreativeChargingStationBlockEntity, Direction, IEnergyStorage> {
|
||||
private final IEnergyStorage energyStorage = new InfinityEnergyStorage();
|
||||
|
||||
private final IEnergyStorage energy = new InfinityEnergyStorage();
|
||||
|
||||
@Override
|
||||
public @Nullable IEnergyStorage getCapability(@NotNull CreativeChargingStationBlockEntity object, Direction context) {
|
||||
if (object.isRemoved()) return null;
|
||||
return energy;
|
||||
}
|
||||
@Nullable
|
||||
public IEnergyStorage getEnergyStorage(@Nullable Direction side) {
|
||||
return energyStorage;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package com.atsuishio.superbwarfare.block.entity;
|
||||
|
||||
import com.atsuishio.superbwarfare.block.FuMO25Block;
|
||||
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||
import com.atsuishio.superbwarfare.init.ModSounds;
|
||||
import com.atsuishio.superbwarfare.menu.FuMO25Menu;
|
||||
import com.atsuishio.superbwarfare.network.dataslot.ContainerEnergyData;
|
||||
import com.atsuishio.superbwarfare.tools.SeekTool;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.core.HolderLookup;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
|
||||
import net.minecraft.sounds.SoundSource;
|
||||
import net.minecraft.util.Mth;
|
||||
import net.minecraft.world.MenuProvider;
|
||||
import net.minecraft.world.effect.MobEffectInstance;
|
||||
|
@ -22,6 +26,8 @@ import net.minecraft.world.inventory.ContainerLevelAccess;
|
|||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.neoforged.neoforge.energy.EnergyStorage;
|
||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import software.bernie.geckolib.animatable.GeoBlockEntity;
|
||||
|
@ -29,8 +35,10 @@ import software.bernie.geckolib.animatable.instance.AnimatableInstanceCache;
|
|||
import software.bernie.geckolib.animation.AnimatableManager;
|
||||
import software.bernie.geckolib.util.GeckoLibUtil;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
import java.util.List;
|
||||
|
||||
// TODO 为什么打不开UI
|
||||
public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoBlockEntity {
|
||||
|
||||
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
|
||||
|
@ -49,7 +57,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
|
||||
public static final int MAX_DATA_COUNT = 5;
|
||||
|
||||
// private LazyOptional<EnergyStorage> energyHandler;
|
||||
private final IEnergyStorage energyStorage;
|
||||
|
||||
public FuncType type = FuncType.NORMAL;
|
||||
public int time = 0;
|
||||
|
@ -61,7 +69,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
@Override
|
||||
public int get(int pIndex) {
|
||||
return switch (pIndex) {
|
||||
// case 0 -> FuMO25BlockEntity.this.energyHandler.map(EnergyStorage::getEnergyStored).orElse(0);
|
||||
case 0 -> FuMO25BlockEntity.this.energyStorage.getEnergyStored();
|
||||
case 1 -> FuMO25BlockEntity.this.type.ordinal();
|
||||
case 2 -> FuMO25BlockEntity.this.time;
|
||||
case 3 -> FuMO25BlockEntity.this.powered ? 1 : 0;
|
||||
|
@ -73,9 +81,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
@Override
|
||||
public void set(int pIndex, int pValue) {
|
||||
switch (pIndex) {
|
||||
case 0 -> {
|
||||
}
|
||||
// FuMO25BlockEntity.this.energyHandler.ifPresent(handler -> handler.receiveEnergy((int) pValue, false));
|
||||
case 0 -> FuMO25BlockEntity.this.energyStorage.receiveEnergy(pValue, false);
|
||||
case 1 -> FuMO25BlockEntity.this.type = FuncType.values()[pValue];
|
||||
case 2 -> FuMO25BlockEntity.this.time = pValue;
|
||||
case 3 -> FuMO25BlockEntity.this.powered = pValue == 1;
|
||||
|
@ -91,13 +97,18 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
|
||||
public FuMO25BlockEntity(BlockPos pPos, BlockState pBlockState) {
|
||||
super(ModBlockEntities.FUMO_25.get(), pPos, pBlockState);
|
||||
// this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY));
|
||||
this.energyStorage = new EnergyStorage(MAX_ENERGY);
|
||||
}
|
||||
|
||||
public IEnergyStorage getEnergyStorage(@Nullable Direction direction) {
|
||||
return this.energyStorage;
|
||||
}
|
||||
|
||||
public static <T extends BlockEntity> void serverTick(Level pLevel, BlockPos pPos, BlockState pState, T blockEntity) {
|
||||
var radar = (FuMO25BlockEntity) blockEntity;
|
||||
|
||||
// int energy = radar.energyHandler.map(EnergyStorage::getEnergyStored).orElse(0);
|
||||
var energyStorage = ((FuMO25BlockEntity) blockEntity).getEnergyStorage(null);
|
||||
var energy = energyStorage.getEnergyStored();
|
||||
radar.tick++;
|
||||
|
||||
FuncType funcType = radar.type;
|
||||
|
@ -108,40 +119,40 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
energyCost = DEFAULT_ENERGY_COST;
|
||||
}
|
||||
|
||||
// if (energy < energyCost) {
|
||||
// if (pState.getValue(FuMO25Block.POWERED)) {
|
||||
// pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false));
|
||||
// pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_END.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||
// radar.powered = false;
|
||||
// setChanged(pLevel, pPos, pState);
|
||||
// }
|
||||
// if (radar.time > 0) {
|
||||
// radar.time = 0;
|
||||
// radar.setChanged();
|
||||
// }
|
||||
// } else {
|
||||
// if (!pState.getValue(FuMO25Block.POWERED)) {
|
||||
// if (energy >= DEFAULT_MIN_ENERGY) {
|
||||
// pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true));
|
||||
// pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_START.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||
// radar.powered = true;
|
||||
// setChanged(pLevel, pPos, pState);
|
||||
// }
|
||||
// } else {
|
||||
// radar.energyHandler.ifPresent(handler -> handler.extractEnergy(energyCost, false));
|
||||
// if (radar.tick == 200) {
|
||||
// pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_IDLE.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||
// }
|
||||
//
|
||||
// if (radar.time > 0) {
|
||||
// if (radar.time % 100 == 0) {
|
||||
// radar.setGlowEffect();
|
||||
// }
|
||||
// radar.time--;
|
||||
// radar.setChanged();
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
if (energy < energyCost) {
|
||||
if (pState.getValue(FuMO25Block.POWERED)) {
|
||||
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false));
|
||||
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_END.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||
radar.powered = false;
|
||||
setChanged(pLevel, pPos, pState);
|
||||
}
|
||||
if (radar.time > 0) {
|
||||
radar.time = 0;
|
||||
radar.setChanged();
|
||||
}
|
||||
} else {
|
||||
if (!pState.getValue(FuMO25Block.POWERED)) {
|
||||
if (energy >= DEFAULT_MIN_ENERGY) {
|
||||
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true));
|
||||
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_START.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||
radar.powered = true;
|
||||
setChanged(pLevel, pPos, pState);
|
||||
}
|
||||
} else {
|
||||
energyStorage.extractEnergy(energyCost, false);
|
||||
if (radar.tick == 200) {
|
||||
pLevel.playSound(null, pPos, ModSounds.RADAR_SEARCH_IDLE.get(), SoundSource.BLOCKS, 1.0F, 1.0F);
|
||||
}
|
||||
|
||||
if (radar.time > 0) {
|
||||
if (radar.time % 100 == 0) {
|
||||
radar.setGlowEffect();
|
||||
}
|
||||
radar.time--;
|
||||
radar.setChanged();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (radar.tick >= 200) {
|
||||
radar.tick = 0;
|
||||
|
@ -171,8 +182,9 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
protected void loadAdditional(@NotNull CompoundTag tag, HolderLookup.@NotNull Provider registries) {
|
||||
super.loadAdditional(tag, registries);
|
||||
|
||||
if (tag.contains("Energy")) {
|
||||
// getCapability(Capabilities.ENERGY).ifPresent(handler -> ((EnergyStorage) handler).deserializeNBT(tag.get("Energy")));
|
||||
var energyTag = tag.get("Energy");
|
||||
if (energyTag != null) {
|
||||
((EnergyStorage) energyStorage).deserializeNBT(registries, energyTag);
|
||||
}
|
||||
this.type = FuncType.values()[Mth.clamp(tag.getInt("Type"), 0, 3)];
|
||||
this.time = tag.getInt("Time");
|
||||
|
@ -180,10 +192,11 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
}
|
||||
|
||||
@Override
|
||||
@ParametersAreNonnullByDefault
|
||||
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
|
||||
super.saveAdditional(tag, registries);
|
||||
|
||||
// getCapability(Capabilities.ENERGY).ifPresent(handler -> tag.put("Energy", ((EnergyStorage) handler).serializeNBT()));
|
||||
tag.put("Energy", ((EnergyStorage) energyStorage).serializeNBT(registries));
|
||||
tag.putInt("Type", this.type.ordinal());
|
||||
tag.putInt("Time", this.time);
|
||||
tag.putBoolean("Powered", this.powered);
|
||||
|
@ -206,27 +219,6 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider, GeoB
|
|||
return ClientboundBlockEntityDataPacket.create(this);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
|
||||
// if (cap == Capabilities.ENERGY) {
|
||||
// return energyHandler.cast();
|
||||
// }
|
||||
// return super.getCapability(cap, side);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public void invalidateCapabilities() {
|
||||
// this.energyHandler.invalidate();
|
||||
// super.invalidateCapabilities();
|
||||
// }
|
||||
//
|
||||
//
|
||||
// @Override
|
||||
// public void reviveCaps() {
|
||||
// super.reviveCaps();
|
||||
// this.energyHandler = LazyOptional.of(() -> new EnergyStorage(MAX_ENERGY));
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void registerControllers(AnimatableManager.ControllerRegistrar controllers) {
|
||||
}
|
||||
|
|
|
@ -3,13 +3,14 @@ package com.atsuishio.superbwarfare.capability;
|
|||
import com.atsuishio.superbwarfare.Mod;
|
||||
import com.atsuishio.superbwarfare.block.entity.ChargingStationBlockEntity;
|
||||
import com.atsuishio.superbwarfare.block.entity.CreativeChargingStationBlockEntity;
|
||||
import com.atsuishio.superbwarfare.capability.energy.BlockEnergyStorageProvider;
|
||||
import com.atsuishio.superbwarfare.capability.energy.ItemEnergyProvider;
|
||||
import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity;
|
||||
import com.atsuishio.superbwarfare.capability.laser.LaserCapability;
|
||||
import com.atsuishio.superbwarfare.capability.laser.LaserCapabilityProvider;
|
||||
import com.atsuishio.superbwarfare.capability.player.PlayerVariable;
|
||||
import com.atsuishio.superbwarfare.capability.player.PlayerVariablesProvider;
|
||||
import com.atsuishio.superbwarfare.entity.vehicle.base.EnergyVehicleEntity;
|
||||
import com.atsuishio.superbwarfare.init.ModBlockEntities;
|
||||
import com.atsuishio.superbwarfare.init.ModEntities;
|
||||
import com.atsuishio.superbwarfare.init.ModItems;
|
||||
import com.atsuishio.superbwarfare.item.BatteryItem;
|
||||
import com.atsuishio.superbwarfare.item.CreativeChargingStationBlockItem;
|
||||
|
@ -33,24 +34,37 @@ public class ModCapabilities {
|
|||
event.registerEntity(ModCapabilities.PLAYER_VARIABLE, EntityType.PLAYER, new PlayerVariablesProvider());
|
||||
|
||||
// 充电站
|
||||
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CHARGING_STATION.value(), new BlockEnergyStorageProvider<>(ChargingStationBlockEntity.MAX_ENERGY));
|
||||
// TODO HANDLER
|
||||
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CHARGING_STATION.value(), ChargingStationBlockEntity::getEnergyStorage);
|
||||
// TODO ITEM HANDLER
|
||||
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ModBlockEntities.CHARGING_STATION.value(), new ChargingStationBlockEntity.ItemHandlerProvider());
|
||||
|
||||
// 创造模式充电站
|
||||
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CREATIVE_CHARGING_STATION.value(), new CreativeChargingStationBlockEntity.EnergyStorageProvider());
|
||||
event.registerItem(Capabilities.EnergyStorage.ITEM, new CreativeChargingStationBlockItem.EnergyStorageProvider(), ModItems.CREATIVE_CHARGING_STATION.value());
|
||||
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CREATIVE_CHARGING_STATION.value(), CreativeChargingStationBlockEntity::getEnergyStorage);
|
||||
event.registerItem(Capabilities.EnergyStorage.ITEM, (obj, ctx) -> ((CreativeChargingStationBlockItem) obj.getItem()).getEnergyStorage(), ModItems.CREATIVE_CHARGING_STATION.value());
|
||||
|
||||
// FuMO25
|
||||
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.FUMO_25.value(), FuMO25BlockEntity::getEnergyStorage);
|
||||
|
||||
// 电池
|
||||
// TODO 电池到底怎么回事
|
||||
for (var item : ModItems.ITEMS.getEntries()) {
|
||||
if (item.get() instanceof BatteryItem battery) {
|
||||
event.registerItem(Capabilities.EnergyStorage.ITEM, new ItemEnergyProvider(battery.maxEnergy), battery);
|
||||
event.registerItem(Capabilities.EnergyStorage.ITEM,
|
||||
(obj, ctx) -> ((BatteryItem) obj.getItem()).getEnergyStorage(),
|
||||
battery
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 载具
|
||||
// TODO 载具能量
|
||||
// event.registerEntity(Capabilities.EnergyStorage.ENTITY, ModEntities., );
|
||||
for (var entity : ModEntities.REGISTRY.getEntries()) {
|
||||
if (entity.get().getBaseClass().isAssignableFrom(EnergyVehicleEntity.class)) {
|
||||
event.registerEntity(Capabilities.EnergyStorage.ENTITY,
|
||||
entity.get(),
|
||||
(obj, ctx) -> (obj instanceof EnergyVehicleEntity vehicle) ? vehicle.getEnergyStorage() : null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,14 +11,11 @@ import net.minecraft.world.entity.EntityType;
|
|||
import net.minecraft.world.level.Level;
|
||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public abstract class EnergyVehicleEntity extends VehicleEntity {
|
||||
|
||||
public static final EntityDataAccessor<Integer> ENERGY = SynchedEntityData.defineId(EnergyVehicleEntity.class, EntityDataSerializers.INT);
|
||||
|
||||
protected final SyncedEntityEnergyStorage energyStorage = new SyncedEntityEnergyStorage(this.getMaxEnergy(), this.entityData, ENERGY);
|
||||
protected final Supplier<IEnergyStorage> energy = () -> energyStorage;
|
||||
protected final IEnergyStorage energyStorage = new SyncedEntityEnergyStorage(this.getMaxEnergy(), this.entityData, ENERGY);
|
||||
|
||||
public EnergyVehicleEntity(EntityType<?> pEntityType, Level pLevel) {
|
||||
super(pEntityType, pLevel);
|
||||
|
@ -36,14 +33,14 @@ public abstract class EnergyVehicleEntity extends VehicleEntity {
|
|||
protected void readAdditionalSaveData(CompoundTag compound) {
|
||||
super.readAdditionalSaveData(compound);
|
||||
if (compound.get("Energy") instanceof IntTag energyNBT) {
|
||||
energyStorage.deserializeNBT(level().registryAccess(), energyNBT);
|
||||
((SyncedEntityEnergyStorage) energyStorage).deserializeNBT(level().registryAccess(), energyNBT);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addAdditionalSaveData(CompoundTag compound) {
|
||||
super.addAdditionalSaveData(compound);
|
||||
compound.put("Energy", energyStorage.serializeNBT(level().registryAccess()));
|
||||
compound.put("Energy", ((SyncedEntityEnergyStorage) energyStorage).serializeNBT(level().registryAccess()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -63,6 +60,10 @@ public abstract class EnergyVehicleEntity extends VehicleEntity {
|
|||
return this.energyStorage.getEnergyStored();
|
||||
}
|
||||
|
||||
public IEnergyStorage getEnergyStorage() {
|
||||
return this.energyStorage;
|
||||
}
|
||||
|
||||
public void setEnergy(int pEnergy) {
|
||||
int targetEnergy = Mth.clamp(pEnergy, 0, this.getMaxEnergy());
|
||||
|
||||
|
|
|
@ -5,6 +5,8 @@ import net.minecraft.world.inventory.tooltip.TooltipComponent;
|
|||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.neoforged.neoforge.capabilities.Capabilities;
|
||||
import net.neoforged.neoforge.energy.EnergyStorage;
|
||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Optional;
|
||||
|
@ -13,9 +15,16 @@ public class BatteryItem extends Item {
|
|||
|
||||
public int maxEnergy;
|
||||
|
||||
public IEnergyStorage getEnergyStorage() {
|
||||
return energy;
|
||||
}
|
||||
|
||||
private final IEnergyStorage energy;
|
||||
|
||||
public BatteryItem(int maxEnergy, Properties properties) {
|
||||
super(properties.stacksTo(1));
|
||||
this.maxEnergy = maxEnergy;
|
||||
this.energy = new EnergyStorage(maxEnergy);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -2,13 +2,9 @@ package com.atsuishio.superbwarfare.item;
|
|||
|
||||
import com.atsuishio.superbwarfare.capability.energy.InfinityEnergyStorage;
|
||||
import com.atsuishio.superbwarfare.init.ModBlocks;
|
||||
import com.atsuishio.superbwarfare.init.ModItems;
|
||||
import net.minecraft.world.item.BlockItem;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Rarity;
|
||||
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
|
||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class CreativeChargingStationBlockItem extends BlockItem {
|
||||
|
@ -17,15 +13,10 @@ public class CreativeChargingStationBlockItem extends BlockItem {
|
|||
super(ModBlocks.CREATIVE_CHARGING_STATION.get(), new Properties().rarity(Rarity.EPIC).stacksTo(1));
|
||||
}
|
||||
|
||||
public static class EnergyStorageProvider implements ICapabilityProvider<ItemStack, Void, IEnergyStorage> {
|
||||
|
||||
private final IEnergyStorage energy = new InfinityEnergyStorage();
|
||||
|
||||
@Override
|
||||
public @Nullable IEnergyStorage getCapability(@NotNull ItemStack object, Void context) {
|
||||
if (object.getItem() != ModItems.CREATIVE_CHARGING_STATION.get()) return null;
|
||||
public @Nullable IEnergyStorage getEnergyStorage() {
|
||||
return energy;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue