Skip to content

Commit 17a865b

Browse files
committed
Removed usage of reflection and unsafe stuff
1 parent 05ab5c9 commit 17a865b

File tree

6 files changed

+14
-94
lines changed

6 files changed

+14
-94
lines changed

src/main/java/de/pascalpex/pexnpc/commands/PexNPCCommand.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import de.pascalpex.pexnpc.PexNPC;
44
import de.pascalpex.pexnpc.commands.subcommands.SubCommands;
5-
import de.pascalpex.pexnpc.files.NPCData;
65
import de.pascalpex.pexnpc.util.MessageHandler;
76
import io.papermc.paper.command.brigadier.BasicCommand;
87
import io.papermc.paper.command.brigadier.CommandSourceStack;
@@ -17,6 +16,9 @@ public class PexNPCCommand implements BasicCommand {
1716
@Override
1817
public void execute(CommandSourceStack commandSourceStack, String @NotNull [] args) {
1918
CommandSender sender = commandSourceStack.getSender();
19+
if(sender != commandSourceStack.getExecutor()) {
20+
sender.sendMessage(MessageHandler.errorMessage("Different senders and executors are currently not supported"));
21+
}
2022
if(sender instanceof Player player) {
2123
if(args.length == 0) {
2224
SubCommands.HELP.getSubCommand().invoke(player, new String[0]);
@@ -33,7 +35,7 @@ public void execute(CommandSourceStack commandSourceStack, String @NotNull [] ar
3335
}
3436

3537
@Override
36-
public Collection<String> suggest(CommandSourceStack commandSourceStack, String[] args) {
38+
public @NotNull Collection<String> suggest(CommandSourceStack commandSourceStack, String @NotNull [] args) {
3739
CommandSender sender = commandSourceStack.getSender();
3840
final List<String> completions = new ArrayList<>();
3941

src/main/java/de/pascalpex/pexnpc/events/PacketReader.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package de.pascalpex.pexnpc.events;
22

33
import de.pascalpex.pexnpc.npc.PlaceableNPC;
4-
import de.pascalpex.pexnpc.util.ReflectionHelper;
54
import io.netty.channel.Channel;
65
import io.netty.channel.ChannelHandlerContext;
76
import io.netty.handler.codec.MessageToMessageDecoder;
8-
import net.minecraft.network.Connection;
97
import net.minecraft.network.protocol.Packet;
108
import net.minecraft.network.protocol.game.ServerboundInteractPacket;
119
import net.minecraft.server.level.ServerPlayer;
@@ -26,8 +24,7 @@ public class PacketReader {
2624
public void inject(Player player) throws NoSuchFieldException, IllegalAccessException {
2725
CraftPlayer craftPlayer = (CraftPlayer) player;
2826
ServerGamePacketListenerImpl serverConnection = craftPlayer.getHandle().connection;
29-
Connection connection = (Connection) ReflectionHelper.getValue(serverConnection, "e");
30-
channel = connection.channel;
27+
channel = serverConnection.connection.channel;
3128
channels.put(player.getUniqueId(), channel);
3229

3330
if (channel.pipeline().get("PacketInjector") != null) {
@@ -61,11 +58,8 @@ public void uninject(Player player) {
6158
}
6259

6360
public void readPacket(Player player, Packet<?> packet) {
64-
65-
String packetName = packet.getClass().getSimpleName();
66-
if (packetName.equalsIgnoreCase("PacketPlayInUseEntity") || packetName.equalsIgnoreCase("ServerboundInteractPacket")) {
67-
68-
int id = (int) ReflectionHelper.getValue(packet, "b");
61+
if (packet instanceof ServerboundInteractPacket serverboundInteractPacket) {
62+
int id = serverboundInteractPacket.getEntityId();
6963

7064
for (ServerPlayer npc : PexNPC.getPlacedNpcs().stream().map(PlaceableNPC::getServerPlayer).toList()) {
7165
if (npc.getId() == id) {

src/main/java/de/pascalpex/pexnpc/files/NPCData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static void load() {
3030
configFile.getParentFile().mkdirs();
3131
if (!configFile.exists()) {
3232
configFile.createNewFile();
33-
save();
33+
config.save(configFile);
3434
}
3535
config.load(configFile);
3636
} catch (IOException | InvalidConfigurationException e) {
@@ -50,7 +50,7 @@ public void run() {
5050
PexNPC.logger().log(Level.SEVERE, e.toString());
5151
}
5252
}
53-
}.runTask(PexNPC.getInstance());
53+
}.runTaskAsynchronously(PexNPC.getInstance());
5454
}
5555

5656
public static void saveNpc(NPC npc) {

src/main/java/de/pascalpex/pexnpc/npc/NPCSender.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.mojang.datafixers.util.Pair;
55
import de.pascalpex.pexnpc.PexNPC;
66
import de.pascalpex.pexnpc.files.Config;
7-
import de.pascalpex.pexnpc.util.ReflectionHelper;
87
import net.minecraft.network.chat.ComponentUtils;
98
import net.minecraft.network.protocol.game.*;
109
import net.minecraft.server.level.ServerPlayer;
@@ -66,7 +65,7 @@ private static void sendNPC(PlaceableNPC placeableNPC, Player player) {
6665
ServerPlayer serverPlayer = placeableNPC.getServerPlayer();
6766

6867
ServerGamePacketListenerImpl connection = ((CraftPlayer) player).getHandle().connection;
69-
connection.send(ReflectionHelper.createInitPacket(serverPlayer));
68+
connection.send(new ClientboundPlayerInfoUpdatePacket(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER, serverPlayer));
7069
// connection.send(new ClientboundEntityEventPacket(serverPlayer, (byte) 1));
7170
Vec3 pos = serverPlayer.position();
7271
connection.send(new ClientboundAddEntityPacket(serverPlayer.getId(), serverPlayer.getUUID(), pos.x(), pos.y(), pos.z(), serverPlayer.getXRot(), serverPlayer.getYRot(), serverPlayer.getType(), 0, serverPlayer.getDeltaMovement(), serverPlayer.getYHeadRot()));

src/main/java/de/pascalpex/pexnpc/util/ReflectionHelper.java

Lines changed: 0 additions & 75 deletions
This file was deleted.

src/main/java/de/pascalpex/pexnpc/util/SkinDownloader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818
public class SkinDownloader {
1919

2020
public static NPCSkin downloadSkin(String playerName) throws IOException, URISyntaxException {
21-
URL url = new URI("https://api.mojang.com/users/profiles/minecraft/" + playerName).toURL();
22-
InputStreamReader profileReader = new InputStreamReader(url.openStream());
21+
URL profileUrl = new URI("https://api.mojang.com/users/profiles/minecraft/" + playerName).toURL();
22+
InputStreamReader profileReader = new InputStreamReader(profileUrl.openStream());
2323
String uuid = JsonParser.parseReader(profileReader).getAsJsonObject().get("id").getAsString();
2424

25-
URL url2 = new URI("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false").toURL();
26-
InputStreamReader skinReader = new InputStreamReader(url2.openStream());
25+
URL skinUrl = new URI("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid + "?unsigned=false").toURL();
26+
InputStreamReader skinReader = new InputStreamReader(skinUrl.openStream());
2727
JsonObject property = JsonParser.parseReader(skinReader).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
2828
String texture = property.get("value").getAsString();
2929
String signature = property.get("signature").getAsString();

0 commit comments

Comments
 (0)