Compare commits

...
2 Commits
Author SHA1 Message Date
retrooper 38c4f3234d Merge pull request #66 from retrooper/dev
1.7.7 release
2020-11-16 17:55:11 +01:00
retrooper e94b6f24dc 1.7.7 release
* Accessing Entity by ID on servers with more than one world ERROR FIX
* In the WrappedPacketOutChat(String, ChatPosition, UUID) constructor, the String(=message) shouldn't be passed in JSON string format, it can be passed in as an ordinary string message!
Example:
WrappedPacketOutChat packet = new WrappedPacketOutChat("Hello", ChatPosition.CHAT, uuid);
2020-11-16 17:52:37 +01:00
8 changed files with 67 additions and 116 deletions

No files matched your search

+1 -1
View File
@@ -6,7 +6,7 @@
<groupId>io.github.retrooper</groupId>
<artifactId>packetevents</artifactId>
<version>1.7.6</version>
<version>1.7.7</version>
<!-- You can build the project with this: "mvn clean compile assembly:single" -->
<build>
@@ -49,6 +49,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
@@ -57,9 +58,8 @@ public final class PacketEvents implements Listener {
private static final PacketEventsAPI packetEventsAPI = new PacketEventsAPI();
private static final PacketEvents instance = new PacketEvents();
private static final ArrayList<Plugin> plugins = new ArrayList<>(1);
private static boolean loaded, initialized;
private static final PEVersion version = new PEVersion(1, 7, 6);
private static boolean loading, loaded, initialized, initializing;
private static final PEVersion version = new PEVersion(1, 7, 7);
private static PacketEventsSettings settings = new PacketEventsSettings();
/**
* General executor service, basically for anything that the packet executor service doesn't do.
@@ -88,7 +88,8 @@ public final class PacketEvents implements Listener {
* All PacketEvents' wrappers are setup and do all loading they need to do.
*/
public static void load() {
if (!loaded) {
if (!loaded && !loading) {
loading = true;
ServerVersion version = ServerVersion.getVersion();
WrappedPacket.version = version;
PacketEvent.version = version;
@@ -107,9 +108,11 @@ public final class PacketEvents implements Listener {
WrappedPacket.loadAllWrappers();
} catch (Exception ex) {
loading = false;
throw new PacketEventsLoadFailureException();
}
loaded = true;
loading = false;
}
}
@@ -135,14 +138,14 @@ public final class PacketEvents implements Listener {
*/
public static void init(final Plugin pl, PacketEventsSettings packetEventsSettings) {
load();
if (!initialized) {
if (!initialized && !initializing) {
initializing = true;
settings = packetEventsSettings;
int packetHandlingThreadCount = settings.getPacketHandlingThreadCount();
//if the count is 1 or is invalid
if(packetHandlingThreadCount == 1 || packetHandlingThreadCount < 0) {
if (packetHandlingThreadCount == 1 || packetHandlingThreadCount < 0) {
packetHandlingExecutorService = Executors.newSingleThreadExecutor();
}
else {
} else {
packetHandlingExecutorService = Executors.newFixedThreadPool(packetHandlingThreadCount);
}
plugins.add(pl);
@@ -152,11 +155,17 @@ public final class PacketEvents implements Listener {
PacketEvents.getAPI().packetManager = new PacketManager(plugins.get(0), settings.shouldInjectEarly());
for (final Player p : Bukkit.getOnlinePlayers()) {
getAPI().getPlayerUtils().injectPlayer(p);
try {
getAPI().getPlayerUtils().injectPlayer(p);
} catch (Exception ex) {
p.kickPlayer("Failed to inject you, please rejoin the server!");
}
}
if (settings.shouldCheckForUpdates()) {
PacketEvents.generalExecutorService.execute(() -> new UpdateChecker().handleUpdate());
Future<?> future =
PacketEvents.generalExecutorService
.submit(() -> new UpdateChecker().handleUpdate());
}
if (getAPI().getServerUtils().isBungeeCordEnabled()) {
@@ -165,6 +174,7 @@ public final class PacketEvents implements Listener {
}
initialized = true;
initializing = false;
}
}
@@ -221,9 +231,7 @@ public final class PacketEvents implements Listener {
if (PacketEvents.getSettings().shouldInjectEarly()) {
assert getAPI().packetManager.tinyProtocol != null;
try {
if (getAPI().packetManager.canInject(e.getPlayer())) {
getAPI().packetManager.injectPlayer(e.getPlayer());
}
getAPI().packetManager.injectPlayer(e.getPlayer());
} catch (Exception ex) {
e.disallow(PlayerLoginEvent.Result.KICK_OTHER, "We are unable to inject you. Please try again!");
}
@@ -232,15 +240,17 @@ public final class PacketEvents implements Listener {
@EventHandler(priority = EventPriority.LOW)
public void onJoin(final PlayerJoinEvent e) {
if(PacketEvents.getAPI().getServerUtils().getVersion() == ServerVersion.v_1_7_10) {
if (PacketEvents.getAPI().getServerUtils().getVersion() == ServerVersion.v_1_7_10) {
Object channel = NMSUtils.getChannel(e.getPlayer());
ClientVersion version = ClientVersion.getClientVersion(ProtocolVersionAccessor_v_1_7.getProtocolVersion(e.getPlayer()));
PacketEvents.getAPI().getPlayerUtils().clientVersionsMap.put(channel, version);
}
//Waiting for the BungeeCord server to send their plugin message with your version,
if (!PacketEvents.getSettings().shouldInjectEarly()) {
if (getAPI().packetManager.canInject(e.getPlayer())) {
try {
PacketEvents.getAPI().packetManager.injectPlayer(e.getPlayer());
} catch (Exception ex) {
e.getPlayer().kickPlayer("There was an issue injecting you. Please try again!");
}
}
}
@@ -30,6 +30,6 @@ public class PacketEventsNMSCachedEntityNotFoundException extends RuntimeExcepti
}
public PacketEventsNMSCachedEntityNotFoundException(int entityID) {
this("PacketEvents failed to find an entity with ID " + entityID + " in NMS' entity cache map!");
this("PacketEvents failed to find an entity with ID " + entityID + "!");
}
}
@@ -95,8 +95,10 @@ public class PacketManager {
PacketEvents.getAPI().getEventManager().callEvent(injectEvent);
if (!injectEvent.isCancelled()) {
if (tinyProtocolMode) {
assert tinyProtocol != null;
tinyProtocol.injectPlayerAsync(player);
} else {
assert nettyProtocol != null;
nettyProtocol.injectPlayerAsync(player);
}
}
@@ -160,10 +162,6 @@ public class PacketManager {
}
}
public boolean canInject(Player player) {
return true;
}
public String getNettyHandlerName() {
return "pe-" + plugin.getName() + "-" + handlerNumber;
}
@@ -1,53 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2020 retrooper
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.github.retrooper.packetevents.packetwrappers.out.blockchange;
import io.github.retrooper.packetevents.packetwrappers.WrappedPacket;
class WrappedPacketOutBlockChange extends WrappedPacket {
public WrappedPacketOutBlockChange(Object packet) {
super(packet);
}
//to-do load
@Override
protected void setup() {
//1.7 - int, int, int, Block, int data
//1.8+ - BlockPosition, IBlockData
//Constructors
//1.7 - int, int, int, World
//1.8->~1.12.2(World, BlockPosition)
//~1.13->~1.14(IBlockAccess, BlockPosition)
//1.16+(BlockPosition, IBlockData)
net.minecraft.server.v1_7_R4.PacketPlayOutBlockChange bc;
net.minecraft.server.v1_8_R3.PacketPlayOutBlockChange bc1;
net.minecraft.server.v1_9_R1.PacketPlayOutBlockChange bc2;
net.minecraft.server.v1_12_R1.PacketPlayOutBlockChange bc3;
net.minecraft.server.v1_13_R2.PacketPlayOutBlockChange bc4;
net.minecraft.server.v1_16_R2.PacketPlayOutBlockChange bc5;
}
}
@@ -79,7 +79,7 @@ public final class WrappedPacketOutChat extends WrappedPacket implements Sendabl
public WrappedPacketOutChat(String message, ChatPosition chatPosition, UUID uuid) {
this.uuid = uuid;
this.message = message;
this.message = fromStringToJSON(message);
this.chatPosition = chatPosition;
}
@@ -40,10 +40,8 @@ public final class EntityFinderUtils {
public static ServerVersion version;
private static Class<?> worldServerClass;
private static Class<?> craftWorldClass;
private static Class<?> entityClass;
private static Method getEntityByIdMethod;
private static Method craftWorldGetHandle;
private static Method getBukkitEntity;
private static boolean isServerVersion_v_1_8_x;
@@ -52,15 +50,13 @@ public final class EntityFinderUtils {
try {
worldServerClass = NMSUtils.getNMSClass("WorldServer");
craftWorldClass = NMSUtils.getOBCClass("CraftWorld");
entityClass = NMSUtils.getNMSClass("Entity");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
getEntityByIdMethod = worldServerClass.getMethod(getEntityByIDMethodName(), int.class);
getEntityByIdMethod = worldServerClass.getMethod(isServerVersion_v_1_8_x ? "a" : "getEntity", int.class);
craftWorldGetHandle = craftWorldClass.getMethod("getHandle");
getBukkitEntity = entityClass.getMethod("getBukkitEntity");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
@@ -73,6 +69,14 @@ public final class EntityFinderUtils {
return entity;
}
}
//Entity has not been found in the NMS entity cache map...
for(World world : Bukkit.getWorlds()) {
for(Entity entity : world.getEntities()) {
if(entity.getEntityId() == id) {
return NMSUtils.getNMSEntity(entity);
}
}
}
throw new PacketEventsNMSCachedEntityNotFoundException(id);
}
@@ -83,11 +87,19 @@ public final class EntityFinderUtils {
* @return Entity
*/
public static Entity getEntityById(final int id) {
Object nmsEntity = getNMSEntityById(id);
try {
return (Entity) getBukkitEntity.invoke(nmsEntity);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
for (final World world : Bukkit.getWorlds()) {
final Object entity = getNMSEntityByIdWithWorld(world, id);
if (entity != null) {
return NMSUtils.getBukkitEntity(entity);
}
}
//Entity has not been found in the NMS entity cache map...
for(World world : Bukkit.getWorlds()) {
for(Entity entity : world.getEntities()) {
if(entity.getEntityId() == id) {
return entity;
}
}
}
throw new PacketEventsNMSCachedEntityNotFoundException(id);
}
@@ -98,25 +110,19 @@ public final class EntityFinderUtils {
} else if (craftWorldClass == null) {
throw new IllegalStateException("PacketEvents failed to locate the CraftWorld class.");
}
Object craftWorld = craftWorldClass.cast(world);
Object worldServer = null;
try {
worldServer = craftWorldGetHandle.invoke(craftWorld);
worldServer = craftWorldGetHandle.invoke(world);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
Object nmsEntity = null;
try {
nmsEntity = getEntityByIdMethod.invoke(worldServer, id);
return getEntityByIdMethod.invoke(worldServer, id);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
if(nmsEntity == null) {
throw new PacketEventsNMSCachedEntityNotFoundException(id);
}
return nmsEntity;
return null;
}
/**
@@ -129,27 +135,6 @@ public final class EntityFinderUtils {
@Nullable
public static Entity getEntityByIdWithWorld(final World world, final int id) {
Object nmsEntity = getNMSEntityByIdWithWorld(world, id);
if (nmsEntity == null) {
return null;
}
try {
return (Entity) getBukkitEntity.invoke(nmsEntity);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
throw new PacketEventsNMSCachedEntityNotFoundException(id);
}
/**
* This is the name of the method that returns the NMS entity when you pass in its ID.
* On 1.8-1.8.8 the function name is called 'a', on 1.7.10 and 1.9 and above the name is called 'getEntity'.
*
* @return entity by ID method name
*/
public static String getEntityByIDMethodName() {
if (isServerVersion_v_1_8_x) {
return "a";
}
return "getEntity";
return NMSUtils.getBukkitEntity(nmsEntity);
}
}
@@ -55,6 +55,7 @@ public final class NMSUtils {
private static Method getCraftPlayerHandle;
private static Method getCraftEntityHandle;
private static Method asBukkitCopy;
private static Method getBukkitEntity;
private static Field entityPlayerPingField, playerConnectionField;
public static final HashMap<UUID, Object> channelCache = new HashMap<>();
@@ -102,6 +103,7 @@ public final class NMSUtils {
getCraftEntityHandle = craftEntityClass.getMethod("getHandle");
asBukkitCopy = craftItemStack.getMethod("asBukkitCopy", nmsItemStackClass);
craftWorldGetHandle = craftWorldClass.getMethod("getHandle");
getBukkitEntity = nmsEntityClass.getMethod("getBukkitEntity");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
@@ -196,6 +198,15 @@ public final class NMSUtils {
return null;
}
public static Entity getBukkitEntity(final Object nmsEntity) {
try {
return (Entity) getBukkitEntity.invoke(nmsEntity);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
return null;
}
public static Object getCraftPlayer(final Player player) {
return craftPlayerClass.cast(player);
}