Skip to content
This repository was archived by the owner on Feb 12, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
39 changes: 24 additions & 15 deletions src/main/java/me/heyimblake/proxyparty/ProxyParty.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import net.md_5.bungee.api.plugin.Plugin;

import java.io.*;
import java.lang.reflect.Type;
import java.util.List;
import java.util.logging.Level;

Expand All @@ -34,9 +33,13 @@
* @since 10/21/2016
*/
public final class ProxyParty extends Plugin {

private static ProxyParty instance;

private boolean loggingEnabled = true;
private String logFileName = "log.json";

private final String logFileName = "log.json";

private File logFile;
private ConfigManager configManager;

Expand Down Expand Up @@ -78,18 +81,22 @@ private void registerListeners() {
}

private void setupLogFile() {
if (!getDataFolder().exists())
getDataFolder().mkdir();
if (!getDataFolder().exists()) getDataFolder().mkdir();

logFile = new File(getDataFolder().getPath(), logFileName);

if (!logFile.exists()) {
try {
logFile.createNewFile();
if (!logFile.createNewFile()) return;

try (InputStream is = getResourceAsStream(logFileName);
OutputStream os = new FileOutputStream(logFile)) {
ByteStreams.copy(is, os);

os.close();
is.close();
getLogger().log(Level.INFO, logFileName + " was created with no issues!");

this.getLogger().log(Level.INFO, logFileName + " was created with no issues!");
}
} catch (IOException e) {
e.printStackTrace();
Expand All @@ -98,16 +105,18 @@ private void setupLogFile() {
}
return;
}
if (!loggingEnabled)
return;
getLogger().log(Level.INFO, "Detected " + logFileName + ".");

if (!loggingEnabled) return;

this.getLogger().log(Level.INFO, "Detected " + logFileName + ".");

try {
Gson gson = new Gson();
List<ActionLogEntry> entires;
Type type = new TypeToken<List<ActionLogEntry>>() {
}.getType();
entires = gson.fromJson(new FileReader(logFile), type);
entires.forEach(actionLogEntry -> ActionLogEntry.savedEntries.add(actionLogEntry));

List<ActionLogEntry> entires = gson.fromJson(new FileReader(logFile), new TypeToken<List<ActionLogEntry>>() {}.getType());

ActionLogEntry.savedEntries.addAll(entires);

getLogger().log(Level.INFO, "Imported old actions from " + logFileName + ".");
} catch (FileNotFoundException e) {
//Not really possible as it's created/verified above, but oh well, here's a catch block!
Expand All @@ -129,4 +138,4 @@ public String getLogFileName() {
public ConfigManager getConfigManager() {
return configManager;
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface PartySubCommandExecutor {
public @interface PartyAnnotationCommand {
/**
* The name of the subcommand.
*
* @return subcommand name
*/
String subCommand();
String name();

/**
* The usage of the subcommand.
Expand Down Expand Up @@ -66,4 +66,4 @@
* @return true if sender must be in a party, false otherwise
*/
boolean mustBeInParty() default true;
}
}
168 changes: 83 additions & 85 deletions src/main/java/me/heyimblake/proxyparty/commands/PartyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.plugin.Command;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
* Copyright (C) 2017 heyimblake
Expand All @@ -35,24 +35,24 @@
*/
public class PartyCommand extends Command {

private static Map<String, Class<? extends AnnotatedPartySubCommand>> subCommandClasses = new HashMap<>();
private final Map<String, PartySubCommand> commands = new HashMap<>();

public PartyCommand() {
super("party", null);
registerSubCommand(InviteSubCommand.class);
registerSubCommand(AcceptSubCommand.class);
registerSubCommand(DenySubCommand.class);
registerSubCommand(FindSubCommand.class);
registerSubCommand(ListSubCommand.class);
registerSubCommand(InvitedSubCommand.class);
registerSubCommand(RetractSubCommand.class);
registerSubCommand(KickSubCommand.class);
registerSubCommand(ChatSubCommand.class);
registerSubCommand(WarpSubCommand.class);
registerSubCommand(LeaveSubCommand.class);
registerSubCommand(PromoteSubCommand.class);
registerSubCommand(DisbandSubCommand.class);
registerSubCommand(ToggleSubCommand.class);
registerSubCommand(new InviteSubCommand());
registerSubCommand(new AcceptSubCommand());
registerSubCommand(new ChatSubCommand());
registerSubCommand(new DenySubCommand());
registerSubCommand(new DisbandSubCommand());
registerSubCommand(new FindSubCommand());
registerSubCommand(new InvitedSubCommand());
registerSubCommand(new KickSubCommand());
registerSubCommand(new LeaveSubCommand());
registerSubCommand(new ListSubCommand());
registerSubCommand(new PromoteSubCommand());
registerSubCommand(new RetractSubCommand());
registerSubCommand(new ToggleSubCommand());
registerSubCommand(new WarpSubCommand());
}

@Override
Expand All @@ -62,110 +62,108 @@ public void execute(CommandSender sender, String[] args) {
return;
}
ProxiedPlayer player = ((ProxiedPlayer) sender);
if (args.length > 0) {
PartySubCommandHandler handler = new PartySubCommandHandler(sender, Arrays.copyOfRange(args, 1, args.length));
String subCMDInput = args[0];
for (String key : subCommandClasses.keySet()) {
Class<? extends AnnotatedPartySubCommand> clazz = subCommandClasses.get(key);
if (key.equalsIgnoreCase(subCMDInput)) {
if (PartyRole.getRoleOf(player) == PartyRole.PARTICIPANT && getSubCommandClassAnnotation(clazz).leaderExclusive()) {
player.sendMessage(Constants.TAG, new ComponentBuilder("You must be the party leader in order to do this.").color(ChatColor.RED).create()[0]);
return;
}
PartySubCommandExecutor partySubCommandExecutor = getSubCommandClassAnnotation(clazz);
if (handler.getArguments().length == 0 && getSubCommandClassAnnotation(clazz).requiresArgumentCompletion()) {
player.sendMessage(Constants.TAG, new TextComponent("Usage: "),
new ComponentBuilder(partySubCommandExecutor.syntax()).color(ChatColor.AQUA)
.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/party " + partySubCommandExecutor.subCommand() + " "))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(ChatColor.YELLOW + "Click to prepare command.")}))
.create()[0]);
return;
}
if (PartyManager.getInstance().getPartyOf(player) == null && partySubCommandExecutor.mustBeInParty()) {
player.sendMessage(Constants.TAG, new ComponentBuilder("You must be in a party to do this!").color(ChatColor.RED).create()[0]);
return;
}
performSubCommand(clazz, handler);
return;
}
}

if (args.length <= 0) {
showHelpMessage(player);

return;
}
showHelpMessage(player);
}

private void performSubCommand(Class<? extends AnnotatedPartySubCommand> clazz, PartySubCommandHandler handler) {
try {
Constructor constructor = clazz.getConstructor(handler.getClass());
AnnotatedPartySubCommand subCommandInstance = (AnnotatedPartySubCommand) constructor.newInstance(handler);
if (handler.isSenderProxiedPlayer()) {
subCommandInstance.runProxiedPlayer();
} else {
subCommandInstance.runConsole();
}
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
String[] newArgs = Arrays.copyOfRange(args, 1, args.length);

PartySubCommand subCommand = this.getCommand(args[0]);

if (subCommand == null) {
showHelpMessage(player);

return;
}

PartyAnnotationCommand annotations = subCommand.getAnnotations();

if (annotations == null) return;

if (PartyRole.getRoleOf(player) == PartyRole.PARTICIPANT && annotations.leaderExclusive()) {
player.sendMessage(Constants.TAG, new ComponentBuilder("You must be the party leader in order to do this.").color(ChatColor.RED).create()[0]);

return;
}

if (newArgs.length == 0 && annotations.requiresArgumentCompletion()) {
player.sendMessage(Constants.TAG, new TextComponent("Usage: "),
new ComponentBuilder(annotations.syntax()).color(ChatColor.AQUA)
.event(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/party " + annotations.name() + " "))
.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{new TextComponent(ChatColor.YELLOW + "Click to prepare command.")}))
.create()[0]);
return;
}

if (PartyManager.getInstance().getPartyOf(player) == null && annotations.mustBeInParty()) {
player.sendMessage(Constants.TAG, new ComponentBuilder("You must be in a party to do this!").color(ChatColor.RED).create()[0]);
return;
}

subCommand.execute(player, newArgs);
}


private void showHelpMessage(ProxiedPlayer player) {
TextComponent topMSG = new TextComponent("Party Commands:");
topMSG.setColor(ChatColor.LIGHT_PURPLE);
topMSG.setBold(true);

player.sendMessage(topMSG);

TextComponent prepareMSG = new TextComponent("Click to prepare this command.");
prepareMSG.setColor(ChatColor.YELLOW);
prepareMSG.setItalic(true);

TextComponent pt1 = new TextComponent("" + '\u25CF' + " ");
for (Class<? extends AnnotatedPartySubCommand> clazz : subCommandClasses.values()) {
PartySubCommandExecutor annotatedPartySubCommand = getSubCommandClassAnnotation(clazz);

for (PartySubCommand command : commands.values()) {
PartyAnnotationCommand annotations = command.getAnnotations();

if (annotations == null) continue;

//Bullet colors tell if the player can run the command or not. Just for a quick glance.
ChatColor bulletColor = ChatColor.DARK_GREEN;
if (annotatedPartySubCommand.mustBeInParty()) {

if (annotations.mustBeInParty()) {
if (!PartyManager.getInstance().hasParty(player)) {
bulletColor = ChatColor.DARK_RED;
} else {
if (annotatedPartySubCommand.leaderExclusive()) {
if (PartyRole.getRoleOf(player) != PartyRole.LEADER)
bulletColor = ChatColor.DARK_RED;
}
} else if (annotations.leaderExclusive()) {
if (PartyRole.getRoleOf(player) != PartyRole.LEADER) bulletColor = ChatColor.DARK_RED;
}
}

pt1.setColor(bulletColor);

TextComponent pt2 = new TextComponent(annotatedPartySubCommand.syntax());
TextComponent pt2 = new TextComponent(annotations.syntax());
pt2.setColor(ChatColor.AQUA);
pt2.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new BaseComponent[]{prepareMSG}));
pt2.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/party " + annotatedPartySubCommand.subCommand() + " "));
pt2.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/party " + annotations.name() + " "));

TextComponent pt3 = new TextComponent(" - ");
pt3.setColor(ChatColor.DARK_GRAY);
TextComponent pt4 = new TextComponent(annotatedPartySubCommand.description());

TextComponent pt4 = new TextComponent(annotations.description());
pt4.setColor(ChatColor.GRAY);

player.sendMessage(pt1, pt2, pt3, pt4);
}

player.sendMessage(new TextComponent(" "));
}

private void registerSubCommand(Class<? extends AnnotatedPartySubCommand> clazz) {
subCommandClasses.put(getSubCommandClassAnnotation(clazz).subCommand(), clazz);
}
private void registerSubCommand(PartySubCommand subCommand) {
PartyAnnotationCommand annotation = subCommand.getAnnotations();

private Map<String, Class<? extends AnnotatedPartySubCommand>> getSubCommandClasses() {
return subCommandClasses;
if (annotation == null) return;

commands.put(annotation.name(), subCommand);
}

/**
* Gets the Annotation of a AnnotatedPartySubCommand class.
*
* @param clazz the annotatedpartysubcommand class
* @return Annotation if it exists, null if invalid
*/
private PartySubCommandExecutor getSubCommandClassAnnotation(Class<? extends AnnotatedPartySubCommand> clazz) {
if (clazz.isAnnotationPresent(PartySubCommandExecutor.class)) {
return clazz.getAnnotation(PartySubCommandExecutor.class);
}
return null;
private PartySubCommand getCommand(String name) {
return commands.get(name.toLowerCase());
}
}
Loading