Skip to content

Cleanup4 #2482

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
d15fa97
Remove ProtocolCommand enum.
Photon-GitHub Jul 14, 2023
1709b62
Lazy init of filteredReporter, use ImmutableSet.
Photon-GitHub Jul 14, 2023
1fe43f7
Rename parameter to not be the "record" keyword.
Photon-GitHub Jul 14, 2023
1abb0d0
Simple cleanup.
Photon-GitHub Jul 14, 2023
5b702ce
Faster getFirst lookup.
Photon-GitHub Jul 14, 2023
3ea61b0
Simple cleanup.
Photon-GitHub Jul 14, 2023
a30e1dd
switch case instead of equalsIgnoreCase chain.
Photon-GitHub Jul 14, 2023
994ec37
Simple cleanup.
Photon-GitHub Jul 14, 2023
1525536
Simple cleanup.
Photon-GitHub Jul 14, 2023
58f311e
Use streams for mapping.
Photon-GitHub Jul 14, 2023
0d6ac32
Reduce nesting.
Photon-GitHub Jul 14, 2023
edd2e42
Faster cache lookup.
Photon-GitHub Jul 14, 2023
ba73098
Reduce nesting.
Photon-GitHub Jul 14, 2023
8acbede
Faster mapping with streams.
Photon-GitHub Jul 14, 2023
f5f2b5c
Simple cleanup.
Photon-GitHub Jul 14, 2023
384e61d
Faster temporaryPlayer method lookup.
Photon-GitHub Jul 14, 2023
129aa5d
Make final.
Photon-GitHub Jul 14, 2023
c4375b8
Fast range parsing.
Photon-GitHub Jul 14, 2023
39174d2
Default command "?"
Photon-GitHub Jul 14, 2023
3230c5a
Bugfix.
Photon-GitHub Jul 14, 2023
9f787c6
Actually limit the range.
Photon-GitHub Jul 14, 2023
73aa45f
Improve RangeParser.
Photon-GitHub Jul 15, 2023
8a98382
Improve documentation.
Photon-GitHub Jul 15, 2023
73d29ca
Make enum field final.
Photon-GitHub Jul 15, 2023
96fd3f0
Simple cleanup.
Photon-GitHub Jul 15, 2023
e7cf2b5
Simple cleanup.
Photon-GitHub Jul 15, 2023
1b860f7
Simplify return value.
Photon-GitHub Jul 15, 2023
b234c6d
Make some values final.
Photon-GitHub Jul 15, 2023
3a30fe9
Use Java's BiFunction.
Photon-GitHub Jul 15, 2023
700012c
Simple cleanup.
Photon-GitHub Jul 15, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions src/main/java/com/comphenix/protocol/CommandPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,7 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.WeakHashMap;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -76,7 +68,7 @@ private enum SubCommand {
private final PacketTypeParser typeParser = new PacketTypeParser();

// Paged message
private final Map<CommandSender, List<String>> pagedMessage = new WeakHashMap<CommandSender, List<String>>();
private final Map<CommandSender, List<String>> pagedMessage = new WeakHashMap<>();

// Current registered packet types
private final PacketTypeSet packetTypes = new PacketTypeSet();
Expand Down
78 changes: 42 additions & 36 deletions src/main/java/com/comphenix/protocol/CommandProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@
*/
package com.comphenix.protocol;

import com.comphenix.protocol.error.DetailedErrorReporter;
import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.timing.TimedListenerManager;
import com.comphenix.protocol.timing.TimingReportGenerator;
import com.comphenix.protocol.updater.Updater;
import com.comphenix.protocol.updater.Updater.UpdateType;
import com.comphenix.protocol.utility.Closer;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
Expand All @@ -27,20 +40,6 @@
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;

import com.comphenix.protocol.error.DetailedErrorReporter;
import com.comphenix.protocol.error.ErrorReporter;
import com.comphenix.protocol.events.PacketListener;
import com.comphenix.protocol.timing.TimedListenerManager;
import com.comphenix.protocol.timing.TimingReportGenerator;
import com.comphenix.protocol.updater.Updater;
import com.comphenix.protocol.updater.Updater.UpdateType;
import com.comphenix.protocol.utility.Closer;

/**
* Handles the "protocol" administration command.
*
Expand All @@ -52,9 +51,9 @@ class CommandProtocol extends CommandBase {
*/
public static final String NAME = "protocol";

private Plugin plugin;
private Updater updater;
private ProtocolConfig config;
private final Plugin plugin;
private final Updater updater;
private final ProtocolConfig config;

public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, ProtocolConfig config) {
super(reporter, CommandBase.PERMISSION_ADMIN, NAME, 1);
Expand All @@ -65,27 +64,34 @@ public CommandProtocol(ErrorReporter reporter, Plugin plugin, Updater updater, P

@Override
protected boolean handleCommand(CommandSender sender, String[] args) {
String subCommand = args[0];

// Only return TRUE if we executed the correct command
if (subCommand.equalsIgnoreCase("config") || subCommand.equalsIgnoreCase("reload")) {
reloadConfiguration(sender);
} else if (subCommand.equalsIgnoreCase("check")) {
checkVersion(sender, true);
} else if (subCommand.equalsIgnoreCase("update")) {
updateVersion(sender, true);
} else if (subCommand.equalsIgnoreCase("timings")) {
toggleTimings(sender, args);
} else if (subCommand.equalsIgnoreCase("listeners")) {
printListeners(sender);
} else if (subCommand.equalsIgnoreCase("version")) {
printVersion(sender);
} else if (subCommand.equalsIgnoreCase("dump")) {
dump(sender);
} else {
return false;
switch (args[0].toLowerCase())
{
case "config":
case "reload":
reloadConfiguration(sender);
break;
case "check":
checkVersion(sender, true);
break;
case "update":
updateVersion(sender, true);
break;
case "timings":
toggleTimings(sender, args);
break;
case "listeners":
printListeners(sender);
break;
case "version":
printVersion(sender);
break;
case "dump":
dump(sender);
break;
default:
return false;
}

return true;
}

Expand Down
17 changes: 6 additions & 11 deletions src/main/java/com/comphenix/protocol/MultipleLinesPrompt.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.comphenix.protocol;

import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationCanceller;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ExactMatchConversationCanceller;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;
import org.bukkit.conversations.*;

/**
* Represents a conversation prompt that accepts a list of lines.
Expand All @@ -17,9 +12,9 @@ class MultipleLinesPrompt extends StringPrompt {
* Represents a canceller that determines if the multiple lines prompt is finished.
* @author Kristian
*/
public static interface MultipleConversationCanceller extends ConversationCanceller {
public interface MultipleConversationCanceller extends ConversationCanceller {
@Override
public boolean cancelBasedOnInput(ConversationContext context, String currentLine);
boolean cancelBasedOnInput(ConversationContext context, String currentLine);

/**
* Determine if the current prompt is done based on the context, last
Expand All @@ -31,7 +26,7 @@ public static interface MultipleConversationCanceller extends ConversationCancel
* @param lineCount - number of lines.
* @return TRUE if we are done, FALSE otherwise.
*/
public boolean cancelBasedOnInput(ConversationContext context, String currentLine,
boolean cancelBasedOnInput(ConversationContext context, String currentLine,
StringBuilder lines, int lineCount);
}

Expand All @@ -40,7 +35,7 @@ public boolean cancelBasedOnInput(ConversationContext context, String currentLin
* @author Kristian
*/
private static class MultipleWrapper implements MultipleConversationCanceller {
private ConversationCanceller canceller;
private final ConversationCanceller canceller;

public MultipleWrapper(ConversationCanceller canceller) {
this.canceller = canceller;
Expand Down Expand Up @@ -142,7 +137,7 @@ public Prompt acceptInput(ConversationContext context, String in) {
// Save the last line as well
context.setSessionData(KEY_LAST, in);
context.setSessionData(KEY_LINES, ++count);
result.append(in + "\n");
result.append(in).append("\n");

// And we're done
if (endMarker.cancelBasedOnInput(context, in, result, count))
Expand Down
43 changes: 21 additions & 22 deletions src/main/java/com/comphenix/protocol/PacketTypeLookup.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.comphenix.protocol;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.comphenix.protocol.PacketType.Protocol;
import com.comphenix.protocol.PacketType.Sender;
import com.comphenix.protocol.collections.IntegerMap;
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Retrieve a packet type based on its version and ID, optionally with protocol and sender too.
* @author Kristian
Expand All @@ -27,7 +27,7 @@ public static class ProtocolSenderLookup {
public final IntegerMap<PacketType> STATUS_SERVER = new IntegerMap<>();
public final IntegerMap<PacketType> LOGIN_CLIENT = new IntegerMap<>();
public final IntegerMap<PacketType> LOGIN_SERVER = new IntegerMap<>();

/**
* Retrieve the correct integer map for a specific protocol and sender.
* @param protocol - the protocol.
Expand All @@ -36,7 +36,7 @@ public static class ProtocolSenderLookup {
*/
public IntegerMap<PacketType> getMap(Protocol protocol, Sender sender) {
switch (protocol) {
case HANDSHAKING:
case HANDSHAKING:
return sender == Sender.CLIENT ? HANDSHAKE_CLIENT : HANDSHAKE_SERVER;
case PLAY:
return sender == Sender.CLIENT ? GAME_CLIENT : GAME_SERVER;
Expand All @@ -60,7 +60,7 @@ public static class ClassLookup {
public final Map<String, PacketType> STATUS_SERVER = new ConcurrentHashMap<>();
public final Map<String, PacketType> LOGIN_CLIENT = new ConcurrentHashMap<>();
public final Map<String, PacketType> LOGIN_SERVER = new ConcurrentHashMap<>();

/**
* Retrieve the correct integer map for a specific protocol and sender.
* @param protocol - the protocol.
Expand All @@ -69,7 +69,7 @@ public static class ClassLookup {
*/
public Map<String, PacketType> getMap(Protocol protocol, Sender sender) {
switch (protocol) {
case HANDSHAKING:
case HANDSHAKING:
return sender == Sender.CLIENT ? HANDSHAKE_CLIENT : HANDSHAKE_SERVER;
case PLAY:
return sender == Sender.CLIENT ? GAME_CLIENT : GAME_SERVER;
Expand All @@ -82,12 +82,12 @@ public Map<String, PacketType> getMap(Protocol protocol, Sender sender) {
}
}
}

// Packet IDs from 1.6.4 and below
private final IntegerMap<PacketType> legacyLookup = new IntegerMap<>();
private final IntegerMap<PacketType> serverLookup = new IntegerMap<>();
private final IntegerMap<PacketType> clientLookup = new IntegerMap<>();

// Packets for 1.7.2
private final ProtocolSenderLookup idLookup = new ProtocolSenderLookup();

Expand All @@ -103,7 +103,7 @@ public Map<String, PacketType> getMap(Protocol protocol, Sender sender) {
*/
public PacketTypeLookup addPacketTypes(Iterable<? extends PacketType> types) {
Preconditions.checkNotNull(types, "types cannot be NULL");

for (PacketType type : types) {
// Skip unknown current packets
if (type.getCurrentId() != PacketType.UNKNOWN_PACKET) {
Expand All @@ -114,7 +114,7 @@ public PacketTypeLookup addPacketTypes(Iterable<? extends PacketType> types) {
}
return this;
}

/**
* Retrieve a packet type from a legacy (1.6.4 and below) packet ID.
* @param packetId - the legacy packet ID.
Expand All @@ -123,7 +123,7 @@ public PacketTypeLookup addPacketTypes(Iterable<? extends PacketType> types) {
public PacketType getFromLegacy(int packetId) {
return legacyLookup.get(packetId);
}

/**
* Retrieve an unmodifiable view of all the packet types with this name.
* @param name - the name.
Expand All @@ -132,28 +132,27 @@ public PacketType getFromLegacy(int packetId) {
public Collection<PacketType> getFromName(String name) {
return Collections.unmodifiableCollection(nameLookup.get(name));
}

/**
* Retrieve a packet type from a legacy (1.6.4 and below) packet ID.
* @param packetId - the legacy packet ID.
* @param preference - which packet type to look for first.
* @return The corresponding packet type, or NULL if not found.
*/
public PacketType getFromLegacy(int packetId, Sender preference) {
public PacketType getFromLegacy(int packetId, Sender preference) {
if (preference == Sender.CLIENT)
return getFirst(packetId, clientLookup, serverLookup);
else
return getFirst(packetId, serverLookup, clientLookup);
}

// Helper method for looking up in two sets
private <T> T getFirst(int packetId, IntegerMap<T> first, IntegerMap<T> second) {
if (first.containsKey(packetId))
return first.get(packetId);
else
return second.get(packetId);
T value = first.get(packetId);
if (value == null) value = second.get(packetId);
return value;
}

/**
* Retrieve a packet type from a protocol, sender and packet ID.
* @param protocol - the current protocol.
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/com/comphenix/protocol/ProtocolConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class ProtocolConfig {
// Defaults
private static final long DEFAULT_UPDATER_DELAY = 43200;

private Plugin plugin;
private final Plugin plugin;
private Configuration config;
private boolean loadingSections;

Expand Down Expand Up @@ -145,7 +145,7 @@ private File getLastUpdateFile() {
/**
* Load data sections.
*
* @param copyDefaults - whether or not to copy configuration defaults.
* @param copyDefaults - whether to copy configuration defaults.
*/
private void loadSections(boolean copyDefaults) {
if (config != null) {
Expand Down Expand Up @@ -231,7 +231,7 @@ public boolean isChatWarnings() {
}

/**
* Retrieve whether or not ProtocolLib should determine if a new version has been released.
* Retrieve whether ProtocolLib should determine if a new version has been released.
*
* @return TRUE if it should do this automatically, FALSE otherwise.
*/
Expand All @@ -240,7 +240,7 @@ public boolean isAutoNotify() {
}

/**
* Retrieve whether or not ProtocolLib should automatically download the new version.
* Retrieve whether ProtocolLib should automatically download the new version.
*
* @return TRUE if it should, FALSE otherwise.
*/
Expand All @@ -249,7 +249,7 @@ public boolean isAutoDownload() {
}

/**
* Determine whether or not debug mode is enabled.
* Determine whether debug mode is enabled.
* <p>
* This grants access to the filter command.
*
Expand All @@ -260,7 +260,7 @@ public boolean isDebug() {
}

/**
* Set whether or not debug mode is enabled.
* Set whether debug mode is enabled.
*
* @param value - TRUE if it is enabled, FALSE otherwise.
*/
Expand All @@ -275,7 +275,7 @@ public void setDebug(boolean value) {
* @return Every suppressed report type.
*/
public ImmutableList<String> getSuppressedReports() {
return ImmutableList.copyOf(getGlobalValue(SUPPRESSED_REPORTS, new ArrayList<String>()));
return ImmutableList.copyOf(getGlobalValue(SUPPRESSED_REPORTS, new ArrayList<>()));
}

/**
Expand Down
Loading