mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2026-04-20 07:10:09 +00:00
protocol lib added
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package me.SuperRonanCraft.BetterRTPAddons.addons.portals.cmds;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
public abstract class AbstractPacket {
|
||||
// The packet we will be modifying
|
||||
protected PacketContainer handle;
|
||||
|
||||
/**
|
||||
* Constructs a new strongly typed wrapper for the given packet.
|
||||
*
|
||||
* @param handle - handle to the raw packet data.
|
||||
* @param type - the packet type.
|
||||
*/
|
||||
protected AbstractPacket(PacketContainer handle, PacketType type) {
|
||||
// Make sure we're given a valid packet
|
||||
if (handle == null)
|
||||
throw new IllegalArgumentException("Packet handle cannot be NULL.");
|
||||
if (!Objects.equal(handle.getType(), type))
|
||||
throw new IllegalArgumentException(handle.getHandle()
|
||||
+ " is not a packet of type " + type);
|
||||
|
||||
this.handle = handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a handle to the raw packet data.
|
||||
*
|
||||
* @return Raw packet data.
|
||||
*/
|
||||
public PacketContainer getHandle() {
|
||||
return handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the current packet to the given receiver.
|
||||
*
|
||||
* @param receiver - the receiver.
|
||||
* @throws RuntimeException If the packet cannot be sent.
|
||||
*/
|
||||
public void sendPacket(Player receiver) {
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().sendServerPacket(receiver,
|
||||
getHandle());
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException("Cannot send packet.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the current packet to all online players.
|
||||
*/
|
||||
public void broadcastPacket() {
|
||||
ProtocolLibrary.getProtocolManager().broadcastServerPacket(getHandle());
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate receiving the current packet from the given sender.
|
||||
*
|
||||
* @param sender - the sender.
|
||||
* @throws RuntimeException If the packet cannot be received.
|
||||
* @deprecated Misspelled. recieve to receive
|
||||
* @see #receivePacket(Player)
|
||||
*/
|
||||
@Deprecated
|
||||
public void recievePacket(Player sender) {
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
|
||||
getHandle());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Cannot recieve packet.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Simulate receiving the current packet from the given sender.
|
||||
*
|
||||
* @param sender - the sender.
|
||||
* @throws RuntimeException if the packet cannot be received.
|
||||
*/
|
||||
public void receivePacket(Player sender) {
|
||||
try {
|
||||
ProtocolLibrary.getProtocolManager().recieveClientPacket(sender,
|
||||
getHandle());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Cannot receive packet.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,39 @@
|
||||
package me.SuperRonanCraft.BetterRTPAddons.addons.portals.cmds;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.ProtocolManager;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.WrappedBlockData;
|
||||
import me.SuperRonanCraft.BetterRTPAddons.addons.portals.AddonPortals;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class PortalsCommand_Loc1 implements PortalsCommands, LocationFinder {
|
||||
|
||||
private PacketContainer fakeBlock;
|
||||
|
||||
@Override
|
||||
public void execute(CommandSender sendi, String label, String[] args, AddonPortals addonPortals) {
|
||||
Player p = (Player) sendi;
|
||||
Location loc = getTargetBlock(p, 10).getLocation();
|
||||
addonPortals.getPortals().setPortal(p, loc, false);
|
||||
sendi.sendMessage("Location 1 set to this location " + loc.toString());
|
||||
p.sendBlockChange(loc, Material.GLOWSTONE, (byte) 0);
|
||||
//p.sendBlockChange(loc, Material.GLOWSTONE, (byte) 0);
|
||||
|
||||
ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager();
|
||||
WrapperPlayServerBlockChange fakeBlock = new WrapperPlayServerBlockChange();
|
||||
fakeBlock.setLocation(BlockLocation);
|
||||
try {
|
||||
//System.out.println(fakeBlock.toString());
|
||||
protocolManager.sendServerPacket(p, fakeBlock);
|
||||
} catch (InvocationTargetException e) {
|
||||
throw new RuntimeException(
|
||||
"Cannot send packet " + fakeBlock, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package me.SuperRonanCraft.BetterRTPAddons.addons.portals.cmds;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import com.comphenix.protocol.PacketType;
|
||||
import com.comphenix.protocol.events.PacketContainer;
|
||||
import com.comphenix.protocol.wrappers.BlockPosition;
|
||||
import com.comphenix.protocol.wrappers.WrappedBlockData;
|
||||
|
||||
public class WrapperPlayServerBlockChange extends AbstractPacket {
|
||||
public static final PacketType TYPE = PacketType.Play.Server.BLOCK_CHANGE;
|
||||
|
||||
public WrapperPlayServerBlockChange() {
|
||||
super(new PacketContainer(TYPE), TYPE);
|
||||
handle.getModifier().writeDefaults();
|
||||
}
|
||||
|
||||
public WrapperPlayServerBlockChange(PacketContainer packet) {
|
||||
super(packet, TYPE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Location.
|
||||
* <p>
|
||||
* Notes: block Coordinates
|
||||
*
|
||||
* @return The current Location
|
||||
*/
|
||||
public BlockPosition getLocation() {
|
||||
return handle.getBlockPositionModifier().read(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Location.
|
||||
*
|
||||
* @param value - new value.
|
||||
*/
|
||||
public void setLocation(BlockPosition value) {
|
||||
handle.getBlockPositionModifier().write(0, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the Bukkit Location.
|
||||
*
|
||||
* @param world World for the location
|
||||
* @return Bukkit Location
|
||||
*/
|
||||
public Location getBukkitLocation(World world) {
|
||||
return getLocation().toVector().toLocation(world);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Block Data.
|
||||
*
|
||||
* @return The current Block Data
|
||||
*/
|
||||
public WrappedBlockData getBlockData() {
|
||||
return handle.getBlockData().read(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Block Data.
|
||||
*
|
||||
* @param value - new value.
|
||||
*/
|
||||
public void setBlockData(WrappedBlockData value) {
|
||||
handle.getBlockData().write(0, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user