Advertisement
HalestormXV

Untitled

Feb 10th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package halestormxv.network.packets;
  2.  
  3. import io.netty.buffer.ByteBuf;
  4. import net.minecraft.block.Block;
  5. import net.minecraft.client.Minecraft;
  6. import net.minecraft.entity.player.EntityPlayerMP;
  7. import net.minecraft.util.math.BlockPos;
  8. import net.minecraft.util.math.RayTraceResult;
  9. import net.minecraft.util.text.TextComponentString;
  10. import net.minecraft.util.text.TextFormatting;
  11. import net.minecraft.world.World;
  12. import net.minecraftforge.fml.common.FMLCommonHandler;
  13. import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
  14. import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
  15. import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
  16.  
  17. public class PacketSendKey implements IMessage
  18. {
  19.     private BlockPos blockPos;
  20.  
  21.     @Override
  22.     public void fromBytes(ByteBuf buf)
  23.     {
  24.         // Encoding the position as a long is more efficient
  25.         blockPos = BlockPos.fromLong(buf.readLong());
  26.     }
  27.  
  28.     @Override
  29.     public void toBytes(ByteBuf buf)
  30.     {
  31.         // Encoding the position as a long is more efficient
  32.         buf.writeLong(blockPos.toLong());
  33.     }
  34.  
  35.     public PacketSendKey()
  36.     {
  37.         // Calculate the position of the block we are looking at
  38.         RayTraceResult mouseOver = Minecraft.getMinecraft().objectMouseOver;
  39.         blockPos = mouseOver.getBlockPos();
  40.     }
  41.  
  42.     public static class Handler implements IMessageHandler<PacketSendKey, IMessage> {
  43.         @Override
  44.         public IMessage onMessage(PacketSendKey message, MessageContext ctx)
  45.         {
  46.             // Always use a construct like this to actually handle your message. This ensures that
  47.             // your 'handle' code is run on the main Minecraft thread. 'onMessage' itself
  48.             // is called on the networking thread so it is not safe to do a lot of things
  49.             // here.
  50.             FMLCommonHandler.instance().getWorldThread(ctx.netHandler).addScheduledTask(() -> handle(message, ctx));
  51.             return null;
  52.         }
  53.  
  54.         private void handle(PacketSendKey message, MessageContext ctx)
  55.         {
  56.             // This code is run on the server side. So you can do server-side calculations here
  57.             EntityPlayerMP playerEntity = ctx.getServerHandler().player;
  58.             World world = playerEntity.getEntityWorld();
  59.             // Check if the block (chunk) is loaded to prevent abuse from a client
  60.             // trying to overload a server by randomly loading chunks
  61.             if (world.isBlockLoaded(message.blockPos)) {
  62.                 Block block = world.getBlockState(message.blockPos).getBlock();
  63.                 // Note: if this is a real message you want to show to a player and not a debug message you should
  64.                 // use localized messages with TextComponentTranslated.
  65.                 playerEntity.sendStatusMessage(new TextComponentString(TextFormatting.GREEN + "Hit block: " + block.getRegistryName()), false);
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement