Skip to content
This repository was archived by the owner on Feb 15, 2022. It is now read-only.

Commit 09f6b19

Browse files
committed
Cleanup the code and fixed default config
1 parent ccf9cc5 commit 09f6b19

File tree

8 files changed

+74
-70
lines changed

8 files changed

+74
-70
lines changed

blccpsapibukkit/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,4 @@
7777
</repository>
7878
</repositories>
7979

80-
8180
</project>

blccpsapibukkit/src/main/java/net/badlion/blccpsapibukkit/BlcCpsApiBukkit.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
import net.badlion.blccpsapibukkit.listener.PlayerListener;
66
import org.bukkit.plugin.java.JavaPlugin;
77

8-
import java.io.BufferedReader;
98
import java.io.File;
109
import java.io.FileNotFoundException;
1110
import java.io.FileReader;
1211
import java.io.FileWriter;
1312
import java.io.IOException;
14-
import java.io.Reader;
1513
import java.util.logging.Level;
1614

1715
public class BlcCpsApiBukkit extends JavaPlugin {
1816

1917
public static final Gson GSON_NON_PRETTY = new GsonBuilder().enableComplexMapKeySerialization().disableHtmlEscaping().create();
20-
public static final Gson GSON_PRETTY = new GsonBuilder().enableComplexMapKeySerialization().disableHtmlEscaping().setPrettyPrinting().create();
18+
private static final Gson GSON_PRETTY = new GsonBuilder().enableComplexMapKeySerialization().disableHtmlEscaping().setPrettyPrinting().create();
2119

2220
private Conf conf;
2321

@@ -46,9 +44,8 @@ public void onEnable() {
4644
this.getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
4745

4846
this.getLogger().log(Level.INFO, "Successfully setup BadlionClientCPSAPI plugin.");
49-
} catch (IOException e) {
50-
this.getLogger().log(Level.SEVERE, "Error with config for BadlionClientCPSAPI plugin.");
51-
e.printStackTrace();
47+
} catch (Exception ex) {
48+
this.getLogger().log(Level.SEVERE, "Error with config for BadlionClientCPSAPI plugin : " + ex.getMessage(), ex);
5249
}
5350
}
5451

@@ -57,29 +54,48 @@ public void onDisable() {
5754

5855
}
5956

60-
public Conf loadConf(File file) throws IOException {
57+
private Conf loadConf(File file) {
58+
FileReader fileReader = null;
59+
6160
try {
62-
Reader reader = new BufferedReader(new FileReader(file));
63-
return BlcCpsApiBukkit.GSON_NON_PRETTY.fromJson(reader, Conf.class);
61+
fileReader = new FileReader(file);
62+
return BlcCpsApiBukkit.GSON_NON_PRETTY.fromJson(fileReader, Conf.class);
6463
} catch (FileNotFoundException ex) {
6564
this.getLogger().log(Level.INFO,"No Config Found: Saving default...");
6665
Conf conf = new Conf();
67-
this.saveConf(conf, new File(this.getDataFolder(), "config.json"));
66+
this.saveConf(conf, file);
6867
return conf;
68+
} finally {
69+
if (fileReader != null) {
70+
try {
71+
fileReader.close();
72+
} catch (IOException ignored) {
73+
74+
}
75+
}
6976
}
7077
}
7178

7279
private void saveConf(Conf conf, File file) {
80+
FileWriter fileWriter = null;
81+
7382
try {
74-
FileWriter writer = new FileWriter(file);
75-
BlcCpsApiBukkit.GSON_PRETTY.toJson(conf, writer);
83+
fileWriter = new FileWriter(file);
84+
BlcCpsApiBukkit.GSON_PRETTY.toJson(conf, fileWriter);
7685
} catch (Exception ex) {
7786
ex.printStackTrace();
87+
} finally {
88+
if (fileWriter != null) {
89+
try {
90+
fileWriter.close();
91+
} catch (IOException ignored) {
92+
93+
}
94+
}
7895
}
7996
}
8097

8198
public Conf getConf() {
8299
return this.conf;
83100
}
84-
85101
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package net.badlion.blccpsapibukkit;
22

33
public class Conf {
4-
54
private int clicksPerSecondLimit = 20;
6-
75
}

blccpsapibukkit/src/main/java/net/badlion/blccpsapibukkit/listener/PlayerListener.java

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package net.badlion.blccpsapibukkit.listener;
22

33
import net.badlion.blccpsapibukkit.BlcCpsApiBukkit;
4-
import org.bukkit.Bukkit;
54
import org.bukkit.entity.Player;
65
import org.bukkit.event.EventHandler;
76
import org.bukkit.event.Listener;
@@ -18,33 +17,25 @@ public class PlayerListener implements Listener {
1817
private BlcCpsApiBukkit plugin;
1918
private String versionSuffix;
2019

21-
// Reflection stuff
22-
private Class<?> craftPlayerClass;
23-
private Method getHandleMethod;
20+
private Method getHandleMethod;
2421

25-
private Class<?> nmsPlayerClass;
26-
private Field playerConnectionField;
22+
private Field playerConnectionField;
2723

28-
private Class<?> playerConnectionClass;
29-
private Method sendPacketMethod;
24+
private Method sendPacketMethod;
3025

31-
private Class<?> packetPlayOutCustomPayloadClass;
32-
private Constructor<?> packetPlayOutCustomPayloadConstructor;
26+
private Constructor<?> packetPlayOutCustomPayloadConstructor;
3327

3428
// Bukkit 1.8+ support
3529
private Class<?> packetDataSerializerClass;
3630
private Constructor<?> packetDataSerializerConstructor;
3731

38-
// Netty classes used by newer 1.8 and newer
39-
private Class<?> byteBufClass;
40-
private Class<?> unpooledClass;
41-
private Method wrappedBufferMethod;
32+
private Method wrappedBufferMethod;
4233

4334
public PlayerListener(BlcCpsApiBukkit plugin) {
4435
this.plugin = plugin;
4536

4637
// Get the v1_X_Y from the end of the package name, e.g. v_1_7_R4 or v_1_12_R1
47-
String packageName = Bukkit.getServer().getClass().getPackage().getName();
38+
String packageName = plugin.getServer().getClass().getPackage().getName();
4839
String[] parts = packageName.split("\\.");
4940

5041
if (parts.length > 0) {
@@ -59,72 +50,74 @@ public PlayerListener(BlcCpsApiBukkit plugin) {
5950
}
6051

6152
// We need to use reflection because Bukkit by default handles plugin messages in a really silly way
62-
this.craftPlayerClass = this.getClass("org.bukkit.craftbukkit." + this.versionSuffix + ".entity.CraftPlayer");
63-
if (this.craftPlayerClass == null) {
53+
// Reflection stuff
54+
Class<?> craftPlayerClass = this.getClass("org.bukkit.craftbukkit." + this.versionSuffix + ".entity.CraftPlayer");
55+
if (craftPlayerClass == null) {
6456
throw new RuntimeException("Failed to find CraftPlayer class");
6557
}
6658

67-
this.nmsPlayerClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".EntityPlayer");
68-
if (this.nmsPlayerClass == null) {
59+
Class<?> nmsPlayerClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".EntityPlayer");
60+
if (nmsPlayerClass == null) {
6961
throw new RuntimeException("Failed to find EntityPlayer class");
7062
}
7163

72-
this.playerConnectionClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".PlayerConnection");
73-
if (this.playerConnectionClass == null) {
64+
Class<?> playerConnectionClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".PlayerConnection");
65+
if (playerConnectionClass == null) {
7466
throw new RuntimeException("Failed to find PlayerConnection class");
7567
}
7668

77-
this.packetPlayOutCustomPayloadClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".PacketPlayOutCustomPayload");
78-
if (this.packetPlayOutCustomPayloadClass == null) {
69+
Class<?> packetPlayOutCustomPayloadClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".PacketPlayOutCustomPayload");
70+
if (packetPlayOutCustomPayloadClass == null) {
7971
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload class");
8072
}
8173

82-
this.packetPlayOutCustomPayloadConstructor = this.getConstructor(this.packetPlayOutCustomPayloadClass, String.class, byte[].class);
74+
this.packetPlayOutCustomPayloadConstructor = this.getConstructor(packetPlayOutCustomPayloadClass, String.class, byte[].class);
8375
if (this.packetPlayOutCustomPayloadConstructor == null) {
8476
// Newer versions of Minecraft use a different custom packet system
8577
this.packetDataSerializerClass = this.getClass("net.minecraft.server." + this.versionSuffix + ".PacketDataSerializer");
8678
if (this.packetDataSerializerClass == null) {
8779
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or PacketDataSerializer class");
8880
}
8981

90-
this.byteBufClass = this.getClass("io.netty.buffer.ByteBuf");
91-
if (this.byteBufClass == null) {
82+
// Netty classes used by newer 1.8 and newer
83+
Class<?> byteBufClass = this.getClass("io.netty.buffer.ByteBuf");
84+
if (byteBufClass == null) {
9285
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or ByteBuf class");
9386
}
9487

95-
this.packetDataSerializerConstructor = this.getConstructor(this.packetDataSerializerClass, this.byteBufClass);
88+
this.packetDataSerializerConstructor = this.getConstructor(this.packetDataSerializerClass, byteBufClass);
9689
if (this.packetDataSerializerConstructor == null) {
9790
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or PacketDataSerializer constructor");
9891
}
9992

100-
this.unpooledClass = this.getClass("io.netty.buffer.Unpooled");
101-
if (this.unpooledClass == null) {
93+
Class<?> unpooledClass = this.getClass("io.netty.buffer.Unpooled");
94+
if (unpooledClass == null) {
10295
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or Unpooled class");
10396
}
10497

105-
this.wrappedBufferMethod = this.getMethod(this.unpooledClass, "wrappedBuffer", byte[].class);
98+
this.wrappedBufferMethod = this.getMethod(unpooledClass, "wrappedBuffer", byte[].class);
10699
if (this.wrappedBufferMethod == null) {
107100
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor or wrappedBuffer()");
108101
}
109102

110103
// If we made it this far in theory we are on at least 1.8
111-
this.packetPlayOutCustomPayloadConstructor = this.getConstructor(this.packetPlayOutCustomPayloadClass, String.class, this.packetDataSerializerClass);
104+
this.packetPlayOutCustomPayloadConstructor = this.getConstructor(packetPlayOutCustomPayloadClass, String.class, this.packetDataSerializerClass);
112105
if (this.packetPlayOutCustomPayloadConstructor == null) {
113106
throw new RuntimeException("Failed to find PacketPlayOutCustomPayload constructor 2x");
114107
}
115108
}
116109

117-
this.getHandleMethod = this.getMethod(this.craftPlayerClass, "getHandle");
110+
this.getHandleMethod = this.getMethod(craftPlayerClass, "getHandle");
118111
if (this.getHandleMethod == null) {
119112
throw new RuntimeException("Failed to find CraftPlayer.getHandle()");
120113
}
121114

122-
this.playerConnectionField = this.getField(this.nmsPlayerClass, "playerConnection");
115+
this.playerConnectionField = this.getField(nmsPlayerClass, "playerConnection");
123116
if (this.playerConnectionField == null) {
124117
throw new RuntimeException("Failed to find EntityPlayer.playerConnection");
125118
}
126119

127-
this.sendPacketMethod = this.getMethod(this.playerConnectionClass, "sendPacket");
120+
this.sendPacketMethod = this.getMethod(playerConnectionClass, "sendPacket");
128121
if (this.sendPacketMethod == null) {
129122
throw new RuntimeException("Failed to find PlayerConnection.sendPacket()");
130123
}
@@ -143,7 +136,7 @@ public void onPlayerJoin(PlayerJoinEvent event) {
143136

144137
// Newer MC version, setup ByteBuf object
145138
if (this.packetDataSerializerClass != null) {
146-
Object byteBuf = this.wrappedBufferMethod.invoke(null, message);
139+
Object byteBuf = this.wrappedBufferMethod.invoke(null, (Object) message);
147140
Object packetDataSerializer = this.packetDataSerializerConstructor.newInstance(byteBuf);
148141

149142
packet = this.packetPlayOutCustomPayloadConstructor.newInstance(channel, packetDataSerializer);
@@ -168,15 +161,15 @@ public void onPlayerJoin(PlayerJoinEvent event) {
168161
}
169162
}
170163

171-
public Class<?> getClass(String className) {
164+
private Class<?> getClass(String className) {
172165
try {
173166
return Class.forName(className);
174167
} catch (ClassNotFoundException e) {
175168
return null;
176169
}
177170
}
178171

179-
public Constructor<?> getConstructor(Class<?> clazz, Class<?> ...params) {
172+
private Constructor<?> getConstructor(Class<?> clazz, Class<?>... params) {
180173
for (final Constructor<?> constructor : clazz.getDeclaredConstructors()) {
181174
if (Arrays.equals(constructor.getParameterTypes(), params)) {
182175
constructor.setAccessible(true);
@@ -187,7 +180,7 @@ public Constructor<?> getConstructor(Class<?> clazz, Class<?> ...params) {
187180
return null;
188181
}
189182

190-
public Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
183+
private Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
191184
for (final Method method : clazz.getDeclaredMethods()) {
192185
if (method.getName().equals(methodName)) {
193186
if (params.length > 0) {
@@ -205,7 +198,7 @@ public Method getMethod(Class<?> clazz, String methodName, Class<?>... params) {
205198
return null;
206199
}
207200

208-
public Field getField(Class<?> clazz, String fieldName) {
201+
private Field getField(Class<?> clazz, String fieldName) {
209202
for (final Field field : clazz.getDeclaredFields()) {
210203
if (field.getName().equals(fieldName)) {
211204
field.setAccessible(true);

blccpsapibungee/src/main/java/net/badlion/blccpsapibungee/BlcCpsApiBungee.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,17 @@
55
import net.badlion.blccpsapibungee.listener.PlayerListener;
66
import net.md_5.bungee.api.plugin.Plugin;
77

8-
import java.io.BufferedReader;
98
import java.io.File;
109
import java.io.FileNotFoundException;
1110
import java.io.FileReader;
1211
import java.io.FileWriter;
1312
import java.io.IOException;
14-
import java.io.Reader;
1513
import java.util.logging.Level;
1614

1715
public class BlcCpsApiBungee extends Plugin {
1816

1917
public static final Gson GSON_NON_PRETTY = new GsonBuilder().enableComplexMapKeySerialization().disableHtmlEscaping().create();
20-
public static final Gson GSON_PRETTY = new GsonBuilder().enableComplexMapKeySerialization().disableHtmlEscaping().setPrettyPrinting().create();
18+
private static final Gson GSON_PRETTY = new GsonBuilder().enableComplexMapKeySerialization().disableHtmlEscaping().setPrettyPrinting().create();
2119

2220
private Conf conf;
2321

@@ -36,7 +34,7 @@ public void onEnable() {
3634
this.getProxy().getPluginManager().registerListener(this, new PlayerListener(this));
3735

3836
this.getLogger().log(Level.INFO, "Successfully setup BadlionClientCPSAPI plugin.");
39-
} catch (IOException e) {
37+
} catch (Exception e) {
4038
this.getLogger().log(Level.SEVERE, "Error with config for BadlionClientCPSAPI plugin.");
4139
e.printStackTrace();
4240
}
@@ -47,20 +45,22 @@ public void onDisable() {
4745

4846
}
4947

50-
public Conf loadConf(File file) throws IOException {
51-
try (Reader reader = new BufferedReader(new FileReader(file))) {
52-
return BlcCpsApiBungee.GSON_NON_PRETTY.fromJson(reader, Conf.class);
48+
private Conf loadConf(File file) throws IOException {
49+
50+
try (FileReader fileReader = new FileReader(file)) {
51+
return BlcCpsApiBungee.GSON_NON_PRETTY.fromJson(fileReader, Conf.class);
5352
} catch (FileNotFoundException ex) {
54-
this.getLogger().log(Level.INFO,"No Config Found: Saving default...");
53+
this.getLogger().log(Level.INFO, "No Config Found: Saving default...");
5554
Conf conf = new Conf();
56-
this.saveConf(conf, new File(this.getDataFolder(), "config.json"));
55+
this.saveConf(conf, file);
5756
return conf;
5857
}
5958
}
6059

6160
private void saveConf(Conf conf, File file) {
62-
try (FileWriter writer = new FileWriter(file)) {
63-
BlcCpsApiBungee.GSON_PRETTY.toJson(conf, writer);
61+
62+
try (FileWriter fileWriter = new FileWriter(file)) {
63+
BlcCpsApiBungee.GSON_PRETTY.toJson(conf, fileWriter);
6464
} catch (Exception ex) {
6565
ex.printStackTrace();
6666
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package net.badlion.blccpsapibungee;
22

33
public class Conf {
4-
54
private int clicksPerSecondLimit = 20;
6-
75
}

blccpsapibungee/src/main/java/net/badlion/blccpsapibungee/listener/PlayerListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public PlayerListener(BlcCpsApiBungee plugin) {
2121
public void onLogin(PostLoginEvent event) {
2222
// Send the click speed limitation to players when they login to the proxy. A notification will appear on the Badlion Client so they know their CPS has been limited
2323
ProxiedPlayer player = event.getPlayer();
24-
if ( player.getPendingConnection().getVersion() <= ProtocolConstants.MINECRAFT_1_12_2 ) { // do not send to 1.13+ clients, this would break the connection
24+
if (player.getPendingConnection().getVersion() <= ProtocolConstants.MINECRAFT_1_12_2) { // do not send to 1.13+ clients, this would break the connection
2525
player.unsafe().sendPacket( new PluginMessage( "BLC|C", BlcCpsApiBungee.GSON_NON_PRETTY.toJson( this.plugin.getConf() ).getBytes(), false ) );
2626
}
2727
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
<artifactId>badlionclientclickspersecondapi</artifactId>
1010
<packaging>pom</packaging>
1111
<version>1.0.0-SNAPSHOT</version>
12+
1213
<modules>
1314
<module>blccpsapibungee</module>
1415
<module>blccpsapibukkit</module>
1516
</modules>
1617

17-
1818
</project>

0 commit comments

Comments
 (0)