注册ItemHandlerCapability
This commit is contained in:
parent
5e8f08c8ba
commit
1595dd23c9
2 changed files with 30 additions and 29 deletions
|
@ -30,11 +30,8 @@ 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.Capabilities;
|
||||
import net.neoforged.neoforge.capabilities.ICapabilityProvider;
|
||||
import net.neoforged.neoforge.energy.EnergyStorage;
|
||||
import net.neoforged.neoforge.energy.IEnergyStorage;
|
||||
import net.neoforged.neoforge.items.IItemHandler;
|
||||
import net.neoforged.neoforge.items.wrapper.SidedInvWrapper;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
|
@ -379,30 +376,6 @@ public class ChargingStationBlockEntity extends BlockEntity implements WorldlyCo
|
|||
return compoundtag;
|
||||
}
|
||||
|
||||
public static class ItemHandlerProvider implements ICapabilityProvider<ChargingStationBlockEntity, Direction, IItemHandler> {
|
||||
|
||||
private IItemHandler[] itemHandlers;
|
||||
|
||||
@Override
|
||||
public @Nullable IItemHandler getCapability(@NotNull ChargingStationBlockEntity object, Direction context) {
|
||||
if (context == null || object.isRemoved()) return null;
|
||||
|
||||
if (itemHandlers == null) {
|
||||
this.itemHandlers = new IItemHandler[]{
|
||||
new SidedInvWrapper(object, Direction.UP),
|
||||
new SidedInvWrapper(object, Direction.DOWN),
|
||||
new SidedInvWrapper(object, Direction.NORTH),
|
||||
};
|
||||
}
|
||||
|
||||
return switch (context) {
|
||||
case UP -> itemHandlers[0];
|
||||
case DOWN -> itemHandlers[1];
|
||||
default -> itemHandlers[2];
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@ParametersAreNonnullByDefault
|
||||
public void saveToItem(ItemStack stack, HolderLookup.Provider registries) {
|
||||
|
|
|
@ -9,12 +9,14 @@ 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.ContainerMobileVehicleEntity;
|
||||
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.CreativeChargingStationBlockItem;
|
||||
import com.atsuishio.superbwarfare.item.EnergyStorageItem;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.entity.EntityType;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
|
@ -22,6 +24,9 @@ import net.neoforged.fml.common.EventBusSubscriber;
|
|||
import net.neoforged.neoforge.capabilities.Capabilities;
|
||||
import net.neoforged.neoforge.capabilities.EntityCapability;
|
||||
import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent;
|
||||
import net.neoforged.neoforge.items.IItemHandler;
|
||||
import net.neoforged.neoforge.items.wrapper.InvWrapper;
|
||||
import net.neoforged.neoforge.items.wrapper.SidedInvWrapper;
|
||||
import net.neoforged.neoforge.registries.DeferredHolder;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
@ -40,8 +45,22 @@ public class ModCapabilities {
|
|||
|
||||
// 充电站
|
||||
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());
|
||||
// TODO 这注册对吗?
|
||||
event.registerBlockEntity(Capabilities.ItemHandler.BLOCK, ModBlockEntities.CHARGING_STATION.value(), (object, context) -> {
|
||||
if (context == null || object.isRemoved()) return null;
|
||||
|
||||
var itemHandlers = new IItemHandler[]{
|
||||
new SidedInvWrapper(object, Direction.UP),
|
||||
new SidedInvWrapper(object, Direction.DOWN),
|
||||
new SidedInvWrapper(object, Direction.NORTH),
|
||||
};
|
||||
|
||||
return switch (context) {
|
||||
case UP -> itemHandlers[0];
|
||||
case DOWN -> itemHandlers[1];
|
||||
default -> itemHandlers[2];
|
||||
};
|
||||
});
|
||||
|
||||
// 创造模式充电站
|
||||
event.registerBlockEntity(Capabilities.EnergyStorage.BLOCK, ModBlockEntities.CREATIVE_CHARGING_STATION.value(), CreativeChargingStationBlockEntity::getEnergyStorage);
|
||||
|
@ -67,12 +86,21 @@ public class ModCapabilities {
|
|||
|
||||
// 载具
|
||||
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
|
||||
);
|
||||
}
|
||||
|
||||
// 物品
|
||||
if (entity.get().getBaseClass().isAssignableFrom(ContainerMobileVehicleEntity.class)) {
|
||||
event.registerEntity(Capabilities.ItemHandler.ENTITY,
|
||||
entity.get(),
|
||||
(obj, ctx) -> (obj instanceof ContainerMobileVehicleEntity vehicle) ? new InvWrapper(vehicle) : null
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue