Skip to content

Commit

Permalink
Fixed bugs and improved performance
Browse files Browse the repository at this point in the history
- Improved the performance and the concurrent ordering of the packet system.
- Added new WrappedOutTabComplete wrapper.
- Implemented a feature where you can make your own hardcoded command with SpigotCommand.
- Added no CommandManager#getMap to get an already grabbed SimpleCommandMap class for use.
  • Loading branch information
funkemunky committed Aug 3, 2019
1 parent cc77102 commit 8beaf4b
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 82 deletions.
4 changes: 2 additions & 2 deletions AtlasParent/Atlas/buildNumber.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#maven.buildNumber.plugin properties file
#Fri Aug 02 14:02:30 EDT 2019
buildNumber=244
#Sat Aug 03 14:52:05 EDT 2019
buildNumber=255
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,27 @@ public FunkeCommand(JavaPlugin plugin, String name, String display, String descr
this.addArguments();
}

public FunkeCommand(JavaPlugin plugin, String name, String display, String permission, String description, boolean helpPage) {
public FunkeCommand(JavaPlugin plugin, String name, String display, String description, String permission, boolean registerLater) {
this.name = name;
this.display = display;
this.permission = permission;
this.description = description;

commandMessages = new CommandMessages("No permission.", "Invalid arguments. Please check the help page for more information.", "You must be a player to use this feature", "Only console can use this feature.", Color.Gray, Color.Yellow, Color.Gold, Color.Red, Color.White, Color.Green);

this.arguments = new ArrayList<>();
instance = this;
helpPage = true;

if(!registerLater) {
plugin.getCommand(name).setExecutor(this);
plugin.getCommand(name).setTabCompleter(this);
}

this.addArguments();
}

public FunkeCommand(JavaPlugin plugin, String name, String display, String permission, String description, boolean helpPage, boolean registerLater) {
this.name = name;
this.display = display;
this.permission = permission;
Expand All @@ -57,8 +77,10 @@ public FunkeCommand(JavaPlugin plugin, String name, String display, String permi

this.arguments = new ArrayList<>();
instance = this;
plugin.getCommand(name).setExecutor(this);
plugin.getCommand(name).setTabCompleter(this);
if(!registerLater) {
plugin.getCommand(name).setExecutor(this);
plugin.getCommand(name).setTabCompleter(this);
}

this.addArguments();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
public class CommandManager implements CommandExecutor {
private Map<String, CommandRegister> commands = new ConcurrentHashMap<>();
private Plugin plugin;
@Getter
private SimpleCommandMap map;
private List<SpigotCommand> registered = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,48 +11,55 @@

public class SpigotCommand extends org.bukkit.command.Command {

private final Plugin owningPlugin;
private Plugin owningPlugin;
protected SpigotCompleter completer;
private CommandExecutor executor;
private boolean notAno = false;

/**
* A slimmed down PluginCommand
*
* @param name
* @param owner
*/
protected SpigotCommand(String label, CommandExecutor executor, Plugin owner) {

public SpigotCommand(String label, CommandExecutor executor, Plugin owner) {
super(label);
this.executor = executor;
this.owningPlugin = owner;
this.usageMessage = "";
}

public SpigotCommand(String label, CommandExecutor executor, Plugin owner, boolean notAno) {
super(label);
this.executor = executor;
this.owningPlugin = owner;
this.usageMessage = "";
this.notAno = notAno;
}

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
boolean success = false;

if (!owningPlugin.isEnabled()) {
return false;
}
if(notAno) {
return executor.onCommand(sender, this, commandLabel, args);
} else {
boolean success = false;

if (!testPermission(sender)) {
return true;
}
if (!testPermission(sender)) {
return true;
}

try {
success = executor.onCommand(sender, this, commandLabel, args);
} catch (Throwable ex) {
throw new CommandException("Unhandled exception executing ancmd '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
}
try {
success = executor.onCommand(sender, this, commandLabel, args);
} catch (Throwable ex) {
throw new CommandException("Unhandled exception executing ancmd '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
}

if (!success && usageMessage.length() > 0) {
for (String line : usageMessage.replace("<ancmd>", commandLabel).split("\n")) {
sender.sendMessage(line);
if (!success && usageMessage.length() > 0) {
for (String line : usageMessage.replace("<ancmd>", commandLabel).split("\n")) {
sender.sendMessage(line);
}
}
}

return success;
return success;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,18 @@

import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.events.exceptions.ListenParamaterException;
import javafx.collections.transformation.SortedList;
import lombok.Getter;
import org.bukkit.plugin.Plugin;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;

@Getter
public class EventManager {
private final SortedSet<ListenerMethod> listenerMethods = new ConcurrentSkipListSet<>(Comparator.comparing(method -> method.getListenerPriority().getPriority(), Comparator.reverseOrder()));
private final List<ListenerMethod> listenerMethods = new ArrayList<>();
private boolean paused = false;

public void registerListener(Method method, AtlasListener listener, Plugin plugin) throws ListenParamaterException {
Expand All @@ -28,6 +27,7 @@ public void registerListener(Method method, AtlasListener listener, Plugin plugi
}

listenerMethods.add(lm);
listenerMethods.sort(Comparator.comparing(mth -> mth.getListenerPriority().getPriority(), Comparator.reverseOrder()));
} else {
throw new ListenParamaterException("Method " + method.getDeclaringClass().getName() + "#" + method.getName() + "'s paramater: " + method.getParameterTypes()[0].getName() + " is not an instanceof " + AtlasEvent.class.getSimpleName() + "!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.bukkit.event.Listener;
import org.bukkit.event.server.PluginDisableEvent;

@Init
//@Init
public class PluginShutdownListeners implements Listener {

@EventHandler(priority = EventPriority.HIGHEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ public static Object construct(String packet, Object... args) {
return null;
}

public static Object construct(String packet, Object arg) {
try {
Class<?> c = constructors.get(packet);
if (c == null) {
c = Reflection.getMinecraftClass(packet);
constructors.put(packet, c);
}
Object p = c.newInstance();
Field[] fields = c.getDeclaredFields();

if(arg != null) {
fields[0].setAccessible(true);
fields[0].set(p, arg);
}
return p;
} catch (Exception e) {
System.out.println("The plugin cannot work as protocol incompatibilities were detected... Disabling...");
e.printStackTrace();
}
return null;
}

public static Object construct(Object p, String packet, Object... args) {
try {
Class<?> c = constructors.get(packet);
Expand Down Expand Up @@ -132,6 +154,14 @@ public void setPacket(String packet, Object... args) {
this.object = construct(packet, args);
}

public void setPacketArg(String packet, Object arg) {
this.object = construct(packet, arg);
}

public void setPacket(String packet, Object arg) {
setPacketArg(packet, arg);
}

public <T> T fetch(FieldAccessor<T> field) {
return field.get(object);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package cc.funkemunky.api.tinyprotocol.api.packets.channelhandler;

import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.tinyprotocol.api.packets.reflections.Reflections;
import cc.funkemunky.api.tinyprotocol.reflection.FieldAccessor;
import cc.funkemunky.api.tinyprotocol.reflection.Reflection;
Expand Down Expand Up @@ -53,14 +54,14 @@ private static class ChannelHandler extends net.minecraft.util.io.netty.channel.
}

@Override public void write(net.minecraft.util.io.netty.channel.ChannelHandlerContext ctx, Object msg, net.minecraft.util.io.netty.channel.ChannelPromise promise) throws Exception {
Object packet = channelHandlerAbstract.run(this.player, msg);
Object packet = Atlas.getInstance().getTinyProtocolHandler().onPacketInAsync(player, msg);
if (packet != null) {
super.write(ctx, packet, promise);
}
}

@Override public void channelRead(net.minecraft.util.io.netty.channel.ChannelHandlerContext ctx, Object msg) throws Exception {
Object packet = channelHandlerAbstract.run(this.player, msg);
Object packet = Atlas.getInstance().getTinyProtocolHandler().onPacketOutAsync(player, msg);
if (packet != null) {
super.channelRead(ctx, packet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
package cc.funkemunky.api.tinyprotocol.api.packets.channelhandler;

import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.tinyprotocol.api.packets.reflections.Reflections;
import cc.funkemunky.api.tinyprotocol.reflection.FieldAccessor;
import cc.funkemunky.api.tinyprotocol.reflection.Reflection;
Expand Down Expand Up @@ -56,14 +57,14 @@ private static class ChannelHandler extends io.netty.channel.ChannelDuplexHandle
}

@Override public void write(io.netty.channel.ChannelHandlerContext ctx, Object msg, io.netty.channel.ChannelPromise promise) throws Exception {
Object packet = channelHandlerAbstract.run(this.player, msg);
Object packet = Atlas.getInstance().getTinyProtocolHandler().onPacketOutAsync(player, msg);
if (packet != null) {
super.write(ctx, packet, promise);
}
}

@Override public void channelRead(io.netty.channel.ChannelHandlerContext ctx, Object msg) throws Exception {
Object packet = channelHandlerAbstract.run(this.player, msg);
Object packet = Atlas.getInstance().getTinyProtocolHandler().onPacketInAsync(player, msg);
if (packet != null) {
super.channelRead(ctx, packet);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,9 @@
*/
package cc.funkemunky.api.tinyprotocol.api.packets.channelhandler;

import cc.funkemunky.api.Atlas;
import cc.funkemunky.api.events.impl.PacketReceiveEvent;
import cc.funkemunky.api.events.impl.PacketSendEvent;
import cc.funkemunky.api.tinyprotocol.api.Packet;
import cc.funkemunky.api.tinyprotocol.api.packets.reflections.Reflections;
import cc.funkemunky.api.tinyprotocol.api.packets.reflections.types.WrappedField;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

Expand All @@ -38,38 +29,6 @@ public abstract class ChannelHandlerAbstract {
this.playerKey = "atlas_player_handler";
}

public Object run(Player player, Object packet) {
if (!Atlas.getInstance().isDone()) {
Plugin plugin = Bukkit.getPluginManager().getPlugin("Atlas");
if (plugin != null && plugin.isEnabled()) {
Object channelInjector = Reflections.getClass(plugin.getClass()).getMethod("getChannelInjector").invoke(plugin);
Reflections.getClass(channelInjector.getClass()).getMethod("addChannel", Player.class).invoke(channelInjector, player);
}
return true;
}
if (packet != null && player != null && player.isOnline()) {
String name = packet.getClass().getSimpleName().replaceAll("PacketPlayInUseItem", "PacketPlayInBlockPlace")
.replaceAll(Packet.Client.LEGACY_LOOK, Packet.Client.LOOK)
.replaceAll(Packet.Client.LEGACY_POSITION, Packet.Client.POSITION)
.replaceAll(Packet.Client.LEGACY_POSITION_LOOK, Packet.Client.POSITION_LOOK);;

if(name.contains("PacketPlayIn")) {
PacketReceiveEvent event = new PacketReceiveEvent(player, packet, name);

Atlas.getInstance().getEventManager().callEvent(event);

return event.isCancelled() ? null : event.getPacket();
} else if(name.contains("PacketPlayOut")) {
PacketSendEvent event = new PacketSendEvent(player, packet, name);

Atlas.getInstance().getEventManager().callEvent(event);

return event.isCancelled() ? null : event.getPacket();
}
}
return packet;
}

public abstract void addChannel(Player player);

public abstract void removeChannel(Player player);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public WrappedOutTabComplete(Object object, Player player) {

//For everything below 1.13. There will be 1.13+ support for this soon.
public WrappedOutTabComplete(String[] result) {
setPacket(packet, result);
setPacketArg(packet, result);
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions AtlasParent/AtlasBungee/buildNumber.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#maven.buildNumber.plugin properties file
#Fri Aug 02 14:02:30 EDT 2019
buildNumber=151
#Sat Aug 03 14:52:05 EDT 2019
buildNumber=162
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Generated by Maven
#Fri Aug 02 14:02:33 EDT 2019
#Sat Aug 03 14:52:08 EDT 2019
version=1.4.7
groupId=cc.funkemunky.utils
artifactId=AtlasBungee

0 comments on commit 8beaf4b

Please sign in to comment.