package net.brokenmoon.redcontrol.blocks; import com.simon816.j65el02.device.RedBus; import net.brokenmoon.redcontrol.RedControl; import net.brokenmoon.redcontrol.api.RCWorldBus; import net.brokenmoon.redcontrol.blockentities.Peripheral; import net.brokenmoon.redcontrol.mixin.RedBusAccessor; import net.minecraft.block.*; import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemPlacementContext; import net.minecraft.item.ItemStack; import net.minecraft.state.StateManager; import net.minecraft.state.property.Properties; import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; public abstract class NetworkCarrier extends BlockWithEntity implements BlockEntityProvider { public NetworkCarrier(Settings settings) { super(settings); } public RCWorldBus getBus(World world, BlockPos pos){ return ((Peripheral)world.getBlockEntity(pos)).getBus(); } public void setBus(World world, BlockPos pos, RCWorldBus bus){ ((Peripheral)world.getBlockEntity(pos)).setBus(bus); } @Override public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { generateBus(world, pos); } @Override @SuppressWarnings("uses or overrides a deprecated API.") public void onStateReplaced(BlockState state, World world, BlockPos pos, BlockState newState, boolean moved) { if(state.hasBlockEntity() && world.getBlockEntity(pos) instanceof Peripheral){ if(((Peripheral) world.getBlockEntity(pos)).getBus() != null) ((Peripheral) world.getBlockEntity(pos)).getBus().setValid(false); } if (state.hasBlockEntity() && !state.isOf(newState.getBlock())) { world.removeBlockEntity(pos); } } @Override public BlockRenderType getRenderType(BlockState state) { // With inheriting from BlockWithEntity this defaults to INVISIBLE, so we need to change that! return BlockRenderType.MODEL; } public void generateBus(World world, BlockPos pos){ Block worldBlock = world.getBlockState(pos).getBlock(); if(world.getBlockEntity(pos) instanceof Peripheral){ Peripheral entityBlock = (Peripheral) world.getBlockEntity(pos); RCWorldBus bus = new RCWorldBus(new RedBus(), world, pos); entityBlock.setBus(bus); entityBlock.getBus().setValid(true); floodBus(entityBlock.getBus(), world, pos); } } private void floodBus(RCWorldBus bus, World world, BlockPos pos) { replaceBus(bus, world, pos.north()); replaceBus(bus, world, pos.south()); replaceBus(bus, world, pos.east()); replaceBus(bus, world, pos.west()); replaceBus(bus, world, pos.up()); replaceBus(bus, world, pos.down()); } private void replaceBus(RCWorldBus bus, World world, BlockPos pos) { if(world.getBlockEntity(pos) instanceof Peripheral && ((Peripheral) world.getBlockEntity(pos)).getBus() != bus){ ((Peripheral) world.getBlockEntity(pos)).setBus(bus); floodBus(bus, world, pos); } } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { if (!world.isClient) { if (player.getStackInHand(hand).getItem() == RedControl.SQUEAKY_HAMMER) { Peripheral peripheral = (Peripheral) world.getBlockEntity(pos); player.sendMessage(Text.literal("Network Carrier Debug: "), false); player.sendMessage(Text.literal(" " + Integer.toHexString(peripheral.getBus().hashCode())), false); player.sendMessage(Text.literal(" " + Integer.toHexString(peripheral.getBus().getRedBus().hashCode())), false); player.sendMessage(Text.literal(" Peripherals on RedBus: "), false); RedBus.Peripheral[] peripherals = ((RedBusAccessor) peripheral.getBus().getRedBus()).getPeripherals(); for (RedBus.Peripheral value : peripherals) { if (value != null) { player.sendMessage(Text.literal(" " + value.getClass().toString()), false); } } } } return ActionResult.SUCCESS; } @Override protected void appendProperties(StateManager.Builder builder) { builder.add(Properties.HORIZONTAL_FACING); } @Override public BlockState getPlacementState(ItemPlacementContext ctx) { return super.getPlacementState(ctx).with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing().getOpposite()); } }