Fixed new code smells in development branch

- static final class variable names in PingCountTimerBukkit
- plugin field hiding protected variable in SpongeTaskSystem
- InspectPageResponse not overriding equals method
This commit is contained in:
Rsl1122 2018-09-09 13:50:38 +03:00
parent 18be484c0d
commit 26f64990d3
3 changed files with 30 additions and 15 deletions

View File

@ -10,19 +10,19 @@ import org.spongepowered.api.Sponge;
import org.spongepowered.api.scheduler.Task; import org.spongepowered.api.scheduler.Task;
public class SpongeTaskSystem extends ServerTaskSystem { public class SpongeTaskSystem extends ServerTaskSystem {
private final PlanSponge plugin; private final PlanSponge planSponge;
public SpongeTaskSystem(PlanSponge plugin) { public SpongeTaskSystem(PlanSponge plugin) {
super(plugin, new SpongeTPSCountTimer(plugin)); super(plugin, new SpongeTPSCountTimer(plugin));
this.plugin = plugin; this.planSponge = plugin;
} }
@Override @Override
public void enable() { public void enable() {
super.enable(); super.enable();
PingCountTimerSponge pingCountTimer = new PingCountTimerSponge(); PingCountTimerSponge pingCountTimer = new PingCountTimerSponge();
plugin.registerListener(pingCountTimer); planSponge.registerListener(pingCountTimer);
long startDelay = TimeAmount.SECOND.ticks() * (long) Settings.PING_SERVER_ENABLE_DELAY.getNumber(); long startDelay = TimeAmount.SECOND.ticks() * (long) Settings.PING_SERVER_ENABLE_DELAY.getNumber();
RunnableFactory.createNew("PingCountTimer", pingCountTimer) RunnableFactory.createNew("PingCountTimer", pingCountTimer)
.runTaskTimer(startDelay, PingCountTimerSponge.PING_INTERVAL); .runTaskTimer(startDelay, PingCountTimerSponge.PING_INTERVAL);
@ -31,7 +31,7 @@ public class SpongeTaskSystem extends ServerTaskSystem {
@Override @Override
public void disable() { public void disable() {
super.disable(); super.disable();
for (Task task : Sponge.getScheduler().getScheduledTasks(plugin)) { for (Task task : Sponge.getScheduler().getScheduledTasks(planSponge)) {
task.cancel(); task.cancel();
} }
} }

View File

@ -59,17 +59,17 @@ public class PingCountTimerBukkit extends AbsRunnable implements Listener {
//https://github.com/bergerkiller/CraftSource/blob/master/net.minecraft.server/PlayerConnection.java#L178 //https://github.com/bergerkiller/CraftSource/blob/master/net.minecraft.server/PlayerConnection.java#L178
public static final int PING_INTERVAL = 2 * 20; public static final int PING_INTERVAL = 2 * 20;
private static final boolean pingMethodAvailable; private static final boolean PING_METHOD_AVAILABLE;
private static final MethodHandle pingField; private static final MethodHandle PING_FIELD;
private static final MethodHandle getHandleMethod; private static final MethodHandle GET_HANDLE_METHOD;
static { static {
pingMethodAvailable = isPingMethodAvailable(); PING_METHOD_AVAILABLE = isPingMethodAvailable();
MethodHandle localHandle = null; MethodHandle localHandle = null;
MethodHandle localPing = null; MethodHandle localPing = null;
if (!pingMethodAvailable) { if (!PING_METHOD_AVAILABLE) {
Class<?> craftPlayerClass = Reflection.getCraftBukkitClass("entity.CraftPlayer"); Class<?> craftPlayerClass = Reflection.getCraftBukkitClass("entity.CraftPlayer");
Class<?> entityPlayer = Reflection.getMinecraftClass("EntityPlayer"); Class<?> entityPlayer = Reflection.getMinecraftClass("EntityPlayer");
@ -84,8 +84,8 @@ public class PingCountTimerBukkit extends AbsRunnable implements Listener {
} }
} }
getHandleMethod = localHandle; GET_HANDLE_METHOD = localHandle;
pingField = localPing; PING_FIELD = localPing;
} }
private final Map<UUID, List<DateObj<Integer>>> playerHistory = new HashMap<>(); private final Map<UUID, List<DateObj<Integer>>> playerHistory = new HashMap<>();
@ -133,7 +133,7 @@ public class PingCountTimerBukkit extends AbsRunnable implements Listener {
} }
private int getPing(Player player) { private int getPing(Player player) {
if (pingMethodAvailable) { if (PING_METHOD_AVAILABLE) {
return player.spigot().getPing(); return player.spigot().getPing();
} }
@ -142,8 +142,8 @@ public class PingCountTimerBukkit extends AbsRunnable implements Listener {
private int getReflectionPing(Player player) { private int getReflectionPing(Player player) {
try { try {
Object entityPlayer = getHandleMethod.invoke(player); Object entityPlayer = GET_HANDLE_METHOD.invoke(player);
return (int) pingField.invoke(entityPlayer); return (int) PING_FIELD.invoke(entityPlayer);
} catch (Exception ex) { } catch (Exception ex) {
return -1; return -1;
} catch (Throwable throwable) { } catch (Throwable throwable) {

View File

@ -9,6 +9,7 @@ import org.apache.commons.text.StringSubstitutor;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
/** /**
@ -48,4 +49,18 @@ public class InspectPageResponse extends PageResponse {
refreshPage.replacePlaceholders(); refreshPage.replacePlaceholders();
return new InspectPageResponse(null, refreshPage.getContent()); return new InspectPageResponse(null, refreshPage.getContent());
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof InspectPageResponse)) return false;
if (!super.equals(o)) return false;
InspectPageResponse that = (InspectPageResponse) o;
return Objects.equals(uuid, that.uuid);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), uuid);
}
} }