9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2025-12-30 12:29:20 +00:00
This commit is contained in:
Daniel Mills
2020-07-31 13:45:41 -04:00
parent 121653a8f2
commit e5cdd9a7f1
40 changed files with 2769 additions and 54 deletions

View File

@@ -0,0 +1,32 @@
package com.volmit.iris.command;
import com.volmit.iris.Iris;
import com.volmit.iris.command.util.Command;
import com.volmit.iris.command.util.MortarCommand;
import com.volmit.iris.command.util.MortarSender;
public class CommandIris extends MortarCommand
{
@Command
private CommandIrisStudio studio;
public CommandIris()
{
super("iris", "ir", "irs");
requiresPermission(Iris.perm);
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
sender.sendMessage("Iris v" + Iris.instance.getDescription().getVersion() + " by Volmit Software");
printHelp(sender);
return true;
}
@Override
protected String getArgsUsage()
{
return "";
}
}

View File

@@ -0,0 +1,41 @@
package com.volmit.iris.command;
import com.volmit.iris.Iris;
import com.volmit.iris.command.util.Command;
import com.volmit.iris.command.util.MortarCommand;
import com.volmit.iris.command.util.MortarSender;
public class CommandIrisStudio extends MortarCommand
{
@Command
private CommandIrisStudioCreate create;
@Command
private CommandIrisStudioOpen open;
@Command
private CommandIrisStudioClose close;
@Command
private CommandIrisStudioList list;
public CommandIrisStudio()
{
super("studio", "std");
requiresPermission(Iris.perm.studio);
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
sender.sendMessage("Iris Studio Commands");
printHelp(sender);
return true;
}
@Override
protected String getArgsUsage()
{
return "[subcommand]";
}
}

View File

@@ -0,0 +1,71 @@
package com.volmit.iris.command;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import com.volmit.iris.Iris;
import com.volmit.iris.command.util.MortarCommand;
import com.volmit.iris.command.util.MortarSender;
public class CommandIrisStudioClose extends MortarCommand
{
public CommandIrisStudioClose()
{
super("close", "x");
requiresPermission(Iris.perm.studio);
setDescription("Close the existing dimension");
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
if(!Iris.proj.isProjectOpen())
{
sender.sendMessage("No open projects.");
return true;
}
if(sender.isPlayer())
{
World f = null;
for(World i : Bukkit.getWorlds())
{
if(i.getWorldFolder().getAbsolutePath().equals(Iris.proj.getCurrentProject().getWorld().getWorldFolder().getAbsolutePath()))
{
continue;
}
f = i;
break;
}
if(f == null)
{
for(Player i : Iris.proj.getCurrentProject().getWorld().getPlayers())
{
i.kickPlayer("Project Closing, No other world to put you in. Rejoin Please!");
}
}
else
{
for(Player i : Iris.proj.getCurrentProject().getWorld().getPlayers())
{
i.teleport(f.getSpawnLocation());
}
}
}
Iris.proj.close();
sender.sendMessage("Projects Closed & Caches Cleared!");
return true;
}
@Override
protected String getArgsUsage()
{
return "";
}
}

View File

@@ -0,0 +1,134 @@
package com.volmit.iris.command;
import java.io.IOException;
import com.google.gson.Gson;
import com.volmit.iris.Iris;
import com.volmit.iris.command.util.MortarCommand;
import com.volmit.iris.command.util.MortarSender;
import com.volmit.iris.object.InterpolationMethod;
import com.volmit.iris.object.IrisBiome;
import com.volmit.iris.object.IrisBiomeGeneratorLink;
import com.volmit.iris.object.IrisDimension;
import com.volmit.iris.object.IrisGenerator;
import com.volmit.iris.object.IrisNoiseGenerator;
import com.volmit.iris.object.IrisRegion;
import com.volmit.iris.util.Form;
import com.volmit.iris.util.IO;
import com.volmit.iris.util.JSONException;
import com.volmit.iris.util.JSONObject;
import net.md_5.bungee.api.ChatColor;
public class CommandIrisStudioCreate extends MortarCommand
{
public CommandIrisStudioCreate()
{
super("create", "new");
requiresPermission(Iris.perm.studio);
setDescription("Create a new project & open it.");
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
if(args.length != 1)
{
sender.sendMessage("Please use a lowercase name with hyphens (-) for spaces.");
sender.sendMessage("I.e. /iris std new " + ChatColor.BOLD + "aether");
return true;
}
IrisDimension dimension = new IrisDimension();
dimension.setLoadKey(args[0]);
dimension.setName(Form.capitalizeWords(args[0].replaceAll("\\Q-\\E", " ")));
sender.sendMessage("Creating New Project \"" + dimension.getName() + "\"...");
IrisRegion exampleRegion = new IrisRegion();
exampleRegion.setName("Example Region");
exampleRegion.setLoadKey("example-region");
IrisBiome exampleLand1 = new IrisBiome();
exampleLand1.setName("Example Land 1");
exampleLand1.setLoadKey("land-1");
IrisBiome exampleShore1 = new IrisBiome();
exampleShore1.setName("Example Shore");
exampleShore1.setLoadKey("shore");
IrisBiome exampleOcean1 = new IrisBiome();
exampleOcean1.setName("Example Sea");
exampleOcean1.setLoadKey("sea");
IrisBiome exampleLand2 = new IrisBiome();
exampleLand2.setName("Example Land 2");
exampleLand2.setLoadKey("land-2");
exampleLand2.setRarity(4);
dimension.setSeaZoom(1);
dimension.setLandZoom(1.5);
IrisGenerator gen = new IrisGenerator();
IrisNoiseGenerator gg = new IrisNoiseGenerator(true);
gen.setInterpolationFunction(InterpolationMethod.HERMITE);
gen.setInterpolationScale(185);
gen.getComposite().add(gg);
gen.setLoadKey("example-generator");
IrisBiomeGeneratorLink b1 = new IrisBiomeGeneratorLink();
b1.setGenerator(gen.getLoadKey());
b1.setMin(3);
b1.setMax(7);
IrisBiomeGeneratorLink b2 = new IrisBiomeGeneratorLink();
b1.setGenerator(gen.getLoadKey());
b1.setMin(12);
b1.setMax(35);
IrisBiomeGeneratorLink b3 = new IrisBiomeGeneratorLink();
b1.setGenerator(gen.getLoadKey());
b1.setMin(-1);
b1.setMax(1);
IrisBiomeGeneratorLink b4 = new IrisBiomeGeneratorLink();
b1.setGenerator(gen.getLoadKey());
b1.setMin(-5);
b1.setMax(-38);
exampleLand2.getLayers().get(0).getPalette().clear();
exampleLand2.getLayers().get(0).getPalette().add("RED_SAND");
exampleShore1.getLayers().get(0).getPalette().clear();
exampleShore1.getLayers().get(0).getPalette().add("SAND");
exampleOcean1.getLayers().get(0).getPalette().clear();
exampleOcean1.getLayers().get(0).getPalette().add("SAND");
exampleLand1.getGenerators().clear();
exampleLand1.getGenerators().add(b1);
exampleLand2.getGenerators().clear();
exampleLand2.getGenerators().add(b2);
exampleShore1.getGenerators().clear();
exampleShore1.getGenerators().add(b3);
exampleOcean1.getGenerators().clear();
exampleOcean1.getGenerators().add(b4);
exampleRegion.getLandBiomes().add(exampleLand1.getLoadKey());
exampleRegion.getLandBiomes().add(exampleLand2.getLoadKey());
exampleRegion.getShoreBiomes().add(exampleShore1.getLoadKey());
exampleRegion.getSeaBiomes().add(exampleOcean1.getLoadKey());
dimension.getRegions().add(exampleRegion.getLoadKey());
try
{
String g = "{\"folders\": [{\"path\": \".\"}],\"settings\": {\"workbench.colorTheme\": \"Monokai\",\"workbench.preferredHighContrastColorTheme\": \"Solarized Dark\",\"workbench.preferredDarkColorTheme\": \"Solarized Dark\",\"workbench.statusBar.visible\": false,\"workbench.tips.enabled\": false,\"workbench.tree.indent\": 24,\"files.autoSave\": \"onFocusChange\"}}";
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "dimensions", dimension.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(dimension)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "regions", exampleRegion.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(exampleRegion)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "biomes", exampleLand1.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(exampleLand1)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "biomes", exampleLand2.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(exampleLand2)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "biomes", exampleShore1.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(exampleShore1)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "biomes", exampleOcean1.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(exampleOcean1)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), "generators", gen.getLoadKey() + ".json"), new JSONObject(new Gson().toJson(gen)).toString(4));
IO.writeAll(Iris.instance.getDataFile("packs", dimension.getLoadKey(), dimension.getLoadKey() + ".code-workspace"), new JSONObject(g).toString(4));
Iris.proj.open(sender, dimension.getName());
}
catch(JSONException | IOException e)
{
sender.sendMessage("Failed! Check the console.");
e.printStackTrace();
}
return true;
}
@Override
protected String getArgsUsage()
{
return "[dimension]";
}
}

View File

@@ -0,0 +1,53 @@
package com.volmit.iris.command;
import java.io.File;
import com.volmit.iris.Iris;
import com.volmit.iris.command.util.MortarCommand;
import com.volmit.iris.command.util.MortarSender;
import com.volmit.iris.object.IrisDimension;
public class CommandIrisStudioList extends MortarCommand
{
public CommandIrisStudioList()
{
super("list", "l");
requiresPermission(Iris.perm.studio);
setDescription("List projects that can be opened.");
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
int m = 0;
for(File i : Iris.data.getDimensionLoader().getFolders())
{
for(File j : i.listFiles())
{
if(j.isFile() && j.getName().endsWith(".json"))
{
try
{
m++;
IrisDimension d = Iris.data.getDimensionLoader().load(j.getName().replaceAll("\\Q.json\\E", ""));
sender.sendMessage("- " + d.getLoadKey() + " (" + d.getName() + ")");
}
catch(Throwable e)
{
}
}
}
}
sender.sendMessage("Found " + m + " project" + (m == 1 ? "" : "s"));
return true;
}
@Override
protected String getArgsUsage()
{
return "";
}
}

View File

@@ -0,0 +1,33 @@
package com.volmit.iris.command;
import com.volmit.iris.Iris;
import com.volmit.iris.command.util.MortarCommand;
import com.volmit.iris.command.util.MortarSender;
public class CommandIrisStudioOpen extends MortarCommand
{
public CommandIrisStudioOpen()
{
super("open", "o");
requiresPermission(Iris.perm.studio);
setDescription("Create a new temporary world to design a dimension.");
}
@Override
public boolean handle(MortarSender sender, String[] args)
{
if(args.length != 1)
{
sender.sendMessage("/iris std open <DIMENSION> (file name without .json)");
}
Iris.proj.open(sender, args[0]);
return true;
}
@Override
protected String getArgsUsage()
{
return "[dimension]";
}
}

View File

@@ -0,0 +1,28 @@
package com.volmit.iris.command;
import com.volmit.iris.command.util.MortarPermission;
import com.volmit.iris.command.util.Permission;
public class PermissionIris extends MortarPermission
{
@Permission
public PermissionIrisStudio studio;
@Override
protected String getNode()
{
return "iris";
}
@Override
public String getDescription()
{
return "Iris Permissions";
}
@Override
public boolean isDefault()
{
return false;
}
}

View File

@@ -0,0 +1,24 @@
package com.volmit.iris.command;
import com.volmit.iris.command.util.MortarPermission;
public class PermissionIrisStudio extends MortarPermission
{
@Override
protected String getNode()
{
return "studio";
}
@Override
public String getDescription()
{
return "Iris Studio Permissions";
}
@Override
public boolean isDefault()
{
return false;
}
}

View File

@@ -0,0 +1,14 @@
package com.volmit.iris.command.util;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(FIELD)
public @interface Command
{
String value() default "";
}

View File

@@ -0,0 +1,14 @@
package com.volmit.iris.command.util;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(FIELD)
public @interface Control
{
}

View File

@@ -0,0 +1,70 @@
package com.volmit.iris.command.util;
import com.volmit.iris.Iris;
public abstract class Controller implements IController
{
private int tickRate;
private String name;
public Controller()
{
name = getClass().getSimpleName().replaceAll("Controller", "") + " Controller";
tickRate = -1;
}
protected void setTickRate(int rate)
{
this.tickRate = rate;
}
protected void disableTicking()
{
setTickRate(-1);
}
@Override
public void l(Object l)
{
Iris.info("[" + getName() + "]: " + l);
}
@Override
public void w(Object l)
{
Iris.warn("[" + getName() + "]: " + l);
}
@Override
public void f(Object l)
{
Iris.error("[" + getName() + "]: " + l);
}
@Override
public void v(Object l)
{
Iris.verbose("[" + getName() + "]: " + l);
}
@Override
public String getName()
{
return name;
}
@Override
public abstract void start();
@Override
public abstract void stop();
@Override
public abstract void tick();
@Override
public int getTickInterval()
{
return tickRate;
}
}

View File

@@ -0,0 +1,55 @@
package com.volmit.iris.command.util;
import com.volmit.iris.util.KList;
/**
* Represents a pawn command
*
* @author cyberpwn
*
*/
public interface ICommand
{
public KList<String> getRequiredPermissions();
/**
* Get the name of this command (node)
*
* @return the node
*/
public String getNode();
/**
* Get all (realized) nodes of this command
*
* @return the nodes
*/
public KList<String> getNodes();
/**
* Get all (every) node in this command
*
* @return all nodes
*/
public KList<String> getAllNodes();
/**
* Add a node to this command
*
* @param node
* the node
*/
public void addNode(String node);
/**
* Handle a command. If this is a subcommand, parameters after the subcommand
* will be adapted in args for you
*
* @param sender
* the volume sender (pre-tagged)
* @param args
* the arguments after this command node
* @return return true to mark it as handled
*/
public boolean handle(MortarSender sender, String[] args);
}

View File

@@ -0,0 +1,24 @@
package com.volmit.iris.command.util;
import org.bukkit.event.Listener;
public interface IController extends Listener
{
public String getName();
public void start();
public void stop();
public void tick();
public int getTickInterval();
public void l(Object l);
public void w(Object l);
public void f(Object l);
public void v(Object l);
}

View File

@@ -0,0 +1,14 @@
package com.volmit.iris.command.util;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(FIELD)
public @interface Instance
{
}

View File

@@ -0,0 +1,180 @@
package com.volmit.iris.command.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.ChatColor;
import com.volmit.iris.util.KList;
/**
* Represents a pawn command
*
* @author cyberpwn
*
*/
public abstract class MortarCommand implements ICommand
{
private KList<MortarCommand> children;
private KList<String> nodes;
private KList<String> requiredPermissions;
private String node;
private String category;
private String description;
/**
* Override this with a super constructor as most commands shouldnt change these
* parameters
*
* @param node
* the node (primary node) i.e. volume
* @param nodes
* the aliases. i.e. v, vol, bile
*/
public MortarCommand(String node, String... nodes)
{
category = "";
this.node = node;
this.nodes = new KList<String>(nodes);
requiredPermissions = new KList<>();
children = buildChildren();
description = "No Description";
}
public void printHelp(MortarSender sender)
{
boolean b = false;
for(MortarCommand i : getChildren())
{
for(String j : i.getRequiredPermissions())
{
if(!sender.hasPermission(j))
{
continue;
}
}
b = true;
sender.sendMessage("/" + ChatColor.GREEN + i.getAllNodes().toString(",") + " " + ChatColor.WHITE + getArgsUsage() + ChatColor.GRAY + " - " + getDescription());
}
if(!b)
{
sender.sendMessage("There are either no sub-commands or you do not have permission to use them.");
}
}
protected abstract String getArgsUsage();
public String getDescription()
{
return description;
}
protected void setDescription(String description)
{
this.description = description;
}
protected void requiresPermission(MortarPermission node)
{
if(node == null)
{
return;
}
requiresPermission(node.toString());
}
protected void requiresPermission(String node)
{
if(node == null)
{
return;
}
requiredPermissions.add(node);
}
@Override
public String getNode()
{
return node;
}
@Override
public KList<String> getNodes()
{
return nodes;
}
@Override
public KList<String> getAllNodes()
{
return getNodes().copy().qadd(getNode());
}
@Override
public void addNode(String node)
{
getNodes().add(node);
}
public KList<MortarCommand> getChildren()
{
return children;
}
private KList<MortarCommand> buildChildren()
{
KList<MortarCommand> p = new KList<>();
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Command.class))
{
try
{
i.setAccessible(true);
MortarCommand pc = (MortarCommand) i.getType().getConstructor().newInstance();
Command c = i.getAnnotation(Command.class);
if(!c.value().trim().isEmpty())
{
pc.setCategory(c.value().trim());
}
else
{
pc.setCategory(getCategory());
}
p.add(pc);
}
catch(IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
e.printStackTrace();
}
}
}
return p;
}
@Override
public KList<String> getRequiredPermissions()
{
return requiredPermissions;
}
public String getCategory()
{
return category;
}
public void setCategory(String category)
{
this.category = category;
}
}

View File

@@ -0,0 +1,100 @@
package com.volmit.iris.command.util;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import org.bukkit.command.CommandSender;
import com.volmit.iris.util.KList;
public abstract class MortarPermission
{
private MortarPermission parent;
public MortarPermission()
{
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Permission.class))
{
try
{
MortarPermission px = (MortarPermission) i.getType().getConstructor().newInstance();
px.setParent(this);
i.set(Modifier.isStatic(i.getModifiers()) ? null : this, px);
}
catch(IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
e.printStackTrace();
}
}
}
}
public KList<MortarPermission> getChildren()
{
KList<MortarPermission> p = new KList<>();
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Permission.class))
{
try
{
p.add((MortarPermission) i.get(Modifier.isStatic(i.getModifiers()) ? null : this));
}
catch(IllegalArgumentException | IllegalAccessException | SecurityException e)
{
e.printStackTrace();
}
}
}
return p;
}
public String getFullNode()
{
if(hasParent())
{
return getParent().getFullNode() + "." + getNode();
}
return getNode();
}
protected abstract String getNode();
public abstract String getDescription();
public abstract boolean isDefault();
@Override
public String toString()
{
return getFullNode();
}
public boolean hasParent()
{
return getParent() != null;
}
public MortarPermission getParent()
{
return parent;
}
public void setParent(MortarPermission parent)
{
this.parent = parent;
}
public boolean has(CommandSender sender)
{
return sender.hasPermission(getFullNode());
}
}

View File

@@ -0,0 +1,620 @@
package com.volmit.iris.command.util;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.Iterator;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin;
import com.volmit.iris.Iris;
import com.volmit.iris.util.IO;
import com.volmit.iris.util.J;
import com.volmit.iris.util.KList;
import com.volmit.iris.util.KMap;
import com.volmit.iris.util.M;
public abstract class MortarPlugin extends JavaPlugin implements Listener
{
private KMap<KList<String>, VirtualCommand> commands;
private KList<MortarCommand> commandCache;
private KList<MortarPermission> permissionCache;
private KMap<String, IController> controllers;
private KList<IController> cachedControllers;
private KMap<Class<? extends IController>, IController> cachedClassControllers;
public void l(Object l)
{
Iris.info("[" + getName() + "]: " + l);
}
public void w(Object l)
{
Iris.warn("[" + getName() + "]: " + l);
}
public void f(Object l)
{
Iris.error("[" + getName() + "]: " + l);
}
public void v(Object l)
{
Iris.verbose("[" + getName() + "]: " + l);
}
public void onEnable()
{
registerInstance();
registerPermissions();
registerCommands();
registerControllers();
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, this::tickControllers, 0, 0);
J.a(() -> outputInfo());
registerListener(this);
start();
}
public void unregisterAll()
{
stopControllers();
unregisterListeners();
unregisterCommands();
unregisterPermissions();
unregisterInstance();
}
private void outputInfo()
{
try
{
IO.delete(getDataFolder("info"));
getDataFolder("info").mkdirs();
outputPluginInfo();
outputCommandInfo();
outputPermissionInfo();
}
catch(Throwable e)
{
}
}
private void outputPermissionInfo() throws IOException
{
FileConfiguration fc = new YamlConfiguration();
for(MortarPermission i : permissionCache)
{
chain(i, fc);
}
fc.save(getDataFile("info", "permissions.yml"));
}
private void chain(MortarPermission i, FileConfiguration fc)
{
KList<String> ff = new KList<String>();
for(MortarPermission j : i.getChildren())
{
ff.add(j.getFullNode());
}
fc.set(i.getFullNode().replaceAll("\\Q.\\E", ",") + "." + "description", i.getDescription());
fc.set(i.getFullNode().replaceAll("\\Q.\\E", ",") + "." + "default", i.isDefault());
fc.set(i.getFullNode().replaceAll("\\Q.\\E", ",") + "." + "children", ff);
for(MortarPermission j : i.getChildren())
{
chain(j, fc);
}
}
private void outputCommandInfo() throws IOException
{
FileConfiguration fc = new YamlConfiguration();
for(MortarCommand i : commandCache)
{
chain(i, "/", fc);
}
fc.save(getDataFile("info", "commands.yml"));
}
private void chain(MortarCommand i, String c, FileConfiguration fc)
{
String n = c + (c.length() == 1 ? "" : " ") + i.getNode();
fc.set(n + "." + "description", i.getDescription());
fc.set(n + "." + "required-permissions", i.getRequiredPermissions());
fc.set(n + "." + "aliases", i.getAllNodes());
for(MortarCommand j : i.getChildren())
{
chain(j, n, fc);
}
}
private void outputPluginInfo() throws IOException
{
FileConfiguration fc = new YamlConfiguration();
fc.set("version", getDescription().getVersion());
fc.set("name", getDescription().getName());
fc.save(getDataFile("info", "plugin.yml"));
}
private void registerPermissions()
{
permissionCache = new KList<>();
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Permission.class))
{
try
{
i.setAccessible(true);
MortarPermission pc = (MortarPermission) i.getType().getConstructor().newInstance();
i.set(Modifier.isStatic(i.getModifiers()) ? null : this, pc);
registerPermission(pc);
permissionCache.add(pc);
v("Registered Permissions " + pc.getFullNode() + " (" + i.getName() + ")");
}
catch(IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
w("Failed to register permission (field " + i.getName() + ")");
e.printStackTrace();
}
}
}
for(org.bukkit.permissions.Permission i : computePermissions())
{
try
{
Bukkit.getPluginManager().addPermission(i);
}
catch(Throwable e)
{
}
}
}
private KList<org.bukkit.permissions.Permission> computePermissions()
{
KList<org.bukkit.permissions.Permission> g = new KList<>();
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Permission.class))
{
try
{
MortarPermission x = (MortarPermission) i.get(Modifier.isStatic(i.getModifiers()) ? null : this);
g.add(toPermission(x));
g.addAll(computePermissions(x));
}
catch(IllegalArgumentException | IllegalAccessException | SecurityException e)
{
e.printStackTrace();
}
}
}
return g.removeDuplicates();
}
private KList<org.bukkit.permissions.Permission> computePermissions(MortarPermission p)
{
KList<org.bukkit.permissions.Permission> g = new KList<>();
if(p == null)
{
return g;
}
for(MortarPermission i : p.getChildren())
{
if(i == null)
{
continue;
}
g.add(toPermission(i));
g.addAll(computePermissions(i));
}
return g;
}
private org.bukkit.permissions.Permission toPermission(MortarPermission p)
{
if(p == null)
{
return null;
}
org.bukkit.permissions.Permission perm = new org.bukkit.permissions.Permission(p.getFullNode() + (p.hasParent() ? "" : ".*"));
perm.setDescription(p.getDescription() == null ? "" : p.getDescription());
perm.setDefault(p.isDefault() ? PermissionDefault.TRUE : PermissionDefault.OP);
for(MortarPermission i : p.getChildren())
{
perm.getChildren().put(i.getFullNode(), true);
}
return perm;
}
private void registerPermission(MortarPermission pc)
{
}
@Override
public void onDisable()
{
stop();
Bukkit.getScheduler().cancelTasks(this);
unregisterListener(this);
unregisterAll();
}
private void tickControllers()
{
for(IController i : getControllers())
{
tickController(i);
}
}
private void tickController(IController i)
{
if(i.getTickInterval() < 0)
{
return;
}
M.tick++;
if(M.interval(i.getTickInterval()))
{
try
{
i.tick();
}
catch(Throwable e)
{
w("Failed to tick controller " + i.getName());
e.printStackTrace();
}
}
}
public KList<IController> getControllers()
{
return cachedControllers;
}
private void registerControllers()
{
controllers = new KMap<>();
cachedClassControllers = new KMap<>();
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Control.class))
{
try
{
i.setAccessible(true);
IController pc = (IController) i.getType().getConstructor().newInstance();
registerController(pc);
i.set(this, pc);
v("Registered " + pc.getName() + " (" + i.getName() + ")");
}
catch(IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
w("Failed to register controller (field " + i.getName() + ")");
e.printStackTrace();
}
}
}
cachedControllers = controllers.v();
}
public IController getController(Class<? extends IController> c)
{
return cachedClassControllers.get(c);
}
private void registerController(IController pc)
{
controllers.put(pc.getName(), pc);
cachedClassControllers.put(pc.getClass(), pc);
registerListener(pc);
try
{
pc.start();
v("Started " + pc.getName());
}
catch(Throwable e)
{
w("Failed to start controller " + pc.getName());
e.printStackTrace();
}
}
private void registerInstance()
{
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Instance.class))
{
try
{
i.setAccessible(true);
i.set(Modifier.isStatic(i.getModifiers()) ? null : this, this);
v("Registered Instance " + i.getName());
}
catch(IllegalArgumentException | IllegalAccessException | SecurityException e)
{
w("Failed to register instance (field " + i.getName() + ")");
e.printStackTrace();
}
}
}
}
private void unregisterInstance()
{
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Instance.class))
{
try
{
i.setAccessible(true);
i.set(Modifier.isStatic(i.getModifiers()) ? null : this, null);
v("Unregistered Instance " + i.getName());
}
catch(IllegalArgumentException | IllegalAccessException | SecurityException e)
{
w("Failed to unregister instance (field " + i.getName() + ")");
e.printStackTrace();
}
}
}
}
private void registerCommands()
{
commands = new KMap<>();
commandCache = new KList<>();
for(Field i : getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(com.volmit.iris.command.util.Command.class))
{
try
{
i.setAccessible(true);
MortarCommand pc = (MortarCommand) i.getType().getConstructor().newInstance();
com.volmit.iris.command.util.Command c = i.getAnnotation(com.volmit.iris.command.util.Command.class);
registerCommand(pc, c.value());
commandCache.add(pc);
v("Registered Commands /" + pc.getNode() + " (" + i.getName() + ")");
}
catch(IllegalArgumentException | IllegalAccessException | InstantiationException | InvocationTargetException | NoSuchMethodException | SecurityException e)
{
w("Failed to register command (field " + i.getName() + ")");
e.printStackTrace();
}
}
}
}
@Override
public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args)
{
KList<String> chain = new KList<String>();
chain.add(args);
for(KList<String> i : commands.k())
{
for(String j : i)
{
if(j.equalsIgnoreCase(label))
{
VirtualCommand cmd = commands.get(i);
if(cmd.hit(sender, chain.copy(), label))
{
return true;
}
}
}
}
return false;
}
public void registerCommand(ICommand cmd)
{
registerCommand(cmd, "");
}
public void registerCommand(ICommand cmd, String subTag)
{
commands.put(cmd.getAllNodes(), new VirtualCommand(cmd, subTag.trim().isEmpty() ? getTag() : getTag(subTag.trim())));
PluginCommand cc = getCommand(cmd.getNode().toLowerCase());
if(cc != null)
{
cc.setExecutor(this);
cc.setUsage(getName() + ":" + getClass().toString() + ":" + cmd.getNode());
}
else
{
RouterCommand r = new RouterCommand(cmd, this);
r.setUsage(getName() + ":" + getClass().toString());
((CommandMap) new com.volmit.iris.util.V(Bukkit.getServer()).get("commandMap")).register("", r);
}
}
public void unregisterCommand(ICommand cmd)
{
try
{
SimpleCommandMap m = new com.volmit.iris.util.V(Bukkit.getServer()).get("commandMap");
Map<String, Command> k = new com.volmit.iris.util.V(m).get("knownCommands");
for(Iterator<Map.Entry<String, Command>> it = k.entrySet().iterator(); it.hasNext();)
{
Map.Entry<String, Command> entry = it.next();
if(entry.getValue() instanceof Command)
{
org.bukkit.command.Command c = (org.bukkit.command.Command) entry.getValue();
String u = c.getUsage();
if(u != null && u.equals(getName() + ":" + getClass().toString() + ":" + cmd.getNode()))
{
if(c.unregister(m))
{
it.remove();
v("Unregistered Command /" + cmd.getNode());
}
else
{
Bukkit.getConsoleSender().sendMessage(getTag() + "Failed to unregister command " + c.getName());
}
}
}
}
}
catch(Throwable e)
{
e.printStackTrace();
}
}
public String getTag()
{
return getTag("");
}
public void registerListener(Listener l)
{
Bukkit.getPluginManager().registerEvents(l, this);
}
public void unregisterListener(Listener l)
{
HandlerList.unregisterAll(l);
}
public void unregisterListeners()
{
HandlerList.unregisterAll((Listener) this);
}
public void unregisterCommands()
{
for(VirtualCommand i : commands.v())
{
try
{
unregisterCommand(i.getCommand());
}
catch(Throwable e)
{
}
}
}
private void unregisterPermissions()
{
for(org.bukkit.permissions.Permission i : computePermissions())
{
Bukkit.getPluginManager().removePermission(i);
v("Unregistered Permission " + i.getName());
}
}
private void stopControllers()
{
for(IController i : controllers.v())
{
try
{
unregisterListener(i);
i.stop();
v("Stopped " + i.getName());
}
catch(Throwable e)
{
w("Failed to stop controller " + i.getName());
e.printStackTrace();
}
}
}
public File getDataFile(String... strings)
{
File f = new File(getDataFolder(), new KList<String>(strings).toString(File.separator));
f.getParentFile().mkdirs();
return f;
}
public File getDataFolder(String... strings)
{
if(strings.length == 0)
{
return super.getDataFolder();
}
File f = new File(getDataFolder(), new KList<String>(strings).toString(File.separator));
f.mkdirs();
return f;
}
public abstract void start();
public abstract void stop();
public abstract String getTag(String subTag);
}

View File

@@ -0,0 +1,209 @@
package com.volmit.iris.command.util;
import java.util.Set;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.permissions.PermissionAttachmentInfo;
import org.bukkit.plugin.Plugin;
import lombok.Getter;
import lombok.Setter;
/**
* Represents a volume sender. A command sender with extra crap in it
*
* @author cyberpwn
*
*/
public class MortarSender implements CommandSender
{
private CommandSender s;
private String tag;
@Getter
@Setter
private String command;
/**
* Wrap a command sender
*
* @param s
* the command sender
*/
public MortarSender(CommandSender s)
{
tag = "";
this.s = s;
}
public MortarSender(CommandSender s, String tag)
{
this.tag = tag;
this.s = s;
}
/**
* Set a command tag (prefix for sendMessage)
*
* @param tag
* the tag
*/
public void setTag(String tag)
{
this.tag = tag;
}
/**
* Get the command tag
*
* @return the command tag
*/
public String getTag()
{
return tag;
}
/**
* Is this sender a player?
*
* @return true if it is
*/
public boolean isPlayer()
{
return getS() instanceof Player;
}
/**
* Force cast to player (be sure to check first)
*
* @return a casted player
*/
public Player player()
{
return (Player) getS();
}
/**
* Get the origin sender this object is wrapping
*
* @return the command sender
*/
public CommandSender getS()
{
return s;
}
@Override
public boolean isPermissionSet(String name)
{
return s.isPermissionSet(name);
}
@Override
public boolean isPermissionSet(Permission perm)
{
return s.isPermissionSet(perm);
}
@Override
public boolean hasPermission(String name)
{
return s.hasPermission(name);
}
@Override
public boolean hasPermission(Permission perm)
{
return s.hasPermission(perm);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value)
{
return s.addAttachment(plugin, name, value);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin)
{
return s.addAttachment(plugin);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, String name, boolean value, int ticks)
{
return s.addAttachment(plugin, name, value, ticks);
}
@Override
public PermissionAttachment addAttachment(Plugin plugin, int ticks)
{
return s.addAttachment(plugin, ticks);
}
@Override
public void removeAttachment(PermissionAttachment attachment)
{
s.removeAttachment(attachment);
}
@Override
public void recalculatePermissions()
{
s.recalculatePermissions();
}
@Override
public Set<PermissionAttachmentInfo> getEffectivePermissions()
{
return s.getEffectivePermissions();
}
@Override
public boolean isOp()
{
return s.isOp();
}
@Override
public void setOp(boolean value)
{
s.setOp(value);
}
@Override
public void sendMessage(String message)
{
s.sendMessage(ChatColor.translateAlternateColorCodes('&', getTag()) + message);
}
@Override
public void sendMessage(String[] messages)
{
for(String str : messages)
s.sendMessage(ChatColor.translateAlternateColorCodes('&', getTag() + str));
}
@Override
public Server getServer()
{
return s.getServer();
}
@Override
public String getName()
{
return s.getName();
}
@Override
public Spigot spigot()
{
return s.spigot();
}
}

View File

@@ -0,0 +1,14 @@
package com.volmit.iris.command.util;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.*;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(FIELD)
public @interface Permission
{
}

View File

@@ -0,0 +1,52 @@
package com.volmit.iris.command.util;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
/**
* Assistive command router
*
* @author cyberpwn
*
*/
public class RouterCommand extends org.bukkit.command.Command
{
private CommandExecutor ex;
private String usage;
/**
* The router command routes commands to bukkit executors
*
* @param realCommand
* the real command
* @param ex
* the executor
*/
public RouterCommand(ICommand realCommand, CommandExecutor ex)
{
super(realCommand.getNode().toLowerCase());
setAliases(realCommand.getNodes());
this.ex = ex;
}
@Override
public Command setUsage(String u)
{
this.usage = u;
return this;
}
@Override
public String getUsage()
{
return usage;
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args)
{
return ex.onCommand(sender, this, commandLabel, args);
}
}

View File

@@ -0,0 +1,146 @@
package com.volmit.iris.command.util;
import java.lang.reflect.Field;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import com.volmit.iris.Iris;
import com.volmit.iris.util.KList;
import com.volmit.iris.util.KMap;
import com.volmit.iris.util.V;
/**
* Represents a virtual command. A chain of iterative processing through
* subcommands.
*
* @author cyberpwn
*
*/
public class VirtualCommand
{
private ICommand command;
private String tag;
private KMap<KList<String>, VirtualCommand> children;
public VirtualCommand(ICommand command)
{
this(command, "");
}
public VirtualCommand(ICommand command, String tag)
{
this.command = command;
children = new KMap<KList<String>, VirtualCommand>();
this.tag = tag;
for(Field i : command.getClass().getDeclaredFields())
{
if(i.isAnnotationPresent(Command.class))
{
try
{
Command cc = i.getAnnotation(Command.class);
ICommand cmd = (ICommand) i.getType().getConstructor().newInstance();
new V(command, true, true).set(i.getName(), cmd);
children.put(cmd.getAllNodes(), new VirtualCommand(cmd, cc.value().trim().isEmpty() ? tag : cc.value().trim()));
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
public String getTag()
{
return tag;
}
public ICommand getCommand()
{
return command;
}
public KMap<KList<String>, VirtualCommand> getChildren()
{
return children;
}
public boolean hit(CommandSender sender, KList<String> chain)
{
return hit(sender, chain, null);
}
public boolean hit(CommandSender sender, KList<String> chain, String label)
{
MortarSender vs = new MortarSender(sender);
vs.setTag(tag);
if(label != null)
vs.setCommand(label);
if(chain.isEmpty())
{
if(!checkPermissions(sender, command))
{
return true;
}
return command.handle(vs, new String[0]);
}
String nl = chain.get(0);
for(KList<String> i : children.k())
{
for(String j : i)
{
if(j.equalsIgnoreCase(nl))
{
vs.setCommand(chain.get(0));
VirtualCommand cmd = children.get(i);
KList<String> c = chain.copy();
c.remove(0);
if(cmd.hit(sender, c, vs.getCommand()))
{
return true;
}
}
}
}
if(!checkPermissions(sender, command))
{
return true;
}
return command.handle(vs, chain.toArray(new String[chain.size()]));
}
private boolean checkPermissions(CommandSender sender, ICommand command2)
{
boolean failed = false;
for(String i : command.getRequiredPermissions())
{
if(!sender.hasPermission(i))
{
failed = true;
Bukkit.getScheduler().scheduleSyncDelayedTask(Iris.instance, () -> sender.sendMessage("- " + ChatColor.WHITE + i), 0);
}
}
if(failed)
{
sender.sendMessage("Insufficient Permissions");
return false;
}
return true;
}
}