use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.
the class TileEntityController method findConnectedBlocks.
@Nonnull
private List<ConnectedBlockClientInfo> findConnectedBlocks() {
WorldBlob worldBlob = XNetBlobData.getBlobData(getWorld()).getWorldBlob(getWorld());
Set<ConnectedBlockClientInfo> set = new HashSet<>();
for (BlockPos consumerPos : worldBlob.getConsumers(networkId)) {
String name = "";
TileEntity te = getWorld().getTileEntity(consumerPos);
if (te instanceof ConnectorTileEntity) {
// Should always be the case. @todo error?
name = ((ConnectorTileEntity) te).getConnectorName();
} else {
XNet.logger.warn("What? The connector at " + BlockPosTools.toString(consumerPos) + " is not a connector?");
}
for (EnumFacing facing : EnumFacing.VALUES) {
if (ConnectorBlock.isConnectable(getWorld(), consumerPos, facing)) {
BlockPos pos = consumerPos.offset(facing);
SidedPos sidedPos = new SidedPos(pos, facing.getOpposite());
IBlockState state = getWorld().getBlockState(pos);
ItemStack item = state.getBlock().getItem(getWorld(), pos, state);
ConnectedBlockClientInfo info = new ConnectedBlockClientInfo(sidedPos, item, name);
set.add(info);
}
}
}
List<ConnectedBlockClientInfo> list = new ArrayList<>(set);
list.sort(Comparator.comparing(ConnectedBlockClientInfo::getBlockUnlocName).thenComparing(ConnectedBlockClientInfo::getPos));
return list;
}
use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.
the class EnergyChannelSettings method tick.
@Override
public void tick(int channel, IControllerContext context) {
updateCache(channel, context);
World world = context.getControllerWorld();
// First find out how much energy we have to distribute in total
int totalToDistribute = 0;
// Keep track of the connectors we already got energy from and how much energy we
// got from it
Map<BlockPos, Integer> alreadyHandled = new HashMap<>();
List<Pair<ConnectorTileEntity, Integer>> energyProducers = new ArrayList<>();
for (Pair<SidedConsumer, EnergyConnectorSettings> entry : energyExtractors) {
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (connectorPos != null) {
EnumFacing side = entry.getKey().getSide();
BlockPos energyPos = connectorPos.offset(side);
if (!WorldTools.chunkLoaded(world, energyPos)) {
continue;
}
TileEntity te = world.getTileEntity(energyPos);
// @todo report error somewhere?
if (isEnergyTE(te, side.getOpposite())) {
EnergyConnectorSettings settings = entry.getValue();
ConnectorTileEntity connectorTE = (ConnectorTileEntity) world.getTileEntity(connectorPos);
if (checkRedstone(world, settings, connectorPos)) {
continue;
}
if (!context.matchColor(settings.getColorsMask())) {
continue;
}
Integer count = settings.getMinmax();
if (count != null) {
int level = getEnergyLevel(te, side.getOpposite());
if (level < count) {
continue;
}
}
Integer rate = settings.getRate();
if (rate == null) {
boolean advanced = ConnectorBlock.isAdvancedConnector(world, connectorPos);
rate = advanced ? GeneralConfiguration.maxRfRateAdvanced : GeneralConfiguration.maxRfRateNormal;
}
connectorTE.setEnergyInputFrom(side, rate);
if (!alreadyHandled.containsKey(connectorPos)) {
// We did not handle this connector yet. Remember the amount of energy in it
alreadyHandled.put(connectorPos, connectorTE.getEnergy());
}
// Check how much energy we can still send from that connector
int connectorEnergy = alreadyHandled.get(connectorPos);
int tosend = Math.min(rate, connectorEnergy);
if (tosend > 0) {
// Decrease the energy from our temporary datastructure
alreadyHandled.put(connectorPos, connectorEnergy - tosend);
totalToDistribute += tosend;
energyProducers.add(Pair.of(connectorTE, tosend));
}
}
}
}
if (totalToDistribute <= 0) {
// Nothing to do
return;
}
if (!context.checkAndConsumeRF(GeneralConfiguration.controllerOperationRFT)) {
// Not enough energy for this operation
return;
}
int actuallyConsumed = insertEnergy(context, totalToDistribute);
if (actuallyConsumed <= 0) {
// Nothing was done
return;
}
// Now we need to actually fetch the energy from the producers
for (Pair<ConnectorTileEntity, Integer> entry : energyProducers) {
ConnectorTileEntity connectorTE = entry.getKey();
int amount = entry.getValue();
int actuallySpent = Math.min(amount, actuallyConsumed);
connectorTE.setEnergy(connectorTE.getEnergy() - actuallySpent);
actuallyConsumed -= actuallySpent;
if (actuallyConsumed <= 0) {
break;
}
}
}
use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.
the class ConnectorUpgradeItem method onItemUse.
@Override
public EnumActionResult onItemUse(EntityPlayer player, World world, BlockPos pos, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) {
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block == NetCableSetup.connectorBlock) {
if (!world.isRemote) {
TileEntity te = world.getTileEntity(pos);
if (te instanceof ConnectorTileEntity) {
NBTTagCompound tag = new NBTTagCompound();
te.writeToNBT(tag);
CableColor color = world.getBlockState(pos).getValue(GenericCableBlock.COLOR);
XNetBlobData blobData = XNetBlobData.getBlobData(world);
WorldBlob worldBlob = blobData.getWorldBlob(world);
ConsumerId consumer = worldBlob.getConsumerAt(pos);
((ConnectorBlock) block).unlinkBlock(world, pos);
world.setBlockState(pos, NetCableSetup.advancedConnectorBlock.getDefaultState().withProperty(GenericCableBlock.COLOR, color));
IBlockState blockState = world.getBlockState(pos);
((ConnectorBlock) blockState.getBlock()).createCableSegment(world, pos, consumer);
te = TileEntity.create(world, tag);
if (te != null) {
world.getChunkFromBlockCoords(pos).addTileEntity(te);
te.markDirty();
world.notifyBlockUpdate(pos, blockState, blockState, 3);
player.inventory.decrStackSize(player.inventory.currentItem, 1);
player.openContainer.detectAndSendChanges();
player.sendStatusMessage(new TextComponentString(TextFormatting.GREEN + "Connector was upgraded"), false);
} else {
player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Something went wrong during upgrade!"), false);
return EnumActionResult.FAIL;
}
}
}
return EnumActionResult.SUCCESS;
} else if (block == NetCableSetup.advancedConnectorBlock) {
if (!world.isRemote) {
player.sendStatusMessage(new TextComponentString(TextFormatting.YELLOW + "This connector is already advanced!"), false);
}
return EnumActionResult.SUCCESS;
} else {
if (!world.isRemote) {
player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Use this item on a connector to upgrade it!"), false);
}
return EnumActionResult.SUCCESS;
}
}
use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.
the class GuiProxy method getClientGuiElement.
@Override
public Object getClientGuiElement(int guiid, EntityPlayer entityPlayer, World world, int x, int y, int z) {
if (guiid == GUI_MANUAL_XNET) {
return new GuiXNetManual(GuiXNetManual.MANUAL_XNET);
}
BlockPos pos = new BlockPos(x, y, z);
Block block = world.getBlockState(pos).getBlock();
if (block instanceof GenericBlock) {
GenericBlock<?, ?> genericBlock = (GenericBlock<?, ?>) block;
TileEntity te = world.getTileEntity(pos);
return genericBlock.createClientGui(entityPlayer, te);
} else if (block instanceof ConnectorBlock) {
TileEntity te = world.getTileEntity(pos);
return new GuiConnector((ConnectorTileEntity) te, new EmptyContainer(entityPlayer, null));
}
return null;
}
use of mcjty.xnet.blocks.cables.ConnectorTileEntity in project XNet by McJty.
the class LogicChannelSettings method tick.
@Override
public void tick(int channel, IControllerContext context) {
delay--;
if (delay <= 0) {
// Multiply of the different speeds we have
delay = 200 * 6;
}
if (delay % 5 != 0) {
return;
}
int d = delay / 5;
updateCache(channel, context);
World world = context.getControllerWorld();
colors = 0;
for (Pair<SidedConsumer, LogicConnectorSettings> entry : sensors) {
LogicConnectorSettings settings = entry.getValue();
if (d % settings.getSpeed() != 0) {
// Use the color settings from this connector as we last remembered it
colors |= settings.getColorMask();
continue;
}
int sensorColors = 0;
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (connectorPos != null) {
EnumFacing side = entry.getKey().getSide();
BlockPos pos = connectorPos.offset(side);
if (!WorldTools.chunkLoaded(world, pos)) {
// If it is not chunkloaded we just use the color settings as we last remembered it
colors |= settings.getColorMask();
continue;
}
boolean sense = true;
sense = !checkRedstone(world, settings, connectorPos);
if (sense && !context.matchColor(settings.getColorsMask())) {
sense = false;
}
// If sense is false the sensor is disabled which means the colors from it will also be disabled
if (sense) {
TileEntity te = world.getTileEntity(pos);
for (Sensor sensor : settings.getSensors()) {
if (sensor.test(te, world, pos, settings)) {
sensorColors |= 1 << sensor.getOutputColor().ordinal();
}
}
}
}
settings.setColorMask(sensorColors);
colors |= sensorColors;
}
for (Pair<SidedConsumer, LogicConnectorSettings> entry : outputs) {
LogicConnectorSettings settings = entry.getValue();
if (d % settings.getSpeed() != 0) {
continue;
}
BlockPos connectorPos = context.findConsumerPosition(entry.getKey().getConsumerId());
if (connectorPos != null) {
EnumFacing side = entry.getKey().getSide();
if (!WorldTools.chunkLoaded(world, connectorPos)) {
continue;
}
TileEntity te = world.getTileEntity(connectorPos);
if (te instanceof ConnectorTileEntity) {
ConnectorTileEntity connectorTE = (ConnectorTileEntity) te;
int powerOut;
if (checkRedstone(world, settings, connectorPos)) {
powerOut = 0;
} else if (!context.matchColor(settings.getColorsMask())) {
powerOut = 0;
} else {
powerOut = settings.getRedstoneOut() == null ? 0 : settings.getRedstoneOut();
}
connectorTE.setPowerOut(side, powerOut);
}
}
}
}
Aggregations