diff --git a/src/main/java/net/montoyo/wd/WebDisplays.java b/src/main/java/net/montoyo/wd/WebDisplays.java index 310cfe0..7364ace 100644 --- a/src/main/java/net/montoyo/wd/WebDisplays.java +++ b/src/main/java/net/montoyo/wd/WebDisplays.java @@ -40,6 +40,7 @@ import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.ModList; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import net.minecraftforge.network.PacketDistributor; import net.montoyo.wd.block.BlockKeyboardRight; import net.montoyo.wd.block.BlockPeripheral; import net.montoyo.wd.block.BlockScreen; @@ -291,7 +292,7 @@ public class WebDisplays { @SubscribeEvent public void onLogIn(PlayerEvent.PlayerLoggedInEvent ev) { if(!ev.getPlayer().getLevel().isClientSide && ev.getPlayer() instanceof ServerPlayer) { - Messages.INSTANCE.sendTo(new CMessageServerInfo(miniservPort), (ServerPlayer) ev.getPlayer()); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> (ServerPlayer) ev.getPlayer()), new CMessageServerInfo(miniservPort)); IWDDCapability cap = (IWDDCapability) ev.getPlayer().getCapability(WDDCapability.Provider.cap, null); if(cap == null) diff --git a/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java b/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java index 308c001..909e173 100644 --- a/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java +++ b/src/main/java/net/montoyo/wd/block/BlockKeyboardRight.java @@ -10,6 +10,7 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; @@ -17,22 +18,27 @@ import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; +import net.minecraft.world.level.block.state.properties.DirectionProperty; import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.material.FluidState; import net.minecraft.world.level.material.Material; import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.shapes.CollisionContext; +import net.minecraft.world.phys.shapes.Shapes; +import net.minecraft.world.phys.shapes.VoxelShape; import net.montoyo.wd.core.DefaultPeripheral; import net.montoyo.wd.core.IPeripheral; import net.montoyo.wd.entity.TileEntityKeyboard; import net.montoyo.wd.item.ItemLinker; import net.montoyo.wd.utilities.BlockSide; import net.montoyo.wd.utilities.Vector3i; +import org.jetbrains.annotations.Nullable; public class BlockKeyboardRight extends Block implements IPeripheral { - public static final IntegerProperty facing = IntegerProperty.create("facing", 0, 3); - public static final AABB KEYBOARD_AABB = new AABB(0.0, 0.0, 0.0, 1.0, 1.0 / 16.0, 1.0); + public static final DirectionProperty facing = DirectionProperty.create("facing", Direction.Plane.HORIZONTAL); + public static final VoxelShape KEYBOARD_AABB = Shapes.box(0.0, 0.0, 0.0, 1.0, 1.0 / 16.0, 1.0); public BlockKeyboardRight() { super(Properties.of(Material.STONE) @@ -52,11 +58,10 @@ public class BlockKeyboardRight extends Block implements IPeripheral { return false; } - /*@Override - @Nonnull - public AABB getBoundingBox(BlockState state, IBlockAccess source, BlockPos pos) { + @Override + public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { return KEYBOARD_AABB; - }*/ + } private TileEntityKeyboard getTileEntity(Level world, BlockPos pos) { for(Direction nf: Direction.Plane.HORIZONTAL) { diff --git a/src/main/java/net/montoyo/wd/block/BlockPeripheral.java b/src/main/java/net/montoyo/wd/block/BlockPeripheral.java index 6089ca2..9b38089 100644 --- a/src/main/java/net/montoyo/wd/block/BlockPeripheral.java +++ b/src/main/java/net/montoyo/wd/block/BlockPeripheral.java @@ -4,108 +4,115 @@ package net.montoyo.wd.block; -import net.minecraft.block.Block; -import net.minecraft.block.material.EnumPushReaction; -import net.minecraft.block.material.Material; -import net.minecraft.block.properties.IProperty; -import net.minecraft.block.properties.PropertyEnum; -import net.minecraft.block.properties.PropertyInteger; -import net.minecraft.block.state.BlockStateContainer; -import net.minecraft.block.state.IBlockState; import net.minecraft.core.BlockPos; -import net.minecraft.creativetab.CreativeTabs; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLivingBase; -import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemStack; -import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumBlockRenderType; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; -import net.minecraft.util.NonNullList; -import net.minecraft.util.math.AxisAlignedBB; -import net.minecraft.util.math.BlockPos; -import net.minecraft.util.math.MathHelper; -import net.minecraft.util.math.Vec3i; -import net.minecraft.world.Explosion; -import net.minecraft.world.IBlockAccess; -import net.minecraft.world.World; -import net.minecraft.world.item.BlockItem; +import net.minecraft.core.Direction; +import net.minecraft.core.Vec3i; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.LivingEntity; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.level.BlockGetter; +import net.minecraft.world.level.Explosion; import net.minecraft.world.level.Level; -import net.minecraftforge.fml.common.network.NetworkRegistry; -import net.minecraftforge.network.NetworkDirection; -import net.minecraftforge.network.NetworkRegistry; -import net.montoyo.wd.WebDisplays; +import net.minecraft.world.level.block.BaseEntityBlock; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.RenderShape; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockBehaviour; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.StateDefinition; +import net.minecraft.world.level.block.state.properties.DirectionProperty; +import net.minecraft.world.level.block.state.properties.EnumProperty; +import net.minecraft.world.level.block.state.properties.Property; +import net.minecraft.world.level.material.Material; +import net.minecraft.world.level.material.PushReaction; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.shapes.CollisionContext; +import net.minecraft.world.phys.shapes.Shapes; +import net.minecraft.world.phys.shapes.VoxelShape; +import net.minecraftforge.network.PacketDistributor; import net.montoyo.wd.core.DefaultPeripheral; -import net.montoyo.wd.entity.*; +import net.montoyo.wd.entity.TileEntityInterfaceBase; +import net.montoyo.wd.entity.TileEntityKeyboard; +import net.montoyo.wd.entity.TileEntityPeripheralBase; +import net.montoyo.wd.entity.TileEntityServer; +import net.montoyo.wd.init.BlockInit; import net.montoyo.wd.item.ItemLinker; -import net.montoyo.wd.item.ItemPeripheral; +import net.montoyo.wd.net.Messages; import net.montoyo.wd.net.client.CMessageCloseGui; -import net.montoyo.wd.utilities.BlockSide; import net.montoyo.wd.utilities.Log; +import org.jetbrains.annotations.Nullable; import javax.annotation.Nonnull; -public class BlockPeripheral extends WDBlockContainer { +public class BlockPeripheral extends BaseEntityBlock { - public static final PropertyEnum type = PropertyEnum.create("type", DefaultPeripheral.class); - public static final PropertyInteger facing = PropertyInteger.create("facing", 0, 3); - private static final IProperty[] properties = new IProperty[] { type, facing }; + public static final EnumProperty type = EnumProperty.create("type", DefaultPeripheral.class); + public static final DirectionProperty facing = DirectionProperty.create("facing", Direction.NORTH, Direction.EAST, Direction.SOUTH, Direction.WEST); + private static final Property[] properties = new Property[] { type, facing }; public BlockPeripheral() { - super(Material.ROCK); - setHardness(1.5f); - setResistance(10.f); - setCreativeTab(WebDisplays.CREATIVE_TAB); - setName("peripheral"); + super(BlockBehaviour.Properties.of(Material.STONE).strength(1.5f, 10.f)); +// setName("peripheral"); + } +// @Override +// protected BlockItem createItemBlock() { +// return new ItemPeripheral(this); +// } + @Override - protected BlockItem createItemBlock() { - return new ItemPeripheral(this); + protected void createBlockStateDefinition(StateDefinition.Builder builder) { + builder.add(properties); } +// @Nullable TODO: Fix +// @Override +// public BlockState getStateForPlacement(BlockPlaceContext context) { +// Direction rot = Direction.fromYRot(placer.getYHeadRot()); +// return defaultBlockState().setValue(type, DefaultPeripheral.fromMetadata(meta)).setValue(facing, rot); +// +// +// return getStateForPlacement(context); +// } +// +// public BlockState getStateForPlacement(@Nonnull Level world, @Nonnull BlockPos pos, @Nonnull Direction nocare, float hitX, +// float hitY, float hitZ, int meta, @Nonnull LivingEntity placer, InteractionHand hand) { +// } + +// @Override +// public void getSubBlocks(CreativeTabs tab, NonNullList list) { +// for(DefaultPeripheral dp : DefaultPeripheral.values()) +// list.add(new ItemStack(getItem(), 1, dp.toMetadata(0))); +// } + +// @Override +// @Nonnull +// public IBlockState getStateFromMeta(int meta) { +// DefaultPeripheral dp = DefaultPeripheral.fromMetadata(meta); +// IBlockState state = getDefaultState().withProperty(type, dp); +// +// if(dp.hasFacing()) +// state = state.withProperty(facing, (meta >> 2) & 3); +// +// return state; +// } +// +// @Override +// public int getMetaFromState(IBlockState state) { +// return state.getValue(type).toMetadata(state.getValue(facing)); +// } + + + @Nullable @Override - @Nonnull - protected BlockStateContainer createBlockState() { - return new BlockStateContainer(this, properties); - } - - @Override - @Nonnull - public IBlockState getStateForPlacement(@Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing nocare, float hitX, - float hitY, float hitZ, int meta, @Nonnull EntityLivingBase placer, EnumHand hand) { - int rot = MathHelper.floor(((double) (placer.rotationYaw * 4.0f / 360.0f)) + 2.5) & 3; - return getDefaultState().withProperty(type, DefaultPeripheral.fromMetadata(meta)).withProperty(facing, rot); - } - - @Override - public void getSubBlocks(CreativeTabs tab, NonNullList list) { - for(DefaultPeripheral dp : DefaultPeripheral.values()) - list.add(new ItemStack(getItem(), 1, dp.toMetadata(0))); - } - - @Override - @Nonnull - public IBlockState getStateFromMeta(int meta) { - DefaultPeripheral dp = DefaultPeripheral.fromMetadata(meta); - IBlockState state = getDefaultState().withProperty(type, dp); - - if(dp.hasFacing()) - state = state.withProperty(facing, (meta >> 2) & 3); - - return state; - } - - @Override - public int getMetaFromState(IBlockState state) { - return state.getValue(type).toMetadata(state.getValue(facing)); - } - - @Override - public TileEntity createNewTileEntity(@Nonnull World world, int meta) { - Class cls = DefaultPeripheral.fromMetadata(meta).getTEClass(); + public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { + Class cls = state.getValue(type).getTEClass(); if(cls == null) return null; @@ -119,159 +126,158 @@ public class BlockPeripheral extends WDBlockContainer { } @Override - @Nonnull - public EnumBlockRenderType getRenderType(IBlockState state) { - return EnumBlockRenderType.MODEL; + public RenderShape getRenderShape(BlockState state) { + return RenderShape.MODEL; } - @Override - public int damageDropped(IBlockState state) { - return state.getValue(type).toMetadata(0); - } +// @Override +// public int damageDropped(IBlockState state) { +// return state.getValue(type).toMetadata(0); +// } + @Override - public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { - if(player.isSneaking()) - return false; + public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) { + if(player.isShiftKeyDown()) + return InteractionResult.FAIL; - if(player.getHeldItem(hand).getItem() instanceof ItemLinker) - return false; + if(player.getItemInHand(hand).getItem() instanceof ItemLinker) + return InteractionResult.FAIL; - TileEntity te = world.getTileEntity(pos); + BlockEntity te = world.getBlockEntity(pos); if(te instanceof TileEntityPeripheralBase) - return ((TileEntityPeripheralBase) te).onRightClick(player, hand, BlockSide.values()[facing.ordinal()]); + return ((TileEntityPeripheralBase) te).onRightClick(player, hand); else if(te instanceof TileEntityServer) { ((TileEntityServer) te).onPlayerRightClick(player); - return true; + return InteractionResult.PASS; } else - return false; + return InteractionResult.FAIL; + } + +// @Override +// public boolean isFullCube(IBlockState state) { TODO: FIx. +// return state.getValue(type) != DefaultPeripheral.KEYBOARD; +// } +// +// @Override +// public boolean isFullBlock(IBlockState state) { +// return state.getValue(type) != DefaultPeripheral.KEYBOARD; +// } +// +// @Override +// public boolean isNormalCube(IBlockState state, IBlockAccess world, BlockPos pos) { +// return state.getValue(type) != DefaultPeripheral.KEYBOARD; +// } +// +// @Override +// public boolean isOpaqueCube(IBlockState state) { +// return state.getValue(type) != DefaultPeripheral.KEYBOARD; +// } +// +// @Override +// public boolean doesSideBlockRendering(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face) { +// return state.getValue(type) != DefaultPeripheral.KEYBOARD; +// } + + @Override + public VoxelShape getShape(BlockState state, BlockGetter level, BlockPos pos, CollisionContext context) { + return state.getValue(type) == DefaultPeripheral.KEYBOARD ? BlockKeyboardRight.KEYBOARD_AABB : Shapes.block(); } @Override - public boolean isFullCube(IBlockState state) { - return state.getValue(type) != DefaultPeripheral.KEYBOARD; - } - - @Override - public boolean isFullBlock(IBlockState state) { - return state.getValue(type) != DefaultPeripheral.KEYBOARD; - } - - @Override - public boolean isNormalCube(IBlockState state, IBlockAccess world, BlockPos pos) { - return state.getValue(type) != DefaultPeripheral.KEYBOARD; - } - - @Override - public boolean isOpaqueCube(IBlockState state) { - return state.getValue(type) != DefaultPeripheral.KEYBOARD; - } - - @Override - public boolean doesSideBlockRendering(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing face) { - return state.getValue(type) != DefaultPeripheral.KEYBOARD; - } - - @Override - @Nonnull - public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) { - return state.getValue(type) == DefaultPeripheral.KEYBOARD ? BlockKeyboardRight.KEYBOARD_AABB : FULL_BLOCK_AABB; - } - - @Override - public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase placer, ItemStack stack) { - if(world.isRemote) + public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) { + if(world.isClientSide) return; if(state.getValue(type) == DefaultPeripheral.KEYBOARD) { //Keyboard special treatment - int f = state.getValue(facing); - Vec3i dir = EnumFacing.getHorizontal(f).rotateY().getDirectionVec(); - BlockPos left = pos.add(dir); + Direction f = state.getValue(facing); + Vec3i dir = f.getClockWise().getNormal(); + BlockPos left = pos.offset(dir); BlockPos right = pos.subtract(dir); - if(!world.isAirBlock(pos.down()) && BlockKeyboardRight.checkNeighborhood(world, pos, null)) { - if(world.isAirBlock(right) && !world.isAirBlock(right.down()) && BlockKeyboardRight.checkNeighborhood(world, right, pos)) { - world.setBlockState(right, WebDisplays.INSTANCE.blockKbRight.getDefaultState().withProperty(BlockKeyboardRight.facing, f)); + if(!world.isEmptyBlock(pos.below()) && BlockKeyboardRight.checkNeighborhood(world, pos, null)) { + if(world.isEmptyBlock(right) && !world.isEmptyBlock(right.below()) && BlockKeyboardRight.checkNeighborhood(world, right, pos)) { + world.setBlock(right, BlockInit.blockKbRight.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); return; - } else if(world.isAirBlock(left) && !world.isAirBlock(left.down()) && BlockKeyboardRight.checkNeighborhood(world, left, pos)) { - world.setBlockState(left, state); - world.setBlockState(pos, WebDisplays.INSTANCE.blockKbRight.getDefaultState().withProperty(BlockKeyboardRight.facing, f)); + } else if(world.isEmptyBlock(left) && !world.isEmptyBlock(left.below()) && BlockKeyboardRight.checkNeighborhood(world, left, pos)) { + world.setBlock(left, state, 3); + world.setBlock(pos, BlockInit.blockKbRight.get().defaultBlockState().setValue(BlockKeyboardRight.facing, f), 3); return; } } //Not good; remove this shit... - world.setBlockToAir(pos); - if(!(placer instanceof EntityPlayer) || !((EntityPlayer) placer).isCreative()) - dropBlockAsItem(world, pos, state, 0); - } else if(placer instanceof EntityPlayer) { - TileEntity te = world.getTileEntity(pos); + world.setBlockAndUpdate(pos, Blocks.AIR.defaultBlockState()); + if(!(placer instanceof Player) || !((Player) placer).isCreative()) { +// dropBlockAsItem(world, pos, state, 0); TODO: Loottable? + } + } else if(placer instanceof Player) { + BlockEntity te = world.getBlockEntity(pos); if(te instanceof TileEntityServer) - ((TileEntityServer) te).setOwner((EntityPlayer) placer); + ((TileEntityServer) te).setOwner((Player) placer); else if(te instanceof TileEntityInterfaceBase) - ((TileEntityInterfaceBase) te).setOwner((EntityPlayer) placer); + ((TileEntityInterfaceBase) te).setOwner((Player) placer); } } @Override - @Nonnull - public EnumPushReaction getMobilityFlag(IBlockState state) { - return EnumPushReaction.IGNORE; + public PushReaction getPistonPushReaction(BlockState state) { + return PushReaction.IGNORE; } - private void removeRightPiece(World world, BlockPos pos) { - for(EnumFacing nf: EnumFacing.HORIZONTALS) { - BlockPos np = pos.add(nf.getDirectionVec()); + private void removeRightPiece(Level world, BlockPos pos) { + for(Direction nf: Direction.Plane.HORIZONTAL) { + BlockPos np = pos.offset(nf.getNormal()); if(world.getBlockState(np).getBlock() instanceof BlockKeyboardRight) { - world.setBlockToAir(np); + world.setBlock(np, Blocks.AIR.defaultBlockState(), 3); break; } } } @Override - public void neighborChanged(IBlockState state, World world, BlockPos pos, Block neighborType, BlockPos neighbor) { - TileEntity te = world.getTileEntity(pos); + public void neighborChanged(BlockState state, Level world, BlockPos pos, Block neighborType, BlockPos neighbor, boolean isMoving) { + BlockEntity te = world.getBlockEntity(pos); if(te != null && te instanceof TileEntityPeripheralBase) ((TileEntityPeripheralBase) te).onNeighborChange(neighborType, neighbor); - if(world.isRemote || state.getValue(type) != DefaultPeripheral.KEYBOARD) + if(world.isClientSide || state.getValue(type) != DefaultPeripheral.KEYBOARD) return; - if(neighbor.getX() == pos.getX() && neighbor.getY() == pos.getY() - 1 && neighbor.getZ() == pos.getZ() && world.isAirBlock(neighbor)) { + if(neighbor.getX() == pos.getX() && neighbor.getY() == pos.getY() - 1 && neighbor.getZ() == pos.getZ() && world.isEmptyBlock(neighbor)) { removeRightPiece(world, pos); - world.setBlockToAir(pos); - dropBlockAsItem(world, pos, state, 0); - WebDisplays.NET_HANDLER.sendToAllAround(new CMessageCloseGui(pos), point(world, pos)); + world.setBlock(pos, Blocks.AIR.defaultBlockState(), 3); +// dropBlockAsItem(world, pos, state, 0); //TODO Loottable + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(world, pos)), new CMessageCloseGui(pos)); } } @Override - public void onBlockDestroyedByPlayer(World world, BlockPos pos, IBlockState state) { - if(!world.isRemote) { + public void playerDestroy(Level world, Player player, BlockPos pos, BlockState state, @Nullable BlockEntity blockEntity, ItemStack tool) { + if(!world.isClientSide) { if(state.getBlock() == this && state.getValue(type) == DefaultPeripheral.KEYBOARD) removeRightPiece(world, pos); - WebDisplays.NET_HANDLER.sendToAllAround(new CMessageCloseGui(pos), point(world, pos)); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(world, pos)), new CMessageCloseGui(pos)); } } @Override - public void onBlockDestroyedByExplosion(World world, BlockPos pos, Explosion explosion) { - onBlockDestroyedByPlayer(world, pos, world.getBlockState(pos)); + public void onBlockExploded(BlockState state, Level level, BlockPos pos, Explosion explosion) { + playerDestroy(level, null, pos, level.getBlockState(pos), null, null); } @Override - public void onEntityCollidedWithBlock(World world, BlockPos pos, IBlockState state, Entity entity) { - if(!world.isRemote && world.getBlockState(pos).getValue(type) == DefaultPeripheral.KEYBOARD) { - double rpos = (entity.posY - ((double) pos.getY())) * 16.0; + public void entityInside(BlockState state, Level world, BlockPos pos, Entity entity) { + if(!world.isClientSide && world.getBlockState(pos).getValue(type) == DefaultPeripheral.KEYBOARD) { + double rpos = (entity.getY() - ((double) pos.getY())) * 16.0; if(rpos >= 1.0 && rpos <= 2.0 && Math.random() < 0.25) { - TileEntity te = world.getTileEntity(pos); + BlockEntity te = world.getBlockEntity(pos); if(te != null && te instanceof TileEntityKeyboard) ((TileEntityKeyboard) te).simulateCat(entity); @@ -279,8 +285,8 @@ public class BlockPeripheral extends WDBlockContainer { } } - private static NetworkDirection point(Level world, BlockPos bp) { - return new NetworkDirection (world.dimension().location(), (double) bp.getX(), (double) bp.getY(), (double) bp.getZ(), 64.0); + public static PacketDistributor.TargetPoint point(Level world, BlockPos bp) { + return new PacketDistributor.TargetPoint(bp.getX(), bp.getY(), bp.getZ(), 64.0, world.dimension()); } } diff --git a/src/main/java/net/montoyo/wd/block/BlockScreen.java b/src/main/java/net/montoyo/wd/block/BlockScreen.java index efb7a7d..b4f5f5b 100644 --- a/src/main/java/net/montoyo/wd/block/BlockScreen.java +++ b/src/main/java/net/montoyo/wd/block/BlockScreen.java @@ -4,59 +4,60 @@ package net.montoyo.wd.block; -import net.minecraft.block.Block; -import net.minecraft.block.material.EnumPushReaction; -import net.minecraft.block.material.Material; -import net.minecraft.block.properties.IProperty; -import net.minecraft.block.properties.PropertyInteger; -import net.minecraft.block.state.BlockStateContainer; +import net.minecraft.ChatFormatting; import net.minecraft.block.state.IBlockState; -import net.minecraft.client.util.ITooltipFlag; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; -import net.minecraft.entity.player.EntityPlayerMP; -import net.minecraft.item.ItemBlock; -import net.minecraft.item.ItemStack; +import net.minecraft.server.level.ServerPlayer; import net.minecraft.tileentity.TileEntity; -import net.minecraft.util.EnumBlockRenderType; -import net.minecraft.util.EnumFacing; -import net.minecraft.util.EnumHand; -import net.minecraft.util.text.TextFormatting; import net.minecraft.world.Explosion; -import net.minecraft.world.IBlockAccess; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.World; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.level.BlockGetter; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelAccessor; +import net.minecraft.world.level.block.BaseEntityBlock; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.RenderShape; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockBehaviour; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.StateDefinition; import net.minecraft.world.level.block.state.properties.BooleanProperty; +import net.minecraft.world.level.block.state.properties.IntegerProperty; import net.minecraft.world.level.block.state.properties.Property; -import net.minecraftforge.common.property.ExtendedBlockState; -import net.minecraftforge.common.property.IExtendedBlockState; -import net.minecraftforge.common.property.IUnlistedProperty; -import net.minecraftforge.common.property.Properties; +import net.minecraft.world.level.material.Material; +import net.minecraft.world.level.material.PushReaction; +import net.minecraft.world.phys.BlockHitResult; import net.montoyo.wd.WebDisplays; import net.montoyo.wd.core.DefaultUpgrade; import net.montoyo.wd.core.IUpgrade; import net.montoyo.wd.core.ScreenRights; import net.montoyo.wd.data.SetURLData; import net.montoyo.wd.entity.TileEntityScreen; -import net.montoyo.wd.item.ItemPeripheral; +import net.montoyo.wd.init.BlockInit; import net.montoyo.wd.item.WDItem; import net.montoyo.wd.utilities.*; import javax.annotation.Nonnull; import javax.annotation.Nullable; -import java.util.List; -public class BlockScreen extends WDBlockContainer { +public class BlockScreen extends BaseEntityBlock { public static final BooleanProperty hasTE = BooleanProperty.create("haste"); public static final BooleanProperty emitting = BooleanProperty.create("emitting"); - private static final Property[] properties = new IProperty[] { hasTE, emitting }; - public static final IUnlistedProperty[] sideFlags = new IUnlistedProperty[6]; + private static final Property[] properties = new Property[] { hasTE, emitting }; + public static final IntegerProperty[] sideFlags = new IntegerProperty[6]; static { for(int i = 0; i < sideFlags.length; i++) - sideFlags[i] = Properties.toUnlisted(PropertyInteger.create("neighbor" + i, 0, 15)); + sideFlags[i] = IntegerProperty.create("neighbor" + i, 0, 15); } private static final int BAR_BOT = 1; @@ -65,34 +66,41 @@ public class BlockScreen extends WDBlockContainer { private static final int BAR_LEFT = 8; public BlockScreen() { - super(Material.ROCK); - setHardness(1.5f); - setResistance(10.f); - setCreativeTab(WebDisplays.CREATIVE_TAB); - setName("screen"); - setDefaultState(blockState.getBaseState().withProperty(hasTE, false).withProperty(emitting, false)); + super(BlockBehaviour.Properties.of(Material.STONE).strength(1.5f, 10.f)); +// setCreativeTab(WebDisplays.CREATIVE_TAB); +// setName("screen"); + registerDefaultState(getStateDefinition().any().setValue(hasTE, false).setValue(emitting, false)); + } + + @Override + protected void createBlockStateDefinition(StateDefinition.Builder builder) { + builder.add(properties).add(sideFlags); + } + + @Override + public RenderShape getRenderShape(BlockState state) { + return RenderShape.MODEL; + } + + public static boolean isntScreenBlock(Level world, Vector3i pos) { + return world.getBlockState(pos.toBlock()).getBlock() != BlockInit.blockScreen.get(); + } + + + @org.jetbrains.annotations.Nullable + @Override + public BlockState getStateForPlacement(BlockPlaceContext context) { + return super.getStateForPlacement(context); + } + + @Override + public BlockState updateShape(BlockState state, Direction direction, BlockState neighborState, LevelAccessor level, BlockPos currentPos, BlockPos neighborPos) { + return super.updateShape(state, direction, neighborState, level, currentPos, neighborPos); } @Override @Nonnull - public EnumBlockRenderType getRenderType(IBlockState state) { - return EnumBlockRenderType.MODEL; - } - - @Override - @Nonnull - protected BlockStateContainer createBlockState() { - return new ExtendedBlockState(this, properties, sideFlags); - } - - public static boolean isntScreenBlock(IBlockAccess world, Vector3i pos) { - return world.getBlockState(pos.toBlock()).getBlock() != WebDisplays.INSTANCE.blockScreen; - } - - @Override - @Nonnull - public IBlockState getExtendedState(@Nonnull IBlockState state, IBlockAccess world, BlockPos bpos) { - IExtendedBlockState ret = (IExtendedBlockState) blockState.getBaseState(); + public BlockState getExtendedState(@Nonnull BlockState ret, Level world, BlockPos bpos) { Vector3i pos = new Vector3i(bpos); for(BlockSide side : BlockSide.values()) { @@ -102,20 +110,20 @@ public class BlockScreen extends WDBlockContainer { if(isntScreenBlock(world, side.left.clone().add(pos))) icon |= BAR_LEFT; if(isntScreenBlock(world, side.right.clone().add(pos))) icon |= BAR_RIGHT; - ret = ret.withProperty(sideFlags[side.ordinal()], icon); + ret = ret.setValue(sideFlags[side.ordinal()], icon); } return ret; } - @Override - @Nonnull - public IBlockState getStateFromMeta(int meta) { - return getDefaultState().withProperty(hasTE, (meta & 1) != 0).withProperty(emitting, (meta & 2) != 0); - } +// @Override +// @Nonnull +// public IBlockState getStateFromMeta(int meta) { +// return getDefaultState().withProperty(hasTE, (meta & 1) != 0).withProperty(emitting, (meta & 2) != 0); +// } +// - @Override - public int getMetaFromState(IBlockState state) { + public int getMetaFromState(BlockState state) { int ret = 0; if(state.getValue(hasTE)) ret |= 1; @@ -127,22 +135,22 @@ public class BlockScreen extends WDBlockContainer { } @Override - public boolean onBlockActivated(World world, BlockPos bpos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing facing, float hitX, float hitY, float hitZ) { - ItemStack heldItem = player.getHeldItem(hand); + public InteractionResult use(BlockState state, Level world, BlockPos position, Player player, InteractionHand hand, BlockHitResult hit) { + ItemStack heldItem = player.getItemInHand(hand); if(heldItem.isEmpty()) heldItem = null; //Easier to work with else if(!(heldItem.getItem() instanceof IUpgrade)) - return false; + return InteractionResult.FAIL; - if(world.isRemote) - return true; + if(world.isClientSide) + return InteractionResult.FAIL; - boolean sneaking = player.isSneaking(); - Vector3i pos = new Vector3i(bpos); - BlockSide side = BlockSide.values()[facing.ordinal()]; + boolean sneaking = player.isShiftKeyDown(); + Vector3i pos = new Vector3i(position); + BlockSide side = BlockSide.values()[hit.getDirection().ordinal()]; Multiblock.findOrigin(world, pos, side, null); - TileEntityScreen te = (TileEntityScreen) world.getTileEntity(pos.toBlock()); + TileEntityScreen te = (TileEntityScreen) world.getBlockEntity(pos.toBlock()); if(te != null && te.getScreen(side) != null) { TileEntityScreen.Screen scr = te.getScreen(side); @@ -151,58 +159,58 @@ public class BlockScreen extends WDBlockContainer { if((scr.rightsFor(player) & ScreenRights.CHANGE_URL) == 0) Util.toast(player, "restrictions"); else - (new SetURLData(pos, scr.side, scr.url)).sendTo((EntityPlayerMP) player); + (new SetURLData(pos, scr.side, scr.url)).sendTo((ServerPlayer) player); - return true; + return InteractionResult.SUCCESS; } else if(heldItem != null && !te.hasUpgrade(side, heldItem)) { //Add upgrade if((scr.rightsFor(player) & ScreenRights.MANAGE_UPGRADES) == 0) { Util.toast(player, "restrictions"); - return true; + return InteractionResult.SUCCESS; } if(te.addUpgrade(side, heldItem, player, false)) { if(!player.isCreative()) heldItem.shrink(1); - Util.toast(player, TextFormatting.AQUA, "upgradeOk"); - if(player instanceof EntityPlayerMP) - WebDisplays.INSTANCE.criterionUpgradeScreen.trigger(((EntityPlayerMP) player).getAdvancements()); + Util.toast(player, ChatFormatting.AQUA, "upgradeOk"); + if(player instanceof ServerPlayer) + WebDisplays.INSTANCE.criterionUpgradeScreen.trigger(((ServerPlayer) player).getAdvancements()); } else Util.toast(player, "upgradeError"); - return true; + return InteractionResult.SUCCESS; } else { //Click if((scr.rightsFor(player) & ScreenRights.CLICK) == 0) { Util.toast(player, "restrictions"); - return true; + return InteractionResult.SUCCESS; } Vector2i tmp = new Vector2i(); - if(hit2pixels(side, bpos, pos, scr, hitX, hitY, hitZ, tmp)) + if(hit2pixels(side, hit.getBlockPos(), pos, scr, (float) hit.getLocation().x, (float) hit.getLocation().y, (float) hit.getLocation().z, tmp)) te.click(side, tmp); - return true; + return InteractionResult.SUCCESS; } } else if(sneaking) { Util.toast(player, "turnOn"); - return true; + return InteractionResult.SUCCESS; } Vector2i size = Multiblock.measure(world, pos, side); if(size.x < 2 || size.y < 2) { Util.toast(player, "tooSmall"); - return true; + return InteractionResult.SUCCESS; } if(size.x > WebDisplays.INSTANCE.maxScreenX || size.y > WebDisplays.INSTANCE.maxScreenY) { Util.toast(player, "tooBig", WebDisplays.INSTANCE.maxScreenX, WebDisplays.INSTANCE.maxScreenY); - return true; + return InteractionResult.SUCCESS; } Vector3i err = Multiblock.check(world, pos, size, side); if(err != null) { Util.toast(player, "invalid", err.toString()); - return true; + return InteractionResult.SUCCESS; } boolean created = false; @@ -210,27 +218,27 @@ public class BlockScreen extends WDBlockContainer { if(te == null) { BlockPos bp = pos.toBlock(); - world.setBlockState(bp, world.getBlockState(bp).withProperty(hasTE, true)); - te = (TileEntityScreen) world.getTileEntity(bp); + world.setBlockAndUpdate(bp, world.getBlockState(bp).setValue(hasTE, true)); + te = (TileEntityScreen) world.getBlockEntity(bp); created = true; } te.addScreen(side, size, null, player, !created); - return true; + return InteractionResult.SUCCESS; } @Override - public void neighborChanged(IBlockState state, World world, BlockPos pos, Block block, BlockPos source) { - if(block != this && !world.isRemote && !state.getValue(emitting)) { + public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block, BlockPos source, boolean isMoving) { + if(block != this && !world.isClientSide && !state.getValue(emitting)) { for(BlockSide side: BlockSide.values()) { Vector3i vec = new Vector3i(pos); Multiblock.findOrigin(world, vec, side, null); - TileEntityScreen tes = (TileEntityScreen) world.getTileEntity(vec.toBlock()); + TileEntityScreen tes = (TileEntityScreen) world.getBlockEntity(vec.toBlock()); if(tes != null && tes.hasUpgrade(side, DefaultUpgrade.REDSTONE_INPUT)) { - EnumFacing facing = EnumFacing.VALUES[side.reverse().ordinal()]; //Opposite face + Direction facing = Direction.from2DDataValue(side.reverse().ordinal()); //Opposite face vec.sub(pos.getX(), pos.getY(), pos.getZ()).neg(); - tes.updateJSRedstone(side, new Vector2i(vec.dot(side.right), vec.dot(side.up)), world.getRedstonePower(pos, facing)); + tes.updateJSRedstone(side, new Vector2i(vec.dot(side.right), vec.dot(side.up)), world.getSignal(pos, facing)); } } } @@ -294,19 +302,21 @@ public class BlockScreen extends WDBlockContainer { return false; } - @Nullable + @org.jetbrains.annotations.Nullable @Override - public TileEntity createNewTileEntity(@Nonnull World world, int meta) { + public BlockEntity newBlockEntity(BlockPos pos, BlockState state) { + int meta = getMetaFromState(state); + if((meta & 1) == 0) return null; - return ((meta & 1) == 0) ? null : new TileEntityScreen(); + return ((meta & 1) == 0) ? null : new TileEntityScreen(pos, state); } /************************************************* DESTRUCTION HANDLING *************************************************/ - private void onDestroy(World world, BlockPos pos, EntityPlayer ply) { - if(!world.isRemote) { + private void onDestroy(Level world, BlockPos pos, Player ply) { + if(!world.isClientSide) { Vector3i bp = new Vector3i(pos); Multiblock.BlockOverride override = new Multiblock.BlockOverride(bp, Multiblock.OverrideAction.SIMULATE); @@ -315,7 +325,7 @@ public class BlockScreen extends WDBlockContainer { } } - private void destroySide(World world, Vector3i pos, BlockSide side, Multiblock.BlockOverride override, EntityPlayer source) { + private void destroySide(Level world, Vector3i pos, BlockSide side, Multiblock.BlockOverride override, Player source) { Multiblock.findOrigin(world, pos, side, override); BlockPos bp = pos.toBlock(); TileEntity te = world.getTileEntity(bp); @@ -361,25 +371,24 @@ public class BlockScreen extends WDBlockContainer { } @Override - @Nonnull - public EnumPushReaction getMobilityFlag(IBlockState state) { - return EnumPushReaction.IGNORE; + public PushReaction getPistonPushReaction(BlockState state) { + return PushReaction.IGNORE; } @Override - public int getWeakPower(IBlockState state, IBlockAccess world, BlockPos pos, EnumFacing side) { + public int getSignal(BlockState state, BlockGetter level, BlockPos pos, Direction direction) { return state.getValue(emitting) ? 15 : 0; } @Override - public boolean canProvidePower(IBlockState state) { + public boolean isSignalSource(BlockState state) { return state.getValue(emitting); } - @Override - protected BlockItem createItemBlock() { - return new ItemBlockScreen(this); - } +// @Override //TODO: Add this +// protected BlockItem createItemBlock() { +// return new ItemBlockScreen(this); +// } private static class ItemBlockScreen extends BlockItem implements WDItem { diff --git a/src/main/java/net/montoyo/wd/client/ClientProxy.java b/src/main/java/net/montoyo/wd/client/ClientProxy.java index 03b09f0..a9df737 100644 --- a/src/main/java/net/montoyo/wd/client/ClientProxy.java +++ b/src/main/java/net/montoyo/wd/client/ClientProxy.java @@ -53,6 +53,8 @@ import net.montoyo.wd.core.HasAdvancement; import net.montoyo.wd.core.JSServerRequest; import net.montoyo.wd.data.GuiData; import net.montoyo.wd.entity.TileEntityScreen; +import net.montoyo.wd.init.BlockInit; +import net.montoyo.wd.init.ItemInit; import net.montoyo.wd.item.WDItem; import net.montoyo.wd.miniserv.client.Client; import net.montoyo.wd.net.Messages; @@ -572,7 +574,7 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer //Laser pointer raycast boolean raycastHit = false; - if(mc.player != null && mc.level != null && mc.player.getItemInHand(InteractionHand.MAIN_HAND).getItem() == WebDisplays.INSTANCE.itemLaserPointer + if(mc.player != null && mc.level != null && mc.player.getItemInHand(InteractionHand.MAIN_HAND).getItem().equals(ItemInit.itemLaserPointer.get()) && mc.options.keyUse.isDown() && (mc.hitResult == null || mc.hitResult.getType() != HitResult.Type.BLOCK)) { laserPointerRenderer.isOn = true; @@ -580,7 +582,7 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer BlockPos bpos = result.getBlockPos(); - if(result.getType() == HitResult.Type.BLOCK && mc.level.getBlockState(bpos).getBlock() == WebDisplays.INSTANCE.blockScreen) { + if(result.getType() == HitResult.Type.BLOCK && mc.level.getBlockState(bpos).getBlock() == BlockInit.blockScreen.get()) { Vector3i pos = new Vector3i(result.getBlockPos()); BlockSide side = BlockSide.values()[result.getDirection().ordinal()]; @@ -626,9 +628,9 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer Item item = ev.getItemStack().getItem(); IItemRenderer renderer; - if(item == WebDisplays.INSTANCE.itemMinePad) + if(item == ItemInit.itemMinePad.get()) renderer = minePadRenderer; - else if(item == WebDisplays.INSTANCE.itemLaserPointer) + else if(item == ItemInit.itemLaserPointer.get()) renderer = laserPointerRenderer; else return; @@ -691,7 +693,7 @@ public class ClientProxy extends SharedProxy implements IDisplayHandler, IJSQuer for(int i = 0; i < cnt; i++) { ItemStack item = inv.get(i); - if(item.getItem() == WebDisplays.INSTANCE.itemMinePad) { + if(item.getItem() == ItemInit.itemMinePad.get()) { CompoundTag tag = item.getTag(); if(tag != null && tag.contains("PadID")) diff --git a/src/main/java/net/montoyo/wd/client/gui/GuiMinePad.java b/src/main/java/net/montoyo/wd/client/gui/GuiMinePad.java index 986a677..4d40331 100644 --- a/src/main/java/net/montoyo/wd/client/gui/GuiMinePad.java +++ b/src/main/java/net/montoyo/wd/client/gui/GuiMinePad.java @@ -4,22 +4,20 @@ package net.montoyo.wd.client.gui; -import com.mojang.blaze3d.vertex.BufferBuilder; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.util.math.BlockPos; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.*; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; +import net.minecraftforge.api.distmarker.OnlyIn; import net.montoyo.wd.WebDisplays; import net.montoyo.wd.client.ClientProxy; import net.montoyo.wd.utilities.BlockSide; -import org.lwjgl.input.Keyboard; -import org.lwjgl.input.Mouse; +import org.lwjgl.glfw.GLFW; -import static org.lwjgl.opengl.GL11.*; +import static net.minecraftforge.api.distmarker.Dist.CLIENT; +import static org.lwjgl.opengl.GL11.glColor4f; -@SideOnly(Side.CLIENT) +@OnlyIn(CLIENT) public class GuiMinePad extends WDScreen { private ClientProxy.PadData pad; @@ -29,15 +27,17 @@ public class GuiMinePad extends WDScreen { private double vh; public GuiMinePad() { + super(Component.nullToEmpty(null)); } public GuiMinePad(ClientProxy.PadData pad) { + this(); this.pad = pad; } @Override - public void initGui() { - super.initGui(); + public void init() { + super.init(); vw = ((double) width) - 32.0f; vh = vw / WebDisplays.PAD_RATIO; @@ -53,87 +53,122 @@ public class GuiMinePad extends WDScreen { } @Override - public void drawScreen(int mouseX, int mouseY, float ptt) { - drawDefaultBackground(); + public void render(PoseStack poseStack, int mouseX, int mouseY, float ptt) { + renderBackground(poseStack); - glDisable(GL_TEXTURE_2D); - glDisable(GL_CULL_FACE); - glColor4f(0.73f, 0.73f, 0.73f, 1.0f); + RenderSystem.disableTexture(); + RenderSystem.disableCull(); + RenderSystem.setShaderColor(0.73f, 0.73f, 0.73f, 1.0f); - Tessellator t = Tessellator.getInstance(); - BufferBuilder bb = t.getBuffer(); - bb.begin(GL_QUADS, DefaultVertexFormats.POSITION); + Tesselator t = Tesselator.getInstance(); + BufferBuilder bb = t.getBuilder(); + bb.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); addRect(bb, vx, vy - 16, vw, 16); addRect(bb, vx, vy + vh, vw, 16); addRect(bb, vx - 16, vy, 16, vh); addRect(bb, vx + vw, vy, 16, vh); - t.draw(); + t.end(); - glEnable(GL_TEXTURE_2D); + RenderSystem.enableTexture(); - if(pad.view != null) { + if (pad.view != null) { glColor4f(1.0f, 1.0f, 1.0f, 1.0f); pad.view.draw(vx, vy + vh, vx + vw, vy); } - glEnable(GL_CULL_FACE); + RenderSystem.enableCull(); } @Override - public void handleInput() { - while(Keyboard.next()) { - char key = Keyboard.getEventCharacter(); - int keycode = Keyboard.getEventKey(); - boolean pressed = Keyboard.getEventKeyState(); + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + key(keyCode, scanCode, true); + return super.keyPressed(keyCode, scanCode, modifiers); + } - if(Keyboard.getEventKey() == Keyboard.KEY_ESCAPE) { - mc.displayGuiScreen(null); - return; - } - if(pad.view != null) { - if(pressed) - pad.view.injectKeyPressedByKeyCode(keycode, key, 0); - else - pad.view.injectKeyReleasedByKeyCode(keycode, key, 0); - if(key != 0) - pad.view.injectKeyTyped(key, 0); - } + @Override + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + key(keyCode, scanCode, false); + return super.keyPressed(keyCode, scanCode, modifiers); + } + + public void key(int keyCode, int scanCode, boolean pressed) { + char key = getChar(keyCode, scanCode); + + if (pad.view != null) { + if (pressed) + pad.view.injectKeyPressedByKeyCode(keyCode, key, 0); + else + pad.view.injectKeyReleasedByKeyCode(keyCode, key, 0); + + if (key != 0) + pad.view.injectKeyTyped(key, 0); } + } + + @Override + public void mouseMoved(double mouseX, double mouseY) { + super.mouseMoved(mouseX, mouseY); + mouse(-1, false, (int) mouseX, (int) mouseY); + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + mouse(button, true, (int) mouseX, (int) mouseY); + return super.mouseClicked(mouseX, mouseY, button); + } + + @Override + public boolean mouseReleased(double mouseX, double mouseY, int button) { + mouse(button, false, (int) mouseX, (int) mouseY); + return super.mouseReleased(mouseX, mouseY, button); + } + + public void mouse(int btn, boolean pressed, int sx, int sy) { + int vx = screen2DisplayX((int) this.vx); int vy = screen2DisplayY((int) this.vy); int vh = screen2DisplayX((int) this.vh); int vw = screen2DisplayY((int) this.vw); - while(Mouse.next()) { - int btn = Mouse.getEventButton(); - boolean pressed = Mouse.getEventButtonState(); - int sx = Mouse.getEventX(); - int sy = Mouse.getEventY(); + if (pad.view != null && sx >= vx && sx <= vx + vw && sy >= vy && sy <= vy + vh) { + sx -= vx; + sy -= vy; + sy = vh - sy; - if(pad.view != null && sx >= vx && sx <= vx + vw && sy >= vy && sy <= vy + vh) { - sx -= vx; - sy -= vy; - sy = vh - sy; + //Scale again according to the webview + sx = (int) (((double) sx) / ((double) vw) * WebDisplays.INSTANCE.padResX); + sy = (int) (((double) sy) / ((double) vh) * WebDisplays.INSTANCE.padResY); - //Scale again according to the webview - sx = (int) (((double) sx) / ((double) vw) * WebDisplays.INSTANCE.padResX); - sy = (int) (((double) sy) / ((double) vh) * WebDisplays.INSTANCE.padResY); + if (btn == -1) + pad.view.injectMouseMove(sx, sy, 0, false); + else + pad.view.injectMouseButton(sx, sy, 0, btn + 1, pressed, 1); - if(btn == -1) - pad.view.injectMouseMove(sx, sy, 0, false); - else - pad.view.injectMouseButton(sx, sy, 0, btn + 1, pressed, 1); - } } } + public char getChar(int keyCode, int scanCode) { + String keystr = GLFW.glfwGetKeyName(keyCode, scanCode); + if(keystr == null){ + keystr = "\0"; + } + if(keyCode == GLFW.GLFW_KEY_ENTER){ + keystr = "\n"; + } + if(keystr.length() == 0){ + return (char) -1; + } + + return keystr.charAt(keystr.length() - 1); + } + @Override - public void updateScreen() { + public void tick() { if(pad.view == null) - mc.displayGuiScreen(null); //In case the user dies with the pad in the hand + minecraft.setScreen(null); //In case the user dies with the pad in the hand } @Override diff --git a/src/main/java/net/montoyo/wd/client/gui/GuiServer.java b/src/main/java/net/montoyo/wd/client/gui/GuiServer.java index 51a4213..c1bbf06 100644 --- a/src/main/java/net/montoyo/wd/client/gui/GuiServer.java +++ b/src/main/java/net/montoyo/wd/client/gui/GuiServer.java @@ -11,6 +11,7 @@ import net.minecraft.client.renderer.GlStateManager; import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.vertex.DefaultVertexFormats; import net.minecraft.client.resources.I18n; +import net.minecraft.core.BlockPos; import net.minecraft.util.ResourceLocation; import net.minecraft.util.SoundCategory; import net.minecraft.util.math.BlockPos; diff --git a/src/main/java/net/montoyo/wd/client/gui/GuiSetURL2.java b/src/main/java/net/montoyo/wd/client/gui/GuiSetURL2.java index 45f6e08..c41f123 100644 --- a/src/main/java/net/montoyo/wd/client/gui/GuiSetURL2.java +++ b/src/main/java/net/montoyo/wd/client/gui/GuiSetURL2.java @@ -4,11 +4,9 @@ package net.montoyo.wd.client.gui; -import net.minecraft.item.ItemStack; +import net.minecraft.core.BlockPos; +import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.EnumHand; -import net.minecraft.util.ResourceLocation; -import net.minecraft.util.math.BlockPos; import net.minecraft.world.InteractionHand; import net.minecraft.world.item.ItemStack; import net.montoyo.wd.WebDisplays; @@ -17,6 +15,7 @@ import net.montoyo.wd.client.gui.controls.Button; import net.montoyo.wd.client.gui.controls.TextField; import net.montoyo.wd.client.gui.loading.FillControl; import net.montoyo.wd.entity.TileEntityScreen; +import net.montoyo.wd.init.ItemInit; import net.montoyo.wd.net.Messages; import net.montoyo.wd.net.server.SMessagePadCtrl; import net.montoyo.wd.net.server.SMessageScreenCtrl; @@ -52,7 +51,7 @@ public class GuiSetURL2 extends WDScreen { private Button btnOk; public GuiSetURL2(TileEntityScreen tes, BlockSide side, String url, Vector3i rl) { - super(); + super(Component.nullToEmpty(null)); tileEntity = tes; screenSide = side; remoteLocation = rl; @@ -61,6 +60,7 @@ public class GuiSetURL2 extends WDScreen { } public GuiSetURL2(String url) { + super(Component.nullToEmpty(null)); isPad = true; screenURL = url; } @@ -105,7 +105,7 @@ public class GuiSetURL2 extends WDScreen { Messages.INSTANCE.sendToServer(new SMessagePadCtrl(url)); ItemStack held = minecraft.player.getItemInHand(InteractionHand.MAIN_HAND); - if(held.getItem() == WebDisplays.INSTANCE.itemMinePad && held.getTag() != null && held.getTag().contains("PadID")) { + if(held.getItem().equals(ItemInit.itemMinePad.get()) && held.getTag() != null && held.getTag().contains("PadID")) { ClientProxy.PadData pd = ((ClientProxy) WebDisplays.PROXY).getPadByID(held.getTag().getInt("PadID")); if(pd != null && pd.view != null) @@ -115,12 +115,12 @@ public class GuiSetURL2 extends WDScreen { Messages.INSTANCE.sendToServer(SMessageScreenCtrl.setURL(tileEntity, screenSide, url, remoteLocation)); } - mc.displayGuiScreen(null); + minecraft.setScreen(null); } @Override public boolean isForBlock(BlockPos bp, BlockSide side) { - return (remoteLocation != null && remoteLocation.equalsBlockPos(bp)) || (bp.equals(tileEntity.getPos()) && side == screenSide); + return (remoteLocation != null && remoteLocation.equalsBlockPos(bp)) || (bp.equals(tileEntity.getBlockPos()) && side == screenSide); } } diff --git a/src/main/java/net/montoyo/wd/client/gui/WDScreen.java b/src/main/java/net/montoyo/wd/client/gui/WDScreen.java index 24ee190..7d01b91 100644 --- a/src/main/java/net/montoyo/wd/client/gui/WDScreen.java +++ b/src/main/java/net/montoyo/wd/client/gui/WDScreen.java @@ -12,9 +12,11 @@ import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.core.BlockPos; import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.FormattedText; +import net.minecraft.network.chat.Style; import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.FormattedCharSequence; import net.minecraft.world.item.ItemStack; -import net.montoyo.wd.WebDisplays; import net.montoyo.wd.client.gui.controls.Container; import net.montoyo.wd.client.gui.controls.Control; import net.montoyo.wd.client.gui.controls.Event; @@ -37,7 +39,9 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public abstract class WDScreen extends Screen { @@ -117,10 +121,10 @@ public abstract class WDScreen extends Screen { renderBackground(poseStack); for(Control ctrl: controls) - ctrl.draw(mouseX, mouseY, ptt); + ctrl.draw(poseStack, mouseX, mouseY, ptt); for(Control ctrl: postDrawList) - ctrl.postDraw(mouseX, mouseY, ptt); + ctrl.postDraw(poseStack, mouseX, mouseY, ptt); } @Override @@ -135,7 +139,7 @@ public abstract class WDScreen extends Screen { for(Control ctrl: controls) typed = typed || ctrl.keyTyped(codePoint, modifiers); - return typed; + return typed || charTyped(codePoint, modifiers); } @Override @@ -145,7 +149,7 @@ public abstract class WDScreen extends Screen { for(Control ctrl: controls) clicked = clicked || ctrl.mouseClicked(mouseX, mouseY, button); - return clicked; + return clicked || mouseClicked(mouseX, mouseY, button); } @Override @@ -155,13 +159,17 @@ public abstract class WDScreen extends Screen { for(Control ctrl: controls) mouseReleased = mouseReleased || ctrl.mouseReleased(mouseX, mouseY, button); - return mouseReleased; + return mouseReleased || mouseClicked(mouseX, mouseY, button); } @Override - protected void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) { + public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) { + boolean dragged = false; + for(Control ctrl: controls) - ctrl.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick); + dragged = dragged || ctrl.mouseClickMove(mouseX, mouseY, button, dragX, dragX); + + return dragged || mouseDragged(mouseX, mouseY, button, dragX, dragY); } @Override @@ -185,54 +193,43 @@ public abstract class WDScreen extends Screen { } @Override - public boolean mouseDragged(double mouseX, double mouseY, int button, double dragX, double dragY) { - return super.mouseDragged(mouseX, mouseY, button, dragX, dragY); + public boolean mouseScrolled(double mouseX, double mouseY, double delta) { + boolean scrolled = false; + for(Control ctrl : controls) + scrolled = scrolled || ctrl.mouseScroll(mouseX, mouseY, delta); + return scrolled; } @Override public void mouseMoved(double mouseX, double mouseY) { - onMouseMove(mouseX, mouseY); - } + boolean moved = false; - @Override - public void handleMouseInput() throws IOException { - super.handleMouseInput(); - - int x = Mouse.getEventX() * width / mc.displayWidth; - int y = height - Mouse.getEventY() * height / mc.displayHeight - 1; - int dw = Mouse.getEventDWheel(); - - if(dw != 0) - onMouseScroll(x, y, dw); - else if(Mouse.getEventButton() == -1) - onMouseMove(x, y); - } - - @Override - public void handleKeyboardInput() throws IOException { - super.handleKeyboardInput(); - - int key = Keyboard.getEventKey(); - if(key != Keyboard.KEY_NONE) { - if(Keyboard.getEventKeyState()) { - for(Control ctrl : controls) - ctrl.keyDown(key); - } else { - for(Control ctrl : controls) - ctrl.keyUp(key); - } - } - } - - public void onMouseScroll(int mouseX, int mouseY, int amount) { for(Control ctrl : controls) - ctrl.mouseScroll(mouseX, mouseY, amount); + moved = moved || ctrl.mouseMove(mouseX, mouseY); + + super.mouseMoved(mouseX, mouseY); } - public void onMouseMove(int mouseX, int mouseY) { + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + boolean down = false; + for(Control ctrl : controls) + down = down || ctrl.keyDown(keyCode); + + return down || super.keyPressed(keyCode, scanCode, modifiers); + } + + @Override + public boolean keyReleased(int keyCode, int scanCode, int modifiers) { + boolean up = false; + + for(Control ctrl : controls) + up = up || ctrl.keyUp(keyCode); + + return up || super.keyReleased(keyCode, scanCode, modifiers); } public Object actionPerformed(Event ev) { @@ -363,13 +360,12 @@ public abstract class WDScreen extends Screen { } } - public void drawItemStackTooltip(ItemStack is, int x, int y) { - - renderToolTip(is, x, y); //Since it's protected... + public void drawItemStackTooltip(PoseStack poseStack, ItemStack is, int x, int y) { + renderTooltip(poseStack, is, x, y); //Since it's protected... } - public void drawTooltip(java.util.List lines, int x, int y) { - drawHoveringText(lines, x, y, font); //This is also protected... + public void drawTooltip(PoseStack poseStack, List lines, int x, int y) { + renderTooltip(poseStack, lines.stream().map(a -> FormattedCharSequence.forward(a, Style.EMPTY)).collect(Collectors.toList()), x, y, font); //This is also protected... } public void requirePostDraw(Control ctrl) { @@ -389,4 +385,6 @@ public abstract class WDScreen extends Screen { return null; } + //Bypass for needing to use Components + } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Button.java b/src/main/java/net/montoyo/wd/client/gui/controls/Button.java index 007df39..c66a644 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Button.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Button.java @@ -4,13 +4,14 @@ package net.montoyo.wd.client.gui.controls; -import net.minecraft.client.gui.GuiButton; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.network.chat.Component; import net.montoyo.wd.client.gui.loading.JsonOWrapper; -import org.lwjgl.input.Keyboard; +import org.lwjgl.glfw.GLFW; public class Button extends Control { - protected final GuiButton btn; + protected final net.minecraft.client.gui.components.Button btn; protected boolean selected = false; protected boolean shiftDown = false; protected int originalColor = 0; @@ -32,47 +33,55 @@ public class Button extends Control { } public Button() { - btn = new GuiButton(0, 0, 0, ""); + btn = new net.minecraft.client.gui.components.Button(0,0, 0, 0, Component.nullToEmpty(null), a -> {}); } public Button(String text, int x, int y, int width) { - btn = new GuiButton(0, x, y, width, 20, text); + btn = new net.minecraft.client.gui.components.Button(x, y, width, 20, Component.nullToEmpty(text), a -> {}); } public Button(String text, int x, int y) { - btn = new GuiButton(0, x, y, text); + btn = new net.minecraft.client.gui.components.Button(0, 0, x, y, Component.nullToEmpty(text), a -> {}); } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - if(mouseButton == 0 && btn.mousePressed(mc, mouseX, mouseY)) { + public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { + if(mouseButton == 0 && btn.mouseClicked(mouseX, mouseY, mouseButton)) { selected = true; - btn.playPressSound(mc.getSoundHandler()); + btn.playDownSound(mc.getSoundManager()); if(!onClick()) parent.actionPerformed(new ClickEvent(this)); + + return true; } + + return false; } @Override - public void mouseReleased(int mouseX, int mouseY, int state) { + public boolean mouseReleased(double mouseX, double mouseY, int state) { if(selected && state == 0) { - btn.mouseReleased(mouseX, mouseY); + btn.mouseReleased(mouseX, mouseY,state); selected = false; + + return true; } + + return true; } @Override - public void draw(int mouseX, int mouseY, float ptt) { - btn.drawButton(mc, mouseX, mouseY, ptt); + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { + btn.render(poseStack, mouseX, mouseY, ptt); } public void setLabel(String label) { - btn.displayString = label; + btn.setMessage(Component.nullToEmpty(label)); } public String getLabel() { - return btn.displayString; + return btn.getMessage().getString(); } public void setWidth(int width) { @@ -81,7 +90,7 @@ public class Button extends Control { @Override public int getWidth() { - return btn.getButtonWidth(); + return btn.getWidth(); } @Override @@ -105,24 +114,24 @@ public class Button extends Control { return btn.y; } - public GuiButton getMcButton() { + public net.minecraft.client.gui.components.Button getMcButton() { return btn; } public void setDisabled(boolean dis) { - btn.enabled = !dis; + btn.active = !dis; } public boolean isDisabled() { - return !btn.enabled; + return !btn.active; } public void enable() { - btn.enabled = true; + btn.active = true; } public void disable() { - btn.enabled = false; + btn.active = false; } public void setVisible(boolean visible) { @@ -146,35 +155,43 @@ public class Button extends Control { } @Override - public void keyUp(int key) { - if(key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT) { + public boolean keyUp(int key) { + if(key == GLFW.GLFW_KEY_LEFT_SHIFT || key == GLFW.GLFW_KEY_RIGHT_SHIFT) { shiftDown = false; - btn.packedFGColour = originalColor; + btn.setFGColor(originalColor); + + return true; } + + return false; } @Override - public void keyDown(int key) { - if(key == Keyboard.KEY_LSHIFT || key == Keyboard.KEY_RSHIFT) { + public boolean keyDown(int key) { + if(key == GLFW.GLFW_KEY_LEFT_SHIFT || key == GLFW.GLFW_KEY_RIGHT_SHIFT) { shiftDown = true; - btn.packedFGColour = shiftColor; + btn.setFGColor(shiftColor); + + return true; } + + return false; } public void setTextColor(int color) { originalColor = color; if(!shiftDown) - btn.packedFGColour = color; + btn.setFGColor(color); } public int getTextColor() { - return btn.packedFGColour; + return btn.getFGColor(); } public void setShiftTextColor(int shiftColor) { this.shiftColor = shiftColor; if(shiftDown) - btn.packedFGColour = shiftColor; + btn.setFGColor(shiftColor); } public int getShiftTextColor() { @@ -186,14 +203,14 @@ public class Button extends Control { super.load(json); btn.x = json.getInt("x", 0); btn.y = json.getInt("y", 0); - btn.width = json.getInt("width", 200); - btn.displayString = tr(json.getString("label", btn.displayString)); - btn.enabled = !json.getBool("disabled", !btn.enabled); + btn.setWidth(json.getInt("width", 200)); + btn.setMessage(Component.nullToEmpty(tr(json.getString("label", btn.getMessage().getContents())))); + btn.active = !json.getBool("disabled", !btn.active); btn.visible = json.getBool("visible", btn.visible); originalColor = json.getColor("color", originalColor); shiftColor = json.getColor("shiftColor", shiftColor); - btn.packedFGColour = originalColor; + btn.setFGColor(originalColor); } protected boolean onClick() { diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java b/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java index f11a376..a46f647 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/CheckBox.java @@ -4,13 +4,16 @@ package net.montoyo.wd.client.gui.controls; -import net.minecraft.client.audio.PositionedSoundRecord; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.init.SoundEvents; -import net.minecraft.util.ResourceLocation; +import com.google.common.collect.Lists; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.resources.sounds.SimpleSoundInstance; +import net.minecraft.network.chat.Component; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.sounds.SoundEvents; import net.montoyo.wd.client.gui.loading.JsonOWrapper; -import java.util.Arrays; +import java.util.stream.Collectors; +import java.util.stream.Stream; public class CheckBox extends BasicControl { @@ -45,7 +48,7 @@ public class CheckBox extends BasicControl { public CheckBox(int x, int y, String label) { this.label = label; - labelW = font.getStringWidth(label); + labelW = font.width(label); checked = false; this.x = x; this.y = y; @@ -53,42 +56,46 @@ public class CheckBox extends BasicControl { public CheckBox(int x, int y, String label, boolean val) { this.label = label; - labelW = font.getStringWidth(label); + labelW = font.width(label); checked = val; this.x = x; this.y = y; } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { + public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { if(mouseButton == 0 && !disabled) { if(mouseX >= x && mouseX <= x + WIDTH + 2 + labelW && mouseY >= y && mouseY < y + HEIGHT) { checked = !checked; - mc.getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + mc.getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)); parent.actionPerformed(new CheckedEvent(this)); } + + return true; } + + return false; } @Override - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(visible) { - GlStateManager.disableAlpha(); +// GlStateManager.disableAlpha(); bindTexture(checked ? texChecked : texUnchecked); blend(true); - fillTexturedRect(x, y, WIDTH, HEIGHT, 0.0, 0.0, 1.0, 1.0); + fillTexturedRect(poseStack, x, y, WIDTH, HEIGHT, 0.0, 0.0, 1.0, 1.0); blend(false); bindTexture(null); boolean inside = (!disabled && mouseX >= x && mouseX <= x + WIDTH + 2 + labelW && mouseY >= y && mouseY < y + HEIGHT); - font.drawString(label, x + WIDTH + 2, y + 4, inside ? 0xFF0080FF : COLOR_WHITE); + font.draw(poseStack, label, x + WIDTH + 2, y + 4, inside ? 0xFF0080FF : COLOR_WHITE); } } public void setLabel(String label) { this.label = label; - labelW = font.getStringWidth(label); + labelW = font.width(label); } public String getLabel() { @@ -117,20 +124,20 @@ public class CheckBox extends BasicControl { public void load(JsonOWrapper json) { super.load(json); label = tr(json.getString("label", "")); - labelW = font.getStringWidth(label); + labelW = font.width(label); checked = json.getBool("checked", false); String tt = tr(json.getString("tooltip", "")); if(!tt.isEmpty()) { - tooltip = Arrays.asList(tt.split("\\\\n")); + tooltip = Lists.newArrayList(tt.split("\\\\n")); parent.requirePostDraw(this); } } @Override - public void postDraw(int mouseX, int mouseY, float ptt) { + public void postDraw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(tooltip != null && !disabled && mouseX >= x && mouseX <= x + WIDTH + 2 + labelW && mouseY >= y && mouseY < y + HEIGHT) - parent.drawTooltip(tooltip, mouseX, mouseY); + parent.drawTooltip(poseStack, tooltip, mouseX, mouseY); } } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Container.java b/src/main/java/net/montoyo/wd/client/gui/controls/Container.java index 4f585c2..c507bfb 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Container.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Container.java @@ -4,12 +4,12 @@ package net.montoyo.wd.client.gui.controls; +import com.mojang.blaze3d.vertex.PoseStack; import net.montoyo.wd.client.gui.loading.GuiLoader; import net.montoyo.wd.client.gui.loading.JsonAWrapper; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import org.lwjgl.opengl.GL11; -import java.io.IOException; import java.util.ArrayList; public abstract class Container extends BasicControl { @@ -24,86 +24,119 @@ public abstract class Container extends BasicControl { } @Override - public void keyTyped(int keyCode, int scanCode, int modifiers) throws IOException { + public boolean keyTyped(char typedChar, int keyCode) { + boolean typed = false; + if(!disabled) { for(Control ctrl : childs) - ctrl.keyTyped(keyCode, scanCode, modifiers); + typed = typed || ctrl.keyTyped(typedChar, keyCode); } + + return typed; } @Override - public void keyUp(int key) { + public boolean keyUp(int key) { + boolean up = false; + if(!disabled) { for(Control ctrl : childs) - ctrl.keyUp(key); + up = up || ctrl.keyUp(key); } + + return up; } @Override - public void keyDown(int key) { + public boolean keyDown(int key) { + boolean down = false; + if(!disabled) { for(Control ctrl : childs) - ctrl.keyDown(key); + down = down || ctrl.keyDown(key); } + + return down; } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { + public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { + boolean clicked = false; + if(!disabled) { mouseX -= x + paddingX; mouseY -= y + paddingY; for(Control ctrl : childs) - ctrl.mouseClicked(mouseX, mouseY, mouseButton); + clicked = clicked || ctrl.mouseClicked(mouseX, mouseY, mouseButton); } + + return clicked; } @Override - public void mouseReleased(int mouseX, int mouseY, int state) { + public boolean mouseReleased(double mouseX, double mouseY, int state) { + boolean released = false; + if(!disabled) { mouseX -= x + paddingX; mouseY -= y + paddingY; for(Control ctrl : childs) - ctrl.mouseReleased(mouseX, mouseY, state); + released = released || ctrl.mouseReleased(mouseX, mouseY, state); } + + return released; } @Override - public void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) { + public boolean mouseClickMove(double mouseX, double mouseY, int button, double dragX, double dragY) { + boolean clicked = false; + if(!disabled) { mouseX -= x + paddingX; mouseY -= y + paddingY; for(Control ctrl : childs) - ctrl.mouseClickMove(mouseX, mouseY, clickedMouseButton, timeSinceLastClick); + clicked = clicked || ctrl.mouseClickMove(mouseX, mouseY, button, dragX, dragY); } + + return clicked; } @Override - public void mouseMove(int mouseX, int mouseY) { + public boolean mouseMove(double mouseX, double mouseY) { + boolean clicked = false; + + if(!disabled) { + mouseX -= x + paddingX; + mouseY -= y + paddingY; + + + for(Control ctrl : childs) + clicked = clicked || ctrl.mouseMove(mouseX, mouseY); + } + + return clicked; + } + + @Override + public boolean mouseScroll(double mouseX, double mouseY, double amount) { + boolean scrolled = false; + if(!disabled) { mouseX -= x + paddingX; mouseY -= y + paddingY; for(Control ctrl : childs) - ctrl.mouseMove(mouseX, mouseY); + scrolled = scrolled || ctrl.mouseScroll(mouseX, mouseY, amount); } + + return scrolled; } @Override - public void mouseScroll(int mouseX, int mouseY, int amount) { - if(!disabled) { - mouseX -= x + paddingX; - mouseY -= y + paddingY; - - for(Control ctrl : childs) - ctrl.mouseScroll(mouseX, mouseY, amount); - } - } - - @Override - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(visible) { mouseX -= x + paddingX; mouseY -= y + paddingY; @@ -113,10 +146,10 @@ public abstract class Container extends BasicControl { if(disabled) { for(Control ctrl : childs) - ctrl.draw(-1, -1, ptt); + ctrl.draw(poseStack, -1, -1, ptt); } else { for(Control ctrl : childs) - ctrl.draw(mouseX, mouseY, ptt); + ctrl.draw(poseStack, mouseX, mouseY, ptt); } GL11.glPopMatrix(); diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Control.java b/src/main/java/net/montoyo/wd/client/gui/controls/Control.java index c8b4af0..119581f 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Control.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Control.java @@ -4,27 +4,21 @@ package net.montoyo.wd.client.gui.controls; -import com.mojang.blaze3d.vertex.BufferBuilder; -import com.mojang.blaze3d.vertex.DefaultVertexFormat; -import com.mojang.blaze3d.vertex.Tesselator; -import com.mojang.blaze3d.vertex.VertexFormat; +import com.mojang.blaze3d.pipeline.RenderTarget; +import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.*; +import com.mojang.math.Matrix4f; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.Font; -import net.minecraft.client.gui.FontRenderer; -import net.minecraft.client.renderer.BufferBuilder; -import net.minecraft.client.renderer.GlStateManager; -import net.minecraft.client.renderer.Tessellator; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; -import net.minecraft.client.resources.I18n; -import net.minecraft.client.shader.Framebuffer; -import net.minecraft.util.ResourceLocation; +import net.minecraft.client.resources.language.I18n; +import net.minecraft.resources.ResourceLocation; import net.montoyo.wd.client.gui.WDScreen; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import net.montoyo.wd.utilities.Bounds; -import java.io.IOException; - -import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.glEnable; public abstract class Control { @@ -62,22 +56,26 @@ public abstract class Control { } public boolean keyTyped(char typedChar, int keyCode) { + return false; } public boolean keyUp(int key) { + return false; } public boolean keyDown(int key) { + return false; } public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { + return false; } public boolean mouseReleased(double mouseX, double mouseY, int state) { return false; } - public boolean mouseClickMove(double mouseX, double mouseY, int clickedMouseButton, long timeSinceLastClick) { + public boolean mouseClickMove(double mouseX, double mouseY, int button, double dragX, double dragY) { return false; } @@ -85,14 +83,14 @@ public abstract class Control { return false; } - public boolean mouseScroll(int mouseX, int mouseY, int amount) { + public boolean mouseScroll(double mouseX, double mouseY, double amount) { return false; } - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { } - public void postDraw(int mouseX, int mouseY, float ptt) { + public void postDraw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { } public void destroy() { @@ -108,7 +106,7 @@ public abstract class Control { public abstract int getHeight(); public abstract void setPos(int x, int y); - public void fillRect(int x, int y, int w, int h, int color) { + public void fillRect(int x, double y, int w, int h, int color) { double x1 = (double) x; double y1 = (double) y; double x2 = (double) (x + w); @@ -118,10 +116,10 @@ public abstract class Control { int g = (color >> 8 ) & 0xFF; int b = color & 0xFF; - glColor4f(((float) r) / 255.f, ((float) g) / 255.f, ((float) b) / 255.f, ((float) a) / 255.f); - glDisable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + RenderSystem.setShaderColor(((float) r) / 255.f, ((float) g) / 255.f, ((float) b) / 255.f, ((float) a) / 255.f); + RenderSystem.disableTexture(); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); vBuffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); vBuffer.vertex(x1, y2, 0.0).endVertex(); @@ -130,44 +128,44 @@ public abstract class Control { vBuffer.vertex(x1, y1, 0.0).endVertex(); tessellator.end(); - glDisable(GL_BLEND); - glEnable(GL_TEXTURE_2D); + RenderSystem.disableBlend(); + RenderSystem.enableTexture();glEnable(GL_TEXTURE_2D); } - public void fillTexturedRect(int x, int y, int w, int h, double u1, double v1, double u2, double v2) { + public void fillTexturedRect(PoseStack poseStack, int x, int y, int w, int h, double u1, double v1, double u2, double v2) { double x1 = (double) x; double y1 = (double) y; double x2 = (double) (x + w); double y2 = (double) (y + h); - vBuffer.begin(GL_QUADS, DefaultVertexFormats.POSITION_TEX_COLOR); - vBuffer.pos(x1, y2, 0.0).tex(u1, v2).color(255, 255, 255, 255).endVertex(); - vBuffer.pos(x2, y2, 0.0).tex(u2, v2).color(255, 255, 255, 255).endVertex(); - vBuffer.pos(x2, y1, 0.0).tex(u2, v1).color(255, 255, 255, 255).endVertex(); - vBuffer.pos(x1, y1, 0.0).tex(u1, v1).color(255, 255, 255, 255).endVertex(); - tessellator.draw(); + vBuffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_TEX_COLOR); + vBuffer.vertex(x1, y2, 0.0).uv((float) u1, (float) v2).color(255, 255, 255, 255).endVertex(); + vBuffer.vertex(x2, y2, 0.0).uv((float) u2, (float) v2).color(255, 255, 255, 255).endVertex(); + vBuffer.vertex(x2, y1, 0.0).uv((float) u2, (float) v1).color(255, 255, 255, 255).endVertex(); + vBuffer.vertex(x1, y1, 0.0).uv((float) u1, (float) v1).color(255, 255, 255, 255).endVertex(); + tessellator.end(); } public static void blend(boolean enable) { if(enable) { - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_CONSTANT_ALPHA); } else - glDisable(GL_BLEND); + RenderSystem.disableBlend(); } public void bindTexture(ResourceLocation resLoc) { if(resLoc == null) - GlStateManager.bindTexture(0); //Damn state manager + RenderSystem.setShaderTexture(0, 0); //Damn state manager else - mc.renderEngine.bindTexture(resLoc); + RenderSystem.setShaderTexture(0, resLoc); } - public void drawBorder(int x, int y, int w, int h, int color) { - drawBorder(x, y, w, h, color, 1.0); + public void drawBorder(PoseStack poseStack, int x, int y, int w, int h, int color) { + drawBorder(poseStack, x, y, w, h, color, 1.0); } - public void drawBorder(int x, int y, int w, int h, int color, double sz) { + public void drawBorder(PoseStack poseStack, int x, int y, int w, int h, int color, double sz) { double x1 = (double) x; double y1 = (double) y; double x2 = (double) (x + w); @@ -177,65 +175,70 @@ public abstract class Control { int g = (color >> 8 ) & 0xFF; int b = color & 0xFF; - glColor4f(((float) r) / 255.f, ((float) g) / 255.f, ((float) b) / 255.f, ((float) a) / 255.f); - glDisable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + RenderSystem.setShaderColor(((float) r) / 255.f, ((float) g) / 255.f, ((float) b) / 255.f, ((float) a) / 255.f); + RenderSystem.enableTexture(); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); - vBuffer.begin(GL_QUADS, DefaultVertexFormats.POSITION); + vBuffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); //Top edge (y = y1) - vBuffer.pos(x1, y1 + sz, 0.0).endVertex(); - vBuffer.pos(x2, y1 + sz, 0.0).endVertex(); - vBuffer.pos(x2, y1, 0.0).endVertex(); - vBuffer.pos(x1, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1 + sz, 0.0).endVertex(); + vBuffer.vertex(x2, y1 + sz, 0.0).endVertex(); + vBuffer.vertex(x2, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1, 0.0).endVertex(); //Bottom edge (y = y2) - vBuffer.pos(x1, y2, 0.0).endVertex(); - vBuffer.pos(x2, y2, 0.0).endVertex(); - vBuffer.pos(x2, y2 - sz, 0.0).endVertex(); - vBuffer.pos(x1, y2 - sz, 0.0).endVertex(); + vBuffer.vertex(x1, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y2 - sz, 0.0).endVertex(); + vBuffer.vertex(x1, y2 - sz, 0.0).endVertex(); //Left edge (x = x1) - vBuffer.pos(x1, y2, 0.0).endVertex(); - vBuffer.pos(x1 + sz, y2, 0.0).endVertex(); - vBuffer.pos(x1 + sz, y1, 0.0).endVertex(); - vBuffer.pos(x1, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y2, 0.0).endVertex(); + vBuffer.vertex(x1 + sz, y2, 0.0).endVertex(); + vBuffer.vertex(x1 + sz, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1, 0.0).endVertex(); //Right edge (x = x2) - vBuffer.pos(x2 - sz, y2, 0.0).endVertex(); - vBuffer.pos(x2, y2, 0.0).endVertex(); - vBuffer.pos(x2, y1, 0.0).endVertex(); - vBuffer.pos(x2 - sz, y1, 0.0).endVertex(); - tessellator.draw(); + vBuffer.vertex(x2 - sz, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y1, 0.0).endVertex(); + vBuffer.vertex(x2 - sz, y1, 0.0).endVertex(); + tessellator.end(); - glDisable(GL_BLEND); - glEnable(GL_TEXTURE_2D); + RenderSystem.disableBlend(); + RenderSystem.enableTexture(); } - public void beginFramebuffer(Framebuffer fbo, int vpW, int vpH) { - fbo.bindFramebuffer(true); - glMatrixMode(GL_PROJECTION); - glPushMatrix(); - glLoadIdentity(); - glOrtho(0.0, (double) vpW, (double) vpH, 0.0, -1.0,1.0); - glMatrixMode(GL_MODELVIEW); - glPushMatrix(); - glLoadIdentity(); + public PoseStack beginFramebuffer(RenderTarget fbo, float vpW, float vpH) { + fbo.bindWrite(true); + + RenderSystem.backupProjectionMatrix(); + RenderSystem.setProjectionMatrix(Matrix4f.orthographic(0.0f, vpW, vpH, 0.0f, -1.0f,1.0f)); + + PoseStack poseStack = RenderSystem.getModelViewStack(); + poseStack.pushPose(); + poseStack.setIdentity(); +// poseStack.mulPose(Vector3f.XP.rotationDegrees(180.0f)); + RenderSystem.applyModelViewMatrix(); if(!fbo.useDepth) - glDisable(GL_DEPTH_TEST); + RenderSystem.disableDepthTest(); + + return poseStack; } - public void endFramebuffer(Framebuffer fbo) { + public void endFramebuffer(PoseStack poseStack, RenderTarget fbo) { if(!fbo.useDepth) - glEnable(GL_DEPTH_TEST); + RenderSystem.enableDepthTest(); - glPopMatrix(); - glMatrixMode(GL_PROJECTION); - glPopMatrix(); - glMatrixMode(GL_MODELVIEW); - fbo.unbindFramebuffer(); - mc.getFramebuffer().bindFramebuffer(true); + + RenderSystem.colorMask(true, true, true, true); + RenderSystem.restoreProjectionMatrix(); + poseStack.popPose(); + RenderSystem.applyModelViewMatrix(); + fbo.unbindWrite(); + mc.getMainRenderTarget().bindWrite(true); } public static String tr(String text) { @@ -243,7 +246,7 @@ public abstract class Control { if(text.charAt(1) == '$') return text.substring(1); else - return I18n.format(text.substring(1)); + return I18n.get(text.substring(1)); } else return text; } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java b/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java index 23c23d5..bc3dc49 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/ControlGroup.java @@ -4,7 +4,11 @@ package net.montoyo.wd.client.gui.controls; -import net.minecraft.client.renderer.vertex.DefaultVertexFormats; +import com.mojang.blaze3d.platform.GlStateManager; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.DefaultVertexFormat; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexFormat; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import net.montoyo.wd.utilities.Bounds; @@ -44,7 +48,7 @@ public class ControlGroup extends Container { width = w; height = h; this.label = label; - this.labelW = font.getStringWidth(label); + this.labelW = font.width(label); paddingX = 8; paddingY = 8; } @@ -66,7 +70,7 @@ public class ControlGroup extends Container { public void setLabel(String label) { this.label = label; - labelW = font.getStringWidth(label); + labelW = font.width(label); } public String getLabel() { @@ -90,14 +94,14 @@ public class ControlGroup extends Container { } @Override - public void draw(int mouseX, int mouseY, float ptt) { - super.draw(mouseX, mouseY, ptt); + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { + super.draw(poseStack, mouseX, mouseY, ptt); if(visible) { - glColor4f(0.5f, 0.5f, 0.5f, 1.f); - glDisable(GL_TEXTURE_2D); - glEnable(GL_BLEND); - glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + RenderSystem.setShaderColor(0.5f, 0.5f, 0.5f, 1.f); + RenderSystem.disableTexture(); + RenderSystem.enableBlend(); + RenderSystem.blendFunc(GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA); double x1 = (double) x; double y1 = (double) y; @@ -112,52 +116,52 @@ public class ControlGroup extends Container { y2 -= bp; lw += 12.0; - vBuffer.begin(GL_QUADS, DefaultVertexFormats.POSITION); + vBuffer.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION); //Top edge (y = y1) if(labelW == 0) { - vBuffer.pos(x1, y1 + 1.0, 0.0).endVertex(); - vBuffer.pos(x2, y1 + 1.0, 0.0).endVertex(); - vBuffer.pos(x2, y1, 0.0).endVertex(); - vBuffer.pos(x1, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1 + 1.0, 0.0).endVertex(); + vBuffer.vertex(x2, y1 + 1.0, 0.0).endVertex(); + vBuffer.vertex(x2, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1, 0.0).endVertex(); } else { //Left - vBuffer.pos(x1, y1 + 1.0, 0.0).endVertex(); - vBuffer.pos(x1 + 8.0, y1 + 1.0, 0.0).endVertex(); - vBuffer.pos(x1 + 8.0, y1, 0.0).endVertex(); - vBuffer.pos(x1, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1 + 1.0, 0.0).endVertex(); + vBuffer.vertex(x1 + 8.0, y1 + 1.0, 0.0).endVertex(); + vBuffer.vertex(x1 + 8.0, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1, 0.0).endVertex(); //Right - vBuffer.pos(x1 + lw, y1 + 1.0, 0.0).endVertex(); - vBuffer.pos(x2, y1 + 1.0, 0.0).endVertex(); - vBuffer.pos(x2, y1, 0.0).endVertex(); - vBuffer.pos(x1 + lw, y1, 0.0).endVertex(); + vBuffer.vertex(x1 + lw, y1 + 1.0, 0.0).endVertex(); + vBuffer.vertex(x2, y1 + 1.0, 0.0).endVertex(); + vBuffer.vertex(x2, y1, 0.0).endVertex(); + vBuffer.vertex(x1 + lw, y1, 0.0).endVertex(); } //Bottom edge (y = y2) - vBuffer.pos(x1, y2, 0.0).endVertex(); - vBuffer.pos(x2, y2, 0.0).endVertex(); - vBuffer.pos(x2, y2 - 1.0, 0.0).endVertex(); - vBuffer.pos(x1, y2 - 1.0, 0.0).endVertex(); + vBuffer.vertex(x1, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y2 - 1.0, 0.0).endVertex(); + vBuffer.vertex(x1, y2 - 1.0, 0.0).endVertex(); //Left edge (x = x1) - vBuffer.pos(x1, y2, 0.0).endVertex(); - vBuffer.pos(x1 + 1.0, y2, 0.0).endVertex(); - vBuffer.pos(x1 + 1.0, y1, 0.0).endVertex(); - vBuffer.pos(x1, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y2, 0.0).endVertex(); + vBuffer.vertex(x1 + 1.0, y2, 0.0).endVertex(); + vBuffer.vertex(x1 + 1.0, y1, 0.0).endVertex(); + vBuffer.vertex(x1, y1, 0.0).endVertex(); //Right edge (x = x2) - vBuffer.pos(x2 - 1.0, y2, 0.0).endVertex(); - vBuffer.pos(x2, y2, 0.0).endVertex(); - vBuffer.pos(x2, y1, 0.0).endVertex(); - vBuffer.pos(x2 - 1.0, y1, 0.0).endVertex(); - tessellator.draw(); + vBuffer.vertex(x2 - 1.0, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y2, 0.0).endVertex(); + vBuffer.vertex(x2, y1, 0.0).endVertex(); + vBuffer.vertex(x2 - 1.0, y1, 0.0).endVertex(); + tessellator.end(); glDisable(GL_BLEND); glEnable(GL_TEXTURE_2D); if(labelW != 0) - font.drawString(label, x + 10 + ((int) bp), y, labelColor, labelShadowed); + font.drawShadow(poseStack, label, x + 10 + ((int) bp), y, labelColor, labelShadowed); } } @@ -176,7 +180,7 @@ public class ControlGroup extends Container { width = json.getInt("width", 100); height = json.getInt("height", 100); label = tr(json.getString("label", "")); - labelW = font.getStringWidth(label); + labelW = font.width(label); labelColor = json.getColor("labelColor", COLOR_WHITE); labelShadowed = json.getBool("labelShadowed", true); diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Icon.java b/src/main/java/net/montoyo/wd/client/gui/controls/Icon.java index e921aae..88464e4 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Icon.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Icon.java @@ -4,7 +4,8 @@ package net.montoyo.wd.client.gui.controls; -import net.minecraft.util.ResourceLocation; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.resources.ResourceLocation; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import org.lwjgl.opengl.GL11; @@ -42,12 +43,12 @@ public class Icon extends BasicControl { } @Override - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(texture != null) { GL11.glEnable(GL11.GL_TEXTURE_2D); bindTexture(texture); blend(true); - fillTexturedRect(x, y, width, height, u1, v1, u2, v2); + fillTexturedRect(poseStack, x, y, width, height, u1, v1, u2, v2); blend(false); bindTexture(null); } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/Label.java b/src/main/java/net/montoyo/wd/client/gui/controls/Label.java index 5a69f71..ea1cc5b 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/Label.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/Label.java @@ -4,6 +4,7 @@ package net.montoyo.wd.client.gui.controls; +import com.mojang.blaze3d.vertex.PoseStack; import net.montoyo.wd.client.gui.loading.JsonOWrapper; public class Label extends BasicControl { @@ -22,7 +23,7 @@ public class Label extends BasicControl { this.x = x; this.y = y; label = str; - labelW = font.getStringWidth(str); + labelW = font.width(str); color = COLOR_WHITE; shadowed = false; } @@ -31,7 +32,7 @@ public class Label extends BasicControl { this.x = x; this.y = y; label = str; - labelW = font.getStringWidth(str); + labelW = font.width(str); this.color = color; shadowed = false; } @@ -40,14 +41,14 @@ public class Label extends BasicControl { this.x = x; this.y = y; label = str; - labelW = font.getStringWidth(str); + labelW = font.width(str); this.color = color; this.shadowed = shadowed; } public void setLabel(String label) { this.label = label; - labelW = font.getStringWidth(label); + labelW = font.width(label); } public String getLabel() { @@ -71,9 +72,9 @@ public class Label extends BasicControl { } @Override - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(visible) - font.drawString(label, x, y, color, shadowed); + font.drawShadow(poseStack, label, x, y, color, shadowed); } @Override @@ -90,7 +91,7 @@ public class Label extends BasicControl { public void load(JsonOWrapper json) { super.load(json); label = tr(json.getString("label", "")); - labelW = font.getStringWidth(label); + labelW = font.width(label); color = json.getColor("color", COLOR_WHITE); shadowed = json.getBool("shadowed", false); } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/List.java b/src/main/java/net/montoyo/wd/client/gui/controls/List.java index 7d59625..4d13a34 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/List.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/List.java @@ -4,7 +4,11 @@ package net.montoyo.wd.client.gui.controls; -import net.minecraft.client.shader.Framebuffer; +import com.mojang.blaze3d.pipeline.RenderTarget; +import com.mojang.blaze3d.pipeline.TextureTarget; +import com.mojang.blaze3d.systems.RenderSystem; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.Minecraft; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import java.util.ArrayList; @@ -52,7 +56,7 @@ public class List extends BasicControl { private int width; private int height; private final ArrayList content = new ArrayList<>(); - private Framebuffer fbo; + private RenderTarget fbo; private int selected = -1; private boolean update; private int selColor = 0xFF0080FF; @@ -60,9 +64,9 @@ public class List extends BasicControl { //Scroll handling private int contentH = 0; private int scrollSize; - private int scrollPos = 0; + private double scrollPos = 0; private boolean scrolling = false; - private int scrollGrab; + private double scrollGrab; public List() { } @@ -81,25 +85,25 @@ public class List extends BasicControl { return (int) amount; } - private boolean isInScrollbar(int mouseX, int mouseY) { + private boolean isInScrollbar(double mouseX, double mouseY) { return mouseX >= x + width - 5 && mouseX <= x + width - 1 && mouseY >= y + 1 + scrollPos && mouseY <= y + 1 + scrollPos + scrollSize; } private void createFBO() { if(fbo != null) - fbo.deleteFramebuffer(); + fbo.destroyBuffers(); - fbo = new Framebuffer(parent.screen2DisplayX(width), parent.screen2DisplayY(height), false); - fbo.setFramebufferFilter(GL_NEAREST); - fbo.bindFramebuffer(false); - glClearColor(0.0f, 0.0f, 0.0f, 1.f); //Set alpha to 1 - glClear(GL_COLOR_BUFFER_BIT); - fbo.unbindFramebuffer(); + fbo = new TextureTarget(parent.screen2DisplayX(width), parent.screen2DisplayY(height), false, Minecraft.ON_OSX); + fbo.setFilterMode(GL_NEAREST); + fbo.bindWrite(false); + RenderSystem.clearColor(0.0f, 0.0f, 0.0f, 1.f); //Set alpha to 1 + RenderSystem.clearDepth(GL_COLOR_BUFFER_BIT); + fbo.unbindWrite(); update = true; } private void renderToFBO() { - beginFramebuffer(fbo, width, height); + PoseStack poseStack = beginFramebuffer(fbo, width, height); fillRect(0, 0, width, height, COLOR_BLACK); glColor4f(1.f, 1.f, 1.f, 1.f); @@ -112,18 +116,18 @@ public class List extends BasicControl { break; int color = (i == selected) ? selColor : COLOR_WHITE; - font.drawString(content.get(i).text, 4, i * 12 + offset, color); + font.draw(poseStack, content.get(i).text, 4, i * 12 + offset, color); } } - drawBorder(0, 0, width, height, 0xFF808080); - endFramebuffer(fbo); + drawBorder(poseStack, 0, 0, width, height, 0xFF808080); + endFramebuffer(poseStack, fbo); } @Override public void destroy() { if(fbo != null) - fbo.deleteFramebuffer(); + fbo.destroyBuffers(); } public void setSize(int w, int h) { @@ -206,11 +210,11 @@ public class List extends BasicControl { } @Override - public void mouseMove(int mouseX, int mouseY) { + public boolean mouseMove(double mouseX, double mouseY) { int sel = -1; if(!disabled && mouseX >= x + 1 && mouseX <= x + width - 6 && mouseY >= y + 2 && mouseY <= y + height - 2) { int offset = y + 4 - getYOffset(); - sel = (mouseY - offset) / 12; + sel = (int) ((mouseY - offset) / 12); if(sel < 0 || sel >= content.size()) sel = -1; @@ -219,31 +223,43 @@ public class List extends BasicControl { if(selected != sel) { selected = sel; update = true; + + return true; } + + return false; } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { + public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { if(!disabled && mouseButton == 0) { if(isInScrollbar(mouseX, mouseY)) { scrolling = true; scrollGrab = mouseY - (y + 1 + scrollPos); } else if(selected >= 0) parent.actionPerformed(new EntryClick(this)); + + return true; } + + return true; } @Override - public void mouseReleased(int mouseX, int mouseY, int state) { - if(!disabled && scrolling) + public boolean mouseReleased(double mouseX, double mouseY, int state) { + if(!disabled && scrolling) { scrolling = false; + return true; + } + + return false; } @Override - public void mouseScroll(int mouseX, int mouseY, int amount) { + public boolean mouseScroll(double mouseX, double mouseY, double amount) { if(!disabled && !scrolling && mouseX >= x && mouseX <= x + width && mouseY >= y && mouseY <= y + height) { double disp = 12.d * ((double) (height - 2 - scrollSize)) / ((double) (contentH - height)); - int sp = scrollPos; + double sp = scrollPos; if(amount < 0) sp += (int) disp; @@ -259,13 +275,17 @@ public class List extends BasicControl { scrollPos = sp; update = true; } + + return true; } + + return false; } @Override - public void mouseClickMove(int mouseX, int mouseY, int clickedMouseButton, long timeSinceLastClick) { + public boolean mouseClickMove(double mouseX, double mouseY, int button, double dragX, double dragY) { if(!disabled && scrolling) { - int sp = mouseY - scrollGrab - y - 1; + double sp = mouseY - scrollGrab - y - 1; if(sp < 0) sp = 0; else if(sp > height - 2 - scrollSize) @@ -275,21 +295,25 @@ public class List extends BasicControl { scrollPos = sp; update = true; } + + return true; } + + return false; } @Override - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(visible) { if(update) { renderToFBO(); update = false; } - fbo.bindFramebufferTexture(); - glColor4f(1.f, 1.f, 1.f, 1.f); - fillTexturedRect(x, y, width, height, 0.0, 1.0, 1.0, 0.0); - fbo.unbindFramebufferTexture(); + fbo.bindRead(); //TODO: Make sure is right + RenderSystem.setShaderColor(1.f, 1.f, 1.f, 1.f); + fillTexturedRect(poseStack, x, y, width, height, 0.0, 1.0, 1.0, 0.0); + fbo.unbindRead(); fillRect(x + width - 5, y + 1 + scrollPos, 4, scrollSize, (scrolling || isInScrollbar(mouseX, mouseY)) ? 0xFF202020 : 0xFF404040); } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java b/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java index 555fdc6..b78fca4 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/TextField.java @@ -4,9 +4,11 @@ package net.montoyo.wd.client.gui.controls; -import net.minecraft.client.gui.GuiTextField; +import com.mojang.blaze3d.vertex.PoseStack; +import net.minecraft.client.gui.components.EditBox; +import net.minecraft.network.chat.Component; import net.montoyo.wd.client.gui.loading.JsonOWrapper; -import org.lwjgl.input.Keyboard; +import org.lwjgl.glfw.GLFW; import java.util.ArrayList; @@ -18,7 +20,7 @@ public class TextField extends Control { private EnterPressedEvent(TextField field) { source = field; - text = field.field.getText(); + text = field.field.getValue(); } public String getText() { @@ -34,7 +36,7 @@ public class TextField extends Control { private TabPressedEvent(TextField field) { source = field; - String text = field.field.getText(); + String text = field.field.getValue(); int max = field.field.getCursorPosition(); int spacePos = 0; @@ -62,7 +64,7 @@ public class TextField extends Control { private TextChangedEvent(TextField tf, String old) { source = tf; oldContent = old; - newContent = tf.field.getText(); + newContent = tf.field.getValue(); } public String getOldContent() { @@ -84,62 +86,64 @@ public class TextField extends Control { public static final int DEFAULT_TEXT_COLOR = 14737632; public static final int DEFAULT_DISABLED_COLOR = 7368816; - private final GuiTextField field; + private final EditBox field; private boolean enabled = true; private int textColor = DEFAULT_TEXT_COLOR; private int disabledColor = DEFAULT_DISABLED_COLOR; private final ArrayList listeners = new ArrayList<>(); public TextField() { - field = new GuiTextField(0, font, 1, 1, 198, 20); + field = new EditBox(font, 1, 1, 198, 20, Component.nullToEmpty("")); } public TextField(int x, int y, int width, int height) { - field = new GuiTextField(0, font, x + 1, y + 1, width - 2, height - 2); + field = new EditBox(font, x + 1, y + 1, width - 2, height - 2, Component.nullToEmpty("")); } public TextField(int x, int y, int width, int height, String text) { - field = new GuiTextField(0, font, x + 1, y + 1, width - 2, height - 2); - field.setText(text); + field = new EditBox(font, x + 1, y + 1, width - 2, height - 2, Component.nullToEmpty("")); + field.setValue(text); } @Override - public void keyTyped(char typedChar, int keyCode) { - if(keyCode == Keyboard.KEY_RETURN || keyCode == Keyboard.KEY_NUMPADENTER) + public boolean keyTyped(char typedChar, int keyCode) { + if(keyCode == GLFW.GLFW_KEY_ENTER || keyCode == GLFW.GLFW_KEY_KP_ENTER) parent.actionPerformed(new EnterPressedEvent(this)); - else if(keyCode == Keyboard.KEY_TAB) + else if(keyCode == GLFW.GLFW_KEY_TAB) parent.actionPerformed(new TabPressedEvent(this)); else { String old; if(enabled && field.isFocused()) - old = field.getText(); + old = field.getValue(); else old = null; - field.textboxKeyTyped(typedChar, keyCode); + field.charTyped(typedChar, keyCode); - if(enabled && field.isFocused() && !field.getText().equals(old)) { + if(enabled && field.isFocused() && !field.getValue().equals(old)) { for(TextChangeListener tcl : listeners) - tcl.onTextChange(this, old, field.getText()); + tcl.onTextChange(this, old, field.getValue()); parent.actionPerformed(new TextChangedEvent(this, old)); } } + + return false; } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - field.mouseClicked(mouseX, mouseY, mouseButton); + public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { + return field.mouseClicked(mouseX, mouseY, mouseButton); } @Override - public void draw(int mouseX, int mouseY, float ptt) { - field.drawTextBox(); + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { + field.render(poseStack, mouseX, mouseY, ptt); } public void setText(String text) { - String old = field.getText(); - field.setText(text); + String old = field.getValue(); + field.setValue(text); if(!old.equals(text)) { for(TextChangeListener tcl : listeners) @@ -148,38 +152,38 @@ public class TextField extends Control { } public void clear() { - field.setText(""); + field.setValue(""); } public String getText() { - return field.getText(); + return field.getValue(); } public String getSelectedText() { - return field.getSelectedText(); + return field.getHighlighted(); } public void setWidth(int width) { - field.width = width - 2; + field.setWidth(width - 2); } @Override public int getWidth() { - return field.width + 2; + return field.getWidth() + 2; } public void setHeight(int height) { - field.height = height - 2; + field.setHeight(height - 2); } @Override public int getHeight() { - return field.height + 2; + return field.getHeight() + 2; } public void setSize(int w, int h) { - field.width = w - 2; - field.height = h - 2; + field.setWidth(w - 2); + field.setHeight(h - 2); } @Override @@ -200,7 +204,7 @@ public class TextField extends Control { public void setDisabled(boolean en) { enabled = !en; - field.setEnabled(enabled); + field.setFocus(enabled); } public boolean isDisabled() { @@ -208,12 +212,12 @@ public class TextField extends Control { } public void enable() { - field.setEnabled(true); + field.setFocus(true); enabled = true; } public void disable() { - field.setEnabled(false); + field.setFocus(false); enabled = false; } @@ -222,7 +226,7 @@ public class TextField extends Control { } public boolean isVisible() { - return field.getVisible(); + return field.isVisible(); } public void show() { @@ -234,7 +238,7 @@ public class TextField extends Control { } public void setFocused(boolean val) { - field.setFocused(val); + field.setFocus(val); } public boolean hasFocus() { @@ -242,15 +246,15 @@ public class TextField extends Control { } public void focus() { - field.setFocused(true); + field.setFocus(true); } public void setMaxLength(int len) { - field.setMaxStringLength(len); + field.setMaxLength(len); } public int getMaxLength() { - return field.getMaxStringLength(); + return field.getMaxLength(); //TODO: access transformer } public void setTextColor(int color) { @@ -263,7 +267,7 @@ public class TextField extends Control { } public void setDisabledTextColor(int color) { - field.setDisabledTextColour(color); + field.setTextColorUneditable(color); disabledColor = color; } @@ -271,7 +275,7 @@ public class TextField extends Control { return disabledColor; } - public GuiTextField getMcField() { + public EditBox getMcField() { return field; } @@ -289,19 +293,19 @@ public class TextField extends Control { super.load(json); field.x = json.getInt("x", 0) + 1; field.y = json.getInt("y", 0) + 1; - field.width = json.getInt("width", 200) - 2; - field.height = json.getInt("height", 22) - 2; - field.setText(tr(json.getString("text", ""))); + field.setWidth(json.getInt("width", 200) - 2); + field.setHeight(json.getInt("height", 22) - 2); + field.setValue(tr(json.getString("text", ""))); field.setVisible(json.getBool("visible", true)); - field.setMaxStringLength(json.getInt("maxLength", 32)); + field.setMaxLength(json.getInt("maxLength", 32)); enabled = !json.getBool("disabled", false); textColor = json.getColor("textColor", DEFAULT_TEXT_COLOR); disabledColor = json.getColor("disabledColor", DEFAULT_DISABLED_COLOR); field.setTextColor(textColor); - field.setDisabledTextColour(disabledColor); - field.setEnabled(enabled); + field.setTextColorUneditable(disabledColor); + field.setFocus(enabled); } } diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java b/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java index d5304ca..7f11a04 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/UpgradeGroup.java @@ -4,6 +4,7 @@ package net.montoyo.wd.client.gui.controls; +import com.mojang.blaze3d.vertex.PoseStack; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.entity.ItemRenderer; import net.minecraft.world.item.ItemStack; @@ -25,7 +26,7 @@ public class UpgradeGroup extends BasicControl { } @Override - public void draw(int mouseX, int mouseY, float ptt) { + public void draw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(upgrades != null) { int x = this.x; @@ -41,9 +42,9 @@ public class UpgradeGroup extends BasicControl { } @Override - public void postDraw(int mouseX, int mouseY, float ptt) { + public void postDraw(PoseStack poseStack, int mouseX, int mouseY, float ptt) { if(overStack != null) - parent.drawItemStackTooltip(overStack, mouseX, mouseY); + parent.drawItemStackTooltip(poseStack, overStack, mouseX, mouseY); } @Override @@ -80,34 +81,47 @@ public class UpgradeGroup extends BasicControl { } @Override - public void mouseMove(int mouseX, int mouseY) { + public boolean mouseMove(double mouseX, double mouseY) { if(upgrades != null) { overStack = null; if(mouseY >= y && mouseY <= y + 16 && mouseX >= x) { mouseX -= x; - int sel = mouseX / 18; + int sel = (int) (mouseX / 18); if(sel < upgrades.size() && mouseX % 18 <= 16) overStack = upgrades.get(sel); + + return true; } } + + return false; } @Override - public void mouseClicked(int mouseX, int mouseY, int mouseButton) { - if(mouseButton == 0) + public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) { + if(mouseButton == 0) { clickStack = overStack; + + return true; + } + + return false; } @Override - public void mouseReleased(int mouseX, int mouseY, int state) { + public boolean mouseReleased(double mouseX, double mouseY, int state) { if(state == 0 && clickStack != null) { if(clickStack == overStack && !disabled && upgrades.contains(clickStack)) //HOTFIX: Make sure it's actually in the list :p parent.actionPerformed(new ClickEvent(this)); clickStack = null; + + return true; } + + return false; } public ItemStack getMouseOverUpgrade() { diff --git a/src/main/java/net/montoyo/wd/client/gui/controls/YTButton.java b/src/main/java/net/montoyo/wd/client/gui/controls/YTButton.java index 8e598e8..4852f2a 100644 --- a/src/main/java/net/montoyo/wd/client/gui/controls/YTButton.java +++ b/src/main/java/net/montoyo/wd/client/gui/controls/YTButton.java @@ -4,6 +4,7 @@ package net.montoyo.wd.client.gui.controls; +import net.minecraft.network.chat.Component; import net.montoyo.wd.client.gui.loading.JsonOWrapper; import net.montoyo.wd.utilities.Util; import net.montoyo.wd.utilities.VideoType; @@ -16,8 +17,8 @@ public class YTButton extends Button implements TextField.TextChangeListener { private TextField urlField; public YTButton() { - btn.displayString = "YT"; - btn.enabled = false; + btn.setMessage(Component.nullToEmpty("YT")); + btn.active = false; shiftColor = 0xFFFF6464; } @@ -72,7 +73,7 @@ public class YTButton extends Button implements TextField.TextChangeListener { @Override public void onTextChange(TextField tf, String oldContent, String newContent) { - btn.enabled = (VideoType.getTypeFromURL(Util.addProtocol(newContent)) == VideoType.YOUTUBE); + btn.active = (VideoType.getTypeFromURL(Util.addProtocol(newContent)) == VideoType.YOUTUBE); } } diff --git a/src/main/java/net/montoyo/wd/data/ScreenConfigData.java b/src/main/java/net/montoyo/wd/data/ScreenConfigData.java index 3337b89..05d05d5 100644 --- a/src/main/java/net/montoyo/wd/data/ScreenConfigData.java +++ b/src/main/java/net/montoyo/wd/data/ScreenConfigData.java @@ -5,6 +5,7 @@ package net.montoyo.wd.data; import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraftforge.api.distmarker.Dist; @@ -67,7 +68,7 @@ public class ScreenConfigData extends GuiData { return null; } - return new GuiScreenConfig((TileEntityScreen) te, side, friends, friendRights, otherRights); + return new GuiScreenConfig(Component.nullToEmpty(""), (TileEntityScreen) te, side, friends, friendRights, otherRights); } @Override diff --git a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java index 356af01..087d95c 100644 --- a/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java +++ b/src/main/java/net/montoyo/wd/entity/TileEntityScreen.java @@ -10,6 +10,7 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.server.packs.repository.Pack; import net.minecraft.sounds.SoundEvent; import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionHand; @@ -22,6 +23,7 @@ import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockState; +import net.minecraftforge.network.PacketDistributor; import net.montoyo.mcef.api.IBrowser; import net.montoyo.wd.WebDisplays; import net.montoyo.wd.block.BlockScreen; @@ -30,6 +32,8 @@ import net.montoyo.wd.core.IUpgrade; import net.montoyo.wd.core.JSServerRequest; import net.montoyo.wd.core.ScreenRights; import net.montoyo.wd.data.ScreenConfigData; +import net.montoyo.wd.init.BlockInit; +import net.montoyo.wd.init.ItemInit; import net.montoyo.wd.net.Messages; import net.montoyo.wd.net.client.CMessageAddScreen; import net.montoyo.wd.net.client.CMessageCloseGui; @@ -49,8 +53,8 @@ import static net.montoyo.wd.block.BlockPeripheral.point; public class TileEntityScreen extends BlockEntity{ - public TileEntityScreen(BlockEntityType arg, BlockPos arg2, BlockState arg3) { - super(arg, arg2, arg3); + public TileEntityScreen(BlockPos arg2, BlockState arg3) { + super(null /*TODO: add blockEnityTYpe */ , arg2, arg3); } public static class Screen { @@ -323,7 +327,7 @@ public class TileEntityScreen extends BlockEntity{ ret.setupRedstoneStatus(level, getBlockPos()); if(sendUpdate) - Messages.INSTANCE.sendTo(new CMessageAddScreen(this, ret), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), new CMessageAddScreen(this, ret)); } screens.add(ret); @@ -362,7 +366,7 @@ public class TileEntityScreen extends BlockEntity{ public void requestData(ServerPlayer ep) { if(!level.isClientSide) - Messages.INSTANCE.sendTo(new CMessageAddScreen(this), ep); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> ep), new CMessageAddScreen(this)); } public void setScreenURL(BlockSide side, String url) { @@ -380,7 +384,7 @@ public class TileEntityScreen extends BlockEntity{ if(scr.browser != null) scr.browser.loadURL(url); } else { - Messages.INSTANCE.sendTo(CMessageScreenUpdate.setURL(this, side, url), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.setURL(this, side, url)); setChanged(); } } @@ -405,13 +409,13 @@ public class TileEntityScreen extends BlockEntity{ screens.get(idx).browser = null; } } else - Messages.INSTANCE(new CMessageScreenUpdate(this, side), point()); //Delete the screen + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), new CMessageScreenUpdate(this, side)); //Delete the screen screens.remove(idx); if(!level.isClientSide) { if(screens.isEmpty()) //No more screens: remove tile entity - level.setBlock(getBlockPos(), WebDisplays.INSTANCE.blockScreen.getDefaultInstance().withProperty(BlockScreen.hasTE, false)); + level.setBlockAndUpdate(getBlockPos(), BlockInit.blockScreen.get().defaultBlockState().setValue(BlockScreen.hasTE, false)); else setChanged(); } @@ -440,14 +444,14 @@ public class TileEntityScreen extends BlockEntity{ scr.browser = null; //Will be re-created by renderer } } else { - Messages.INSTANCE.sendTo(CMessageScreenUpdate.setResolution(this, side, res), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.setResolution(this, side, res)); setChanged(); } } private static Player getLaserUser(Screen scr) { if(scr.laserUser != null) { - if(scr.laserUser.isRemoved() || scr.laserUser.getItemInHand(InteractionHand.MAIN_HAND).getItem() != WebDisplays.INSTANCE.itemLaserPointer) + if(scr.laserUser.isRemoved() || !scr.laserUser.getItemInHand(InteractionHand.MAIN_HAND).getItem().equals(ItemInit.itemLaserPointer.get())) scr.laserUser = null; } @@ -476,13 +480,13 @@ public class TileEntityScreen extends BlockEntity{ if(level.isClientSide) Log.warning("TileEntityScreen.click() from client side is useless..."); else if(getLaserUser(scr) == null) - Messages.INSTANCE.sendTo(CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_CLICK, vec), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_CLICK, vec)); } void clickUnsafe(BlockSide side, int action, int x, int y) { if(level.isClientSide) { Vector2i vec = (action == CMessageScreenUpdate.MOUSE_UP) ? null : new Vector2i(x, y); - Messages.INSTANCE.sendTo(CMessageScreenUpdate.click(this, side, action, vec), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.click(this, side, action, vec)); } } @@ -539,7 +543,7 @@ public class TileEntityScreen extends BlockEntity{ } if(sendMsg) - Messages.INSTANCE.sendTo(CMessageScreenUpdate.jsRedstone(this, side, vec, redstoneLevel), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.jsRedstone(this, side, vec, redstoneLevel)); } } @@ -552,18 +556,18 @@ public class TileEntityScreen extends BlockEntity{ Screen scr = getScreen(side); if(scr == null) { Log.error("Called handleJSRequest on non-existing side %s", side.toString()); - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, 403, "Invalid side"), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, 403, "Invalid side")); return; } if(!scr.owner.uuid.equals(src.getGameProfile().getId())) { Log.warning("Player %s (UUID %s) tries to use the redstone output API on a screen he doesn't own!", src.getName(), src.getGameProfile().getId().toString()); - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, 403, "Only the owner can do that"), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, 403, "Only the owner can do that")); return; } if(scr.upgrades.stream().noneMatch(DefaultUpgrade.REDSTONE_OUTPUT::matches)) { - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, 403, "Missing upgrade"), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, 403, "Missing upgrade")); return; } @@ -588,25 +592,25 @@ public class TileEntityScreen extends BlockEntity{ vec1.add(side.up.x, side.up.y, side.up.z); } - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, new byte[0]), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, new byte[0])); } else if(req == JSServerRequest.SET_REDSTONE_AT) { int x = (Integer) data[0]; int y = (Integer) data[1]; boolean state = (Boolean) data[2]; if(x < 0 || x >= scr.size.x || y < 0 || y >= scr.size.y) - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, 403, "Out of range"), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, 403, "Out of range")); else { BlockPos bp = (new Vector3i(getBlockPos())).addMul(side.right, x).addMul(side.up, y).toBlock(); BlockState bs = level.getBlockState(bp); if(!bs.getValue(BlockScreen.emitting).equals(state)) - level.setBlock(bp, bs.setValue(BlockScreen.emitting, state)); + level.setBlockAndUpdate(bp, bs.setValue(BlockScreen.emitting, state)); - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, new byte[0]), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, new byte[0])); } } else - Messages.INSTANCE.sendTo(new CMessageJSResponse(reqId, req, 400, "Invalid request"), src); + Messages.INSTANCE.send(PacketDistributor.PLAYER.with(() -> src), new CMessageJSResponse(reqId, req, 400, "Invalid request")); } @Override @@ -722,7 +726,7 @@ public class TileEntityScreen extends BlockEntity{ if(!scr.friends.contains(pair)) { scr.friends.add(pair); - (new ScreenConfigData(new Vector3i(getBlockPos()), side, scr)).updateOnly().sendTo(point()); + (new ScreenConfigData(new Vector3i(getBlockPos()), side, scr)).updateOnly().sendTo(point(level, getBlockPos())); setChanged(); } } @@ -738,7 +742,7 @@ public class TileEntityScreen extends BlockEntity{ if(scr.friends.remove(pair)) { checkLaserUserRights(scr); - (new ScreenConfigData(new Vector3i(getBlockPos()), side, scr)).updateOnly().sendTo(point()); + (new ScreenConfigData(new Vector3i(getBlockPos()), side, scr)).updateOnly().sendTo(point(level, getBlockPos())); setChanged(); } } @@ -756,8 +760,8 @@ public class TileEntityScreen extends BlockEntity{ scr.otherRights = or; checkLaserUserRights(scr); - (new ScreenConfigData(new Vector3i(getBlockPos()), side, scr)).updateOnly().sendTo(point()); - markDirty(); + (new ScreenConfigData(new Vector3i(getBlockPos()), side, scr)).updateOnly().sendTo(point(level, getBlockPos())); + setChanged(); } } @@ -783,21 +787,11 @@ public class TileEntityScreen extends BlockEntity{ TypeData[] data = WebDisplays.GSON.fromJson(text, TypeData[].class); for(TypeData ev : data) { - switch(ev.getAction()) { - case PRESS: - scr.browser.injectKeyPressedByKeyCode(ev.getKeyCode(), ev.getKeyChar(), 0); - break; - - case RELEASE: - scr.browser.injectKeyReleasedByKeyCode(ev.getKeyCode(), ev.getKeyChar(), 0); - break; - - case TYPE: - scr.browser.injectKeyTyped(ev.getKeyChar(), 0); - break; - - default: - throw new RuntimeException("Invalid type action '" + ev.getAction() + '\''); + switch (ev.getAction()) { + case PRESS -> scr.browser.injectKeyPressedByKeyCode(ev.getKeyCode(), ev.getKeyChar(), 0); + case RELEASE -> scr.browser.injectKeyReleasedByKeyCode(ev.getKeyCode(), ev.getKeyChar(), 0); + case TYPE -> scr.browser.injectKeyTyped(ev.getKeyChar(), 0); + default -> throw new RuntimeException("Invalid type action '" + ev.getAction() + '\''); } } } @@ -806,7 +800,7 @@ public class TileEntityScreen extends BlockEntity{ } } } else { - Messages.INSTANCE.sendTo(CMessageScreenUpdate.type(this, side, text), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.type(this, side, text)); if(soundPos != null) playSoundAt(WebDisplays.INSTANCE.soundTyping, soundPos, 0.25f, 1.f); @@ -862,7 +856,7 @@ public class TileEntityScreen extends BlockEntity{ } if(scr.upgrades.size() >= 16) { - Log.error("Can't insert upgrade %s in screen %s at %s: too many upgrades already!", safeName(is), side.toString(), pos.toString()); + Log.error("Can't insert upgrade %s in screen %s at %s: too many upgrades already!", safeName(is), side.toString(), getBlockPos().toString()); return false; } @@ -874,7 +868,7 @@ public class TileEntityScreen extends BlockEntity{ isCopy.setCount(1); scr.upgrades.add(isCopy); - Messages.INSTANCE.sendTo(CMessageScreenUpdate.upgrade(this, side), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.upgrade(this, side)); itemAsUpgrade.onInstall(this, side, player, isCopy); playSoundAt(WebDisplays.INSTANCE.soundUpgradeAdd, getBlockPos(), 1.0f, 1.0f); setChanged(); @@ -926,11 +920,11 @@ public class TileEntityScreen extends BlockEntity{ if(idxToRemove >= 0) { dropUpgrade(scr.upgrades.get(idxToRemove), side, player); scr.upgrades.remove(idxToRemove); - Messages.INSTANCE.sendTo(CMessageScreenUpdate.upgrade(this, side), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.upgrade(this, side)); playSoundAt(WebDisplays.INSTANCE.soundUpgradeDel, getBlockPos(), 1.0f, 1.0f); setChanged(); } else - Log.warning("Tried to remove non-existing upgrade %s to screen %s at %s", safeName(is), side.toString(), pos.toString()); + Log.warning("Tried to remove non-existing upgrade %s to screen %s at %s", safeName(is), side.toString(), getBlockPos().toString()); } private void dropUpgrade(ItemStack is, BlockSide side, @Nullable Player ply) { @@ -982,10 +976,10 @@ public class TileEntityScreen extends BlockEntity{ //Try to acquire laser lock if(getLaserUser(scr) == null) { scr.laserUser = ply; - Messages.INSTANCE.sendTo(CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_DOWN, pos), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_DOWN, pos)); } } else if(getLaserUser(scr) == ply) - Messages.INSTANCE.sendTo(CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_MOVE, pos), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_MOVE, pos)); } } @@ -995,7 +989,7 @@ public class TileEntityScreen extends BlockEntity{ if(scr != null) { if(getLaserUser(scr) == ply) { scr.laserUser = null; - Messages.INSTANCE.sendTo(CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_UP, null), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.click(this, side, CMessageScreenUpdate.MOUSE_UP, null)); } } } @@ -1006,7 +1000,7 @@ public class TileEntityScreen extends BlockEntity{ scr.upgrades.clear(); } - Messages.INSTANCE.sendTo(new CMessageCloseGui(getBlockPos()), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), new CMessageCloseGui(getBlockPos())); } public void setOwner(BlockSide side, Player newOwner) { @@ -1027,7 +1021,7 @@ public class TileEntityScreen extends BlockEntity{ } scr.owner = new NameUUIDPair(newOwner.getGameProfile()); - Messages.INSTANCE.sendTo(CMessageScreenUpdate.owner(this, side, scr.owner), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.owner(this, side, scr.owner)); checkLaserUserRights(scr); setChanged(); } @@ -1051,7 +1045,7 @@ public class TileEntityScreen extends BlockEntity{ } } else { scr.rotation = rot; - Messages.INSTANCE.sendTo(CMessageScreenUpdate.rotation(this, side, rot), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.rotation(this, side, rot)); setChanged(); } } @@ -1067,7 +1061,7 @@ public class TileEntityScreen extends BlockEntity{ if(scr.browser != null) scr.browser.runJS(code, ""); } else - Messages.INSTANCE.sendTo(CMessageScreenUpdate.js(this, side, code), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.js(this, side, code)); } public void setAutoVolume(BlockSide side, boolean av) { @@ -1082,18 +1076,18 @@ public class TileEntityScreen extends BlockEntity{ if(level.isClientSide) WebDisplays.PROXY.screenUpdateAutoVolumeInGui(new Vector3i(getBlockPos()), side, av); else { - Messages.INSTANCE.sendTo(CMessageScreenUpdate.autoVolume(this, side, av), point()); + Messages.INSTANCE.send(PacketDistributor.NEAR.with(() -> point(level, getBlockPos())), CMessageScreenUpdate.autoVolume(this, side, av)); setChanged(); } } - @Override - public boolean shouldRefresh(Level world, BlockPos pos, @Nonnull BlockState oldState, @Nonnull BlockState newState) { - if(oldState.getBlock() != WebDisplays.INSTANCE.blockScreen || newState.getBlock() != WebDisplays.INSTANCE.blockScreen) - return true; - - return oldState.getValue(BlockScreen.hasTE) != newState.getValue(BlockScreen.hasTE); - } +// @Override +// public boolean shouldRefresh(Level world, BlockPos pos, @Nonnull BlockState oldState, @Nonnull BlockState newState) { +// if(oldState.getBlock() != WebDisplays.INSTANCE.blockScreen || newState.getBlock() != WebDisplays.INSTANCE.blockScreen) +// return true; +// +// return oldState.getValue(BlockScreen.hasTE) != newState.getValue(BlockScreen.hasTE); +// } } diff --git a/src/main/java/net/montoyo/wd/init/BlockInit.java b/src/main/java/net/montoyo/wd/init/BlockInit.java index 3fcf084..50df18c 100644 --- a/src/main/java/net/montoyo/wd/init/BlockInit.java +++ b/src/main/java/net/montoyo/wd/init/BlockInit.java @@ -32,7 +32,7 @@ public class BlockInit { return reg; } - public static final RegistryObject blockScreen = registerBlock("screen_block", () -> new BlockScreen()); + public static final RegistryObject blockScreen = registerBlock("screen_block", BlockScreen::new); public static final RegistryObject blockPeripheral = registerBlock("peripheral_block", () -> new BlockPeripheral());