Skip to content

Commit 395d77e

Browse files
committed
Reimplement auto updater, fix legacy packets
Also a few minor bug fixes and improvements Fixes #127
1 parent e84fca6 commit 395d77e

File tree

16 files changed

+840
-329
lines changed

16 files changed

+840
-329
lines changed

ProtocolLib/src/main/java/com/comphenix/protocol/CommandProtocol.java

Lines changed: 79 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
/**
22
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
33
* Copyright (C) 2012 Kristian S. Stangeland
44
*
@@ -14,7 +14,6 @@
1414
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
1515
* 02111-1307 USA
1616
*/
17-
1817
package com.comphenix.protocol;
1918

2019
import java.io.File;
@@ -36,7 +35,9 @@
3635
import com.comphenix.protocol.events.PacketListener;
3736
import com.comphenix.protocol.timing.TimedListenerManager;
3837
import com.comphenix.protocol.timing.TimingReportGenerator;
39-
import com.google.common.io.Closer;
38+
import com.comphenix.protocol.updater.Updater;
39+
import com.comphenix.protocol.updater.Updater.UpdateType;
40+
import com.comphenix.protocol.utility.Closer;
4041

4142
/**
4243
* Handles the "protocol" administration command.
@@ -48,38 +49,56 @@ class CommandProtocol extends CommandBase {
4849
* Name of this command.
4950
*/
5051
public static final String NAME = "protocol";
51-
52+
5253
private Plugin plugin;
54+
private Updater updater;
55+
private ProtocolConfig config;
5356

54-
public CommandProtocol(ErrorReporter reporter, Plugin plugin) {
57+
public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, ProtocolConfig config) {
5558
super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 1);
5659
this.plugin = plugin;
60+
this.updater = updater;
61+
this.config = config;
5762
}
58-
63+
5964
@Override
6065
protected boolean handleCommand(CommandSender sender, String[] args) {
6166
String subCommand = args[0];
6267

6368
// Only return TRUE if we executed the correct command
64-
if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload"))
69+
if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) {
6570
reloadConfiguration(sender);
66-
else if (subCommand.equalsIgnoreCase("timings"))
71+
} else if (subCommand.equalsIgnoreCase("check")) {
72+
checkVersion(sender);
73+
} else if (subCommand.equalsIgnoreCase("update")) {
74+
updateVersion(sender);
75+
} else if (subCommand.equalsIgnoreCase("timings")) {
6776
toggleTimings(sender, args);
68-
else if (subCommand.equalsIgnoreCase("listeners"))
69-
printListeners(sender);
70-
else if (subCommand.equalsIgnoreCase("version"))
77+
} else if (subCommand.equalsIgnoreCase("listeners")) {
78+
printListeners(sender, args);
79+
} else if (subCommand.equalsIgnoreCase("version")) {
7180
printVersion(sender);
72-
else if (subCommand.equalsIgnoreCase("dump"))
81+
} else if (subCommand.equalsIgnoreCase("dump")) {
7382
dump(sender);
74-
else
83+
} else {
7584
return false;
85+
}
86+
7687
return true;
7788
}
78-
89+
90+
public void checkVersion(final CommandSender sender) {
91+
performUpdate(sender, UpdateType.NO_DOWNLOAD);
92+
}
93+
94+
public void updateVersion(final CommandSender sender) {
95+
performUpdate(sender, UpdateType.DEFAULT);
96+
}
97+
7998
// Display every listener on the server
80-
private void printListeners(final CommandSender sender) {
99+
private void printListeners(final CommandSender sender, String[] args) {
81100
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
82-
101+
83102
sender.sendMessage(ChatColor.GOLD + "Packet listeners:");
84103
for (PacketListener listener : manager.getPacketListeners()) {
85104
sender.sendMessage(ChatColor.GOLD + " - " + listener);
@@ -91,15 +110,37 @@ private void printListeners(final CommandSender sender) {
91110
sender.sendMessage(ChatColor.GOLD + " - " + listener);
92111
}
93112
}
113+
114+
private void performUpdate(final CommandSender sender, UpdateType type) {
115+
if (updater.isChecking()) {
116+
sender.sendMessage(ChatColor.RED + "Already checking for an update.");
117+
return;
118+
}
119+
120+
// Perform on an async thread
121+
Runnable notify = new Runnable() {
122+
@Override
123+
public void run() {
124+
if (updater.shouldNotify() || config.isDebug()) {
125+
sender.sendMessage(ChatColor.YELLOW + "[ProtocolLib] " + updater.getResult());
126+
}
94127

128+
updater.removeListener(this);
129+
updateFinished();
130+
}
131+
};
132+
updater.start(type);
133+
updater.addListener(notify);
134+
}
135+
95136
private void toggleTimings(CommandSender sender, String[] args) {
96137
TimedListenerManager manager = TimedListenerManager.getInstance();
97138
boolean state = !manager.isTiming(); // toggle
98-
139+
99140
// Parse the boolean parameter
100141
if (args.length == 2) {
101142
Boolean parsed = parseBoolean(toQueue(args, 2), "start");
102-
143+
103144
if (parsed != null) {
104145
state = parsed;
105146
} else {
@@ -110,7 +151,7 @@ private void toggleTimings(CommandSender sender, String[] args) {
110151
sender.sendMessage(ChatColor.RED + "Too many parameters.");
111152
return;
112153
}
113-
154+
114155
// Now change the state
115156
if (state) {
116157
if (manager.startTiming())
@@ -126,19 +167,35 @@ private void toggleTimings(CommandSender sender, String[] args) {
126167
}
127168
}
128169
}
129-
170+
130171
private void saveTimings(TimedListenerManager manager) {
131172
try {
132173
File destination = new File(plugin.getDataFolder(), "Timings - " + System.currentTimeMillis() + ".txt");
133174
TimingReportGenerator generator = new TimingReportGenerator();
134-
175+
135176
// Print to a text file
136177
generator.saveTo(destination, manager);
137178
manager.clear();
179+
138180
} catch (IOException e) {
139181
reporter.reportMinimal(plugin, "saveTimings()", e);
140182
}
141183
}
184+
185+
/**
186+
* Prevent further automatic updates until the next delay.
187+
*/
188+
public void updateFinished() {
189+
long currentTime = System.currentTimeMillis() / ProtocolLibrary.MILLI_PER_SECOND;
190+
191+
config.setAutoLastTime(currentTime);
192+
config.saveAll();
193+
}
194+
195+
public void reloadConfiguration(CommandSender sender) {
196+
plugin.reloadConfig();
197+
sender.sendMessage(ChatColor.YELLOW + "Reloaded configuration!");
198+
}
142199

143200
private void printVersion(CommandSender sender) {
144201
PluginDescriptionFile desc = plugin.getDescription();
@@ -148,11 +205,6 @@ private void printVersion(CommandSender sender) {
148205
sender.sendMessage(ChatColor.WHITE + "Issues: " + ChatColor.GREEN + "https://github.com/dmulloy2/ProtocolLib/issues");
149206
}
150207

151-
public void reloadConfiguration(CommandSender sender) {
152-
plugin.reloadConfig();
153-
sender.sendMessage(ChatColor.YELLOW + "Reloaded configuration!");
154-
}
155-
156208
private static SimpleDateFormat FILE_FORMAT;
157209
private static SimpleDateFormat TIMESTAMP_FORMAT;
158210

@@ -207,10 +259,7 @@ private void dump(CommandSender sender) {
207259
ProtocolLibrary.getStaticLogger().log(Level.SEVERE, "Failed to create dump:", ex);
208260
sender.sendMessage(ChatColor.RED + "Failed to create dump! Check console!");
209261
} finally {
210-
try {
211-
closer.close();
212-
} catch (IOException ex1) {
213-
}
262+
closer.close();
214263
}
215264
}
216265
}

ProtocolLib/src/main/java/com/comphenix/protocol/PacketType.java

Lines changed: 78 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import java.util.Arrays;
55
import java.util.Collection;
66
import java.util.List;
7+
import java.util.Map;
78
import java.util.UUID;
89
import java.util.concurrent.Callable;
910
import java.util.concurrent.Future;
1011

1112
import org.apache.commons.lang.WordUtils;
1213
import org.bukkit.Bukkit;
1314

15+
import com.comphenix.protocol.PacketTypeLookup.ClassLookup;
1416
import com.comphenix.protocol.events.ConnectionSide;
1517
import com.comphenix.protocol.injector.packet.PacketRegistry;
1618
import com.comphenix.protocol.reflect.ObjectEnum;
@@ -635,7 +637,9 @@ public static boolean hasLegacy(int packetId) {
635637
* @param packetId - the packet ID.
636638
* @return The corresponding packet type.
637639
* @throws IllegalArgumentException If the current packet could not be found.
640+
* @deprecated IDs are no longer reliable
638641
*/
642+
@Deprecated
639643
public static PacketType findCurrent(Protocol protocol, Sender sender, int packetId) {
640644
PacketType type = getLookup().getFromCurrent(protocol, sender, packetId);
641645

@@ -645,13 +649,34 @@ public static PacketType findCurrent(Protocol protocol, Sender sender, int packe
645649
"(Protocol: " + protocol + ", Sender: " + sender + ")");
646650
}
647651

652+
public static PacketType findCurrent(Protocol protocol, Sender sender, String name) {
653+
name = format(protocol, sender, name);
654+
PacketType type = getLookup().getFromCurrent(protocol, sender, name);
655+
656+
if (type != null) {
657+
return type;
658+
} else {
659+
throw new IllegalArgumentException("Cannot find packet " + name +
660+
"(Protocol: " + protocol + ", Sender: " + sender + ")");
661+
}
662+
}
663+
664+
private static String format(Protocol protocol, Sender sender, String name) {
665+
if (name.contains("Packet"))
666+
return name;
667+
668+
return String.format("Packet%s%s%s", protocol.getPacketName(), sender.getPacketName(), name);
669+
}
670+
648671
/**
649672
* Determine if the given packet exists.
650673
* @param protocol - the protocol.
651674
* @param sender - the sender.
652675
* @param packetId - the packet ID.
653676
* @return TRUE if it exists, FALSE otherwise.
677+
* @deprecated IDs are no longer reliable
654678
*/
679+
@Deprecated
655680
public static boolean hasCurrent(Protocol protocol, Sender sender, int packetId) {
656681
return getLookup().getFromCurrent(protocol, sender, packetId) != null;
657682
}
@@ -680,7 +705,7 @@ public static PacketType fromLegacy(int id, Sender sender) {
680705
}
681706

682707
/**
683-
* Retrieve a packet type from a protocol, sender and packet ID.
708+
* Retrieve a packet type from a protocol, sender and packet ID, for pre-1.8.
684709
* <p>
685710
* The packet will automatically be registered if its missing.
686711
* @param protocol - the current protocol.
@@ -689,21 +714,59 @@ public static PacketType fromLegacy(int id, Sender sender) {
689714
* @param packetClass - the packet class
690715
* @return The corresponding packet type.
691716
*/
717+
public static PacketType fromID(Protocol protocol, Sender sender, int packetId, Class<?> packetClass) {
718+
PacketType type = getLookup().getFromCurrent(protocol, sender, packetId);
719+
720+
if (type == null) {
721+
type = new PacketType(protocol, sender, packetId, -1, PROTOCOL_VERSION, packetClass.getName());
722+
type.dynamic = true;
723+
724+
// Many may be scheduled, but only the first will be executed
725+
scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString());
726+
}
727+
728+
return type;
729+
}
730+
731+
/**
732+
* Retrieve a packet type from a protocol, sender, ID, and class for 1.8+
733+
* <p>
734+
* The packet will automatically be registered if its missing.
735+
* @param protocol - the current protocol.
736+
* @param sender - the sender.
737+
* @param packetId - the packet ID. Can be UNKNOWN_PACKET.
738+
* @param packetClass - the packet class.
739+
* @return The corresponding packet type.
740+
*/
692741
public static PacketType fromCurrent(Protocol protocol, Sender sender, int packetId, Class<?> packetClass) {
742+
ClassLookup lookup = getLookup().getClassLookup();
743+
Map<String, PacketType> map = lookup.getMap(protocol, sender);
744+
745+
// Check the map first
693746
String className = packetClass.getSimpleName();
694-
for (PacketType type : PacketType.values()) {
695-
for (String name : type.classNames) {
696-
if (className.equals(name)) {
697-
return type;
747+
PacketType type = map.get(className);
748+
if (type == null) {
749+
// Then check any aliases
750+
for (PacketType check : map.values()) {
751+
String[] aliases = check.getClassNames();
752+
if (aliases.length > 1) {
753+
for (String alias : aliases) {
754+
if (alias.equals(className)) {
755+
// We have a match!
756+
type = check;
757+
}
758+
}
698759
}
699760
}
700-
}
701761

702-
PacketType type = new PacketType(protocol, sender, packetId, -1, PROTOCOL_VERSION, className);
703-
type.dynamic = true;
762+
// Guess we don't support this packet :/
763+
type = new PacketType(protocol, sender, packetId, -1, PROTOCOL_VERSION, className);
764+
type.dynamic = true;
765+
766+
// Many may be scheduled, but only the first will be executed
767+
scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString());
768+
}
704769

705-
// Many may be scheduled, but only the first will be executed
706-
scheduleRegister(type, "Dynamic-" + UUID.randomUUID().toString());
707770
return type;
708771
}
709772

@@ -826,7 +889,7 @@ public PacketType(Protocol protocol, Sender sender, int currentId, int legacyId,
826889

827890
this.classNames = new String[names.length];
828891
for (int i = 0; i < classNames.length; i++) {
829-
classNames[i] = String.format("Packet%s%s%s", protocol.getPacketName(), sender.getPacketName(), names[i]);
892+
classNames[i] = format(protocol, sender, names[i]);
830893
}
831894
}
832895

@@ -894,6 +957,10 @@ public int getCurrentId() {
894957
return currentId;
895958
}
896959

960+
public String[] getClassNames() {
961+
return classNames;
962+
}
963+
897964
/**
898965
* Retrieve the equivalent packet class.
899966
* @return The packet class, or NULL if not found.

0 commit comments

Comments
 (0)