use of net.minecraftforge.common.capabilities.Capability in project Metalworks by canitzp.
the class ItemBattery method initCapabilities.
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable NBTTagCompound nbt) {
return new ICapabilityProvider() {
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == CapabilityEnergy.ENERGY;
}
@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
int maxTransfer = Math.round(maxEnergy / 50.0F);
return capability == CapabilityEnergy.ENERGY ? CapabilityEnergy.ENERGY.cast(new EnergyStorage(maxEnergy, maxTransfer, maxTransfer, stack.getTagCompound() != null ? stack.getTagCompound().getInteger("Energy") : 0) {
@Override
public int receiveEnergy(int maxReceive, boolean simulate) {
int i = super.receiveEnergy(maxReceive, simulate);
setEnergy(energy);
return i;
}
@Override
public int extractEnergy(int maxExtract, boolean simulate) {
int i = super.extractEnergy(maxExtract, simulate);
setEnergy(energy);
return i;
}
private void setEnergy(int energy) {
NBTTagCompound nbt = stack.getTagCompound() != null ? stack.getTagCompound() : new NBTTagCompound();
nbt.setInteger("Energy", energy);
stack.setTagCompound(nbt);
}
}) : null;
}
};
}
use of net.minecraftforge.common.capabilities.Capability in project minecolonies by Minecolonies.
the class AbstractBuilding method getCapability.
@Nullable
@Override
public <T> T getCapability(@Nonnull final Capability<T> capability, @Nullable final EnumFacing facing) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY && facing == null) {
final Set<ICapabilityProvider> providers = new HashSet<>();
// Add myself
providers.add(getTileEntity());
// Add additional containers
providers.addAll(getAdditionalCountainers().stream().map(getTileEntity().getWorld()::getTileEntity).filter(entity -> (entity instanceof TileEntityChest) || (entity instanceof TileEntityRack)).collect(Collectors.toSet()));
providers.removeIf(Objects::isNull);
// Map all providers to IItemHandlers.
final Set<IItemHandlerModifiable> modifiables = providers.stream().flatMap(provider -> InventoryUtils.getItemHandlersFromProvider(provider).stream()).filter(handler -> handler instanceof IItemHandlerModifiable).map(handler -> (IItemHandlerModifiable) handler).collect(Collectors.toSet());
return (T) new CombinedItemHandler(getSchematicName(), modifiables.toArray(new IItemHandlerModifiable[modifiables.size()]));
}
return null;
}
use of net.minecraftforge.common.capabilities.Capability in project SpringFestival by TeamCovertDragon.
the class ModuleFortune method onPlayerClone.
@SubscribeEvent
public void onPlayerClone(net.minecraftforge.event.entity.player.PlayerEvent.Clone event) {
Capability<IFortuneValueSystem> capability = CapabilityLoader.fortuneValue;
Capability.IStorage<IFortuneValueSystem> storage = capability.getStorage();
if (event.getOriginal().hasCapability(capability, null) && event.getEntityPlayer().hasCapability(capability, null)) {
NBTBase nbt = storage.writeNBT(capability, event.getOriginal().getCapability(capability, null), null);
storage.readNBT(capability, event.getEntityPlayer().getCapability(capability, null), null, nbt);
}
}
use of net.minecraftforge.common.capabilities.Capability in project BluePower by Qmunity.
the class TileBlulectricCable method tick.
@Override
public void tick() {
storage.resetCurrent();
if (level != null && !level.isClientSide) {
BlockState state = getBlockState();
if (state.getBlock() instanceof BlockBlulectricCable) {
List<Direction> directions = new ArrayList<>(BlockBlulectricCable.FACING.getPossibleValues());
// Check the side has capability
directions.removeIf(d -> !getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, d).isPresent());
// Balance power of attached blulectric blocks.
for (Direction facing : directions) {
Block fBlock = level.getBlockState(worldPosition.relative(facing)).getBlock();
if (fBlock != Blocks.AIR && fBlock != Blocks.WATER) {
TileEntity tile = level.getBlockEntity(worldPosition.relative(facing));
if (tile != null)
tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, facing.getOpposite()).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
} else {
TileEntity tile = level.getBlockEntity(worldPosition.relative(facing).relative(state.getValue(BlockBlulectricCable.FACING).getOpposite()));
if (tile != null)
tile.getCapability(CapabilityBlutricity.BLUTRICITY_CAPABILITY, state.getValue(BlockBlulectricCable.FACING)).ifPresent(exStorage -> EnergyHelper.balancePower(exStorage, storage));
}
}
}
}
}
use of net.minecraftforge.common.capabilities.Capability in project BluePower by Qmunity.
the class BlockBPCableBase method getStateForPos.
private BlockState getStateForPos(World world, BlockPos pos, BlockState state, Direction face) {
List<Direction> directions = new ArrayList<>(FACING.getPossibleValues());
List<Direction> internal = null;
boolean connected_left = false;
boolean connected_right = false;
boolean connected_front = false;
boolean connected_back = false;
boolean join_left = false;
boolean join_right = false;
boolean join_front = false;
boolean join_back = false;
// Make sure the side we are trying to connect on isn't blocked.
TileEntity ownTile = world.getBlockEntity(pos);
if (ownTile instanceof TileBPMultipart) {
directions.removeIf(d -> ((TileBPMultipart) ownTile).isSideBlocked(getCapability(), d));
internal = ((TileBPMultipart) ownTile).getStates().stream().filter(s -> s.getBlock() == this).map(s -> s.getValue(FACING)).collect(Collectors.toList());
}
// Make sure the cable is on the same side of the block
directions.removeIf(d -> {
TileEntity t = world.getBlockEntity(pos.relative(d));
return (world.getBlockState(pos.relative(d)).getBlock() == this && world.getBlockState(pos.relative(d)).getValue(FACING) != face) || (t instanceof TileBPMultipart && ((TileBPMultipart) t).getStates().stream().noneMatch(s -> s.getValue(FACING) == face));
});
// Populate all directions
for (Direction d : directions) {
TileEntity tileEntity = world.getBlockEntity(pos.relative(d));
BlockState dirState = world.getBlockState(pos.relative(d));
BlockPos dirPos = pos.relative(d);
boolean join = false;
// If Air look for a change in Direction
if (world.getBlockState(pos.relative(d)).getBlock() == Blocks.AIR) {
dirState = world.getBlockState(pos.relative(d).relative(face.getOpposite()));
dirPos = pos.relative(d).relative(face.getOpposite());
if (dirState.getBlock() == this && dirState.getValue(FACING) == d) {
tileEntity = world.getBlockEntity(pos.relative(d).relative(face.getOpposite()));
join = true;
} else if (dirState.getBlock() instanceof BlockBPMultipart) {
tileEntity = world.getBlockEntity(pos.relative(d).relative(face.getOpposite()));
if (tileEntity instanceof TileBPMultipart && ((TileBPMultipart) tileEntity).getStates().stream().filter(s -> s.getBlock() == this).anyMatch(s -> s.getValue(FACING) == d)) {
join = true;
} else {
tileEntity = null;
}
}
}
// Check Capability for Direction
switch(state.getValue(FACING)) {
case UP:
case DOWN:
switch(d) {
case EAST:
connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case WEST:
connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case NORTH:
connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case SOUTH:
connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
}
break;
case NORTH:
switch(d) {
case WEST:
connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case EAST:
connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case UP:
connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case DOWN:
connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
}
break;
case SOUTH:
switch(d) {
case EAST:
connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case WEST:
connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case UP:
connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case DOWN:
connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
}
break;
case EAST:
switch(d) {
case NORTH:
connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case SOUTH:
connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case UP:
connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case DOWN:
connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
}
break;
case WEST:
switch(d) {
case SOUTH:
connected_right = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_right = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case NORTH:
connected_left = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_left = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case UP:
connected_front = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_front = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
case DOWN:
connected_back = canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
join_back = join && canConnect(world, dirPos, dirState, tileEntity, d.getOpposite());
break;
}
}
}
if (internal != null)
for (Direction d : internal) {
switch(state.getValue(FACING)) {
case UP:
case DOWN:
switch(d) {
case EAST:
connected_left = true;
break;
case WEST:
connected_right = true;
break;
case NORTH:
connected_back = true;
break;
case SOUTH:
connected_front = true;
break;
}
break;
case NORTH:
switch(d) {
case WEST:
connected_left = true;
break;
case EAST:
connected_right = true;
break;
case UP:
connected_back = true;
break;
case DOWN:
connected_front = true;
break;
}
break;
case SOUTH:
switch(d) {
case EAST:
connected_left = true;
break;
case WEST:
connected_right = true;
break;
case UP:
connected_back = true;
break;
case DOWN:
connected_front = true;
break;
}
break;
case EAST:
switch(d) {
case NORTH:
connected_left = true;
break;
case SOUTH:
connected_right = true;
break;
case UP:
connected_back = true;
break;
case DOWN:
connected_front = true;
break;
}
break;
case WEST:
switch(d) {
case SOUTH:
connected_left = true;
break;
case NORTH:
connected_right = true;
break;
case UP:
connected_back = true;
break;
case DOWN:
connected_front = true;
break;
}
}
}
FluidState fluidstate = world.getFluidState(pos);
return state.setValue(CONNECTED_LEFT, connected_left).setValue(CONNECTED_RIGHT, connected_right).setValue(CONNECTED_FRONT, connected_front).setValue(CONNECTED_BACK, connected_back).setValue(JOIN_LEFT, join_left).setValue(JOIN_RIGHT, join_right).setValue(JOIN_FRONT, join_front).setValue(JOIN_BACK, join_back).setValue(WATERLOGGED, fluidstate.getType() == Fluids.WATER);
}
Aggregations