From 7d859661ba710d4889e783917a15ecba1c62b6bf Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Tue, 17 Aug 2021 17:22:27 +0200 Subject: [PATCH] Sorry for merge conflicts :) --- .../object/CommandIrisObjectAnalyze.java | 66 ----------- .../core/command/world/CommandLocate.java | 72 ------------ .../com/volmit/iris/core/decrees/DecIris.java | 4 + .../volmit/iris/core/decrees/DecObject.java | 86 ++++++++++++++ .../volmit/iris/core/service/LocateSVC.java | 33 ++++++ .../util/decree/handlers/ObjectHandler.java | 67 +++++++++++ .../volmit/iris/util/plugin/VolmitSender.java | 106 +++++++++--------- 7 files changed, 245 insertions(+), 189 deletions(-) delete mode 100644 src/main/java/com/volmit/iris/core/command/world/CommandLocate.java create mode 100644 src/main/java/com/volmit/iris/core/decrees/DecObject.java create mode 100644 src/main/java/com/volmit/iris/core/service/LocateSVC.java create mode 100644 src/main/java/com/volmit/iris/util/decree/handlers/ObjectHandler.java diff --git a/src/main/java/com/volmit/iris/core/command/object/CommandIrisObjectAnalyze.java b/src/main/java/com/volmit/iris/core/command/object/CommandIrisObjectAnalyze.java index 88e541d0a..fa8e99db0 100644 --- a/src/main/java/com/volmit/iris/core/command/object/CommandIrisObjectAnalyze.java +++ b/src/main/java/com/volmit/iris/core/command/object/CommandIrisObjectAnalyze.java @@ -86,73 +86,7 @@ public class CommandIrisObjectAnalyze extends MortarCommand { Player p = sender.player(); J.a(() -> { - IrisObject obj = IrisData.loadAnyObject(args[0]); - if (obj == null || obj.getLoadFile() == null) { - sender.sendMessage("Can't find " + args[0] + " in the " + StudioSVC.WORKSPACE_NAME + " folder"); - return; - } - - sender.sendMessage("Object Size: " + obj.getW() + " * " + obj.getH() + " * " + obj.getD() + ""); - sender.sendMessage("Blocks Used: " + NumberFormat.getIntegerInstance().format(obj.getBlocks().size())); - - Queue queue = obj.getBlocks().enqueueValues(); - Map> unsorted = new HashMap<>(); - Map amounts = new HashMap<>(); - Map materials = new HashMap<>(); - while (queue.hasNext()) { - BlockData block = queue.next(); - - //unsorted.put(block.getMaterial(), block); - - if (!amounts.containsKey(block)) { - amounts.put(block, 1); - - - } else - amounts.put(block, amounts.get(block) + 1); - - if (!materials.containsKey(block.getMaterial())) { - materials.put(block.getMaterial(), 1); - unsorted.put(block.getMaterial(), new HashSet<>()); - unsorted.get(block.getMaterial()).add(block); - } else { - materials.put(block.getMaterial(), materials.get(block.getMaterial()) + 1); - unsorted.get(block.getMaterial()).add(block); - } - - } - - List sortedMatsList = amounts.keySet().stream().map(BlockData::getMaterial) - .sorted().collect(Collectors.toList()); - Set sortedMats = new TreeSet<>(Comparator.comparingInt(materials::get).reversed()); - sortedMats.addAll(sortedMatsList); - sender.sendMessage("== Blocks in object =="); - - int n = 0; - for (Material mat : sortedMats) { - int amount = materials.get(mat); - List set = new ArrayList<>(unsorted.get(mat)); - set.sort(Comparator.comparingInt(amounts::get).reversed()); - BlockData data = set.get(0); - int dataAmount = amounts.get(data); - - String string = " - " + mat.toString() + "*" + amount; - if (data.getAsString(true).contains("[")) { - string = string + " --> [" + data.getAsString(true).split("\\[")[1] - .replaceAll("true", ChatColor.GREEN + "true" + ChatColor.GRAY) - .replaceAll("false", ChatColor.RED + "false" + ChatColor.GRAY) + "*" + dataAmount; - } - - sender.sendMessage(string); - - n++; - - if (n >= 10) { - sender.sendMessage(" + " + (sortedMats.size() - n) + " other block types"); - return; - } - } }); return true; diff --git a/src/main/java/com/volmit/iris/core/command/world/CommandLocate.java b/src/main/java/com/volmit/iris/core/command/world/CommandLocate.java deleted file mode 100644 index ac8a59636..000000000 --- a/src/main/java/com/volmit/iris/core/command/world/CommandLocate.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Iris is a World Generator for Minecraft Bukkit Servers - * Copyright (c) 2021 Arcane Arts (Volmit Software) - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.volmit.iris.core.command.world; - -import com.volmit.iris.Iris; -import com.volmit.iris.core.tools.IrisToolbelt; -import com.volmit.iris.util.collection.KList; -import com.volmit.iris.util.plugin.MortarCommand; -import com.volmit.iris.util.plugin.VolmitSender; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; - -import java.util.Arrays; - -public class CommandLocate extends MortarCommand implements Listener { - - @EventHandler - public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event) { - if (IrisToolbelt.isIrisWorld(event.getPlayer().getWorld())) { - - // Make sure the command starts with /locate and does not locate stronghold - if (event.getMessage().contains("/locate") && event.getMessage().contains("stronghold")) { - return; - } - if (event.getMessage().contains("/locate")) { - event.setCancelled(true); // Cancel the vanilla command process - String command = event.getMessage().replace("/locate", "ir std goto"); - Bukkit.dispatchCommand(event.getPlayer(), command); - } - } - } - - public CommandLocate() { - super("locate"); - requiresPermission(Iris.perm); - Iris.instance.registerListener(this); - } - - @Override - public boolean handle(VolmitSender sender, String[] args) { - Bukkit.dispatchCommand(sender, "/ir std goto " + Arrays.toString(args)); - return true; - } - - @Override - public void addTabOptions(VolmitSender sender, String[] args, KList list) { - - } - - @Override - protected String getArgsUsage() { - return "[biome/region/structure]"; - } -} diff --git a/src/main/java/com/volmit/iris/core/decrees/DecIris.java b/src/main/java/com/volmit/iris/core/decrees/DecIris.java index f491a764e..b86d1dc8f 100644 --- a/src/main/java/com/volmit/iris/core/decrees/DecIris.java +++ b/src/main/java/com/volmit/iris/core/decrees/DecIris.java @@ -42,6 +42,10 @@ public class DecIris implements DecreeExecutor { private DecPregen pregen; + private DecSettings settings; + + private DecObject object; + @Decree(description = "Create a new world", aliases = "+") public void create( @Param(aliases = "world-name", description = "The name of the world to create", defaultValue = "IrisWorld") diff --git a/src/main/java/com/volmit/iris/core/decrees/DecObject.java b/src/main/java/com/volmit/iris/core/decrees/DecObject.java new file mode 100644 index 000000000..5cc763a9b --- /dev/null +++ b/src/main/java/com/volmit/iris/core/decrees/DecObject.java @@ -0,0 +1,86 @@ +package com.volmit.iris.core.decrees; + +import com.volmit.iris.engine.object.objects.IrisObject; +import com.volmit.iris.util.decree.DecreeExecutor; +import com.volmit.iris.util.decree.DecreeOrigin; +import com.volmit.iris.util.decree.annotations.Decree; +import com.volmit.iris.util.decree.annotations.Param; +import com.volmit.iris.util.scheduling.Queue; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.block.data.BlockData; + +import java.text.NumberFormat; +import java.util.*; +import java.util.stream.Collectors; + +@Decree(name = "object", origin = DecreeOrigin.PLAYER, description = "Iris object manipulation") +public class DecObject implements DecreeExecutor { + + @Decree(description = "Check the composition of an object") + public void analyze( + @Param(description = "The object to analyze") + IrisObject object + ) { + sender().sendMessage("Object Size: " + object.getW() + " * " + object.getH() + " * " + object.getD() + ""); + sender().sendMessage("Blocks Used: " + NumberFormat.getIntegerInstance().format(object.getBlocks().size())); + + Queue queue = object.getBlocks().enqueueValues(); + Map> unsorted = new HashMap<>(); + Map amounts = new HashMap<>(); + Map materials = new HashMap<>(); + while (queue.hasNext()) { + BlockData block = queue.next(); + + //unsorted.put(block.getMaterial(), block); + + if (!amounts.containsKey(block)) { + amounts.put(block, 1); + + + } else + amounts.put(block, amounts.get(block) + 1); + + if (!materials.containsKey(block.getMaterial())) { + materials.put(block.getMaterial(), 1); + unsorted.put(block.getMaterial(), new HashSet<>()); + unsorted.get(block.getMaterial()).add(block); + } else { + materials.put(block.getMaterial(), materials.get(block.getMaterial()) + 1); + unsorted.get(block.getMaterial()).add(block); + } + + } + + List sortedMatsList = amounts.keySet().stream().map(BlockData::getMaterial) + .sorted().collect(Collectors.toList()); + Set sortedMats = new TreeSet<>(Comparator.comparingInt(materials::get).reversed()); + sortedMats.addAll(sortedMatsList); + sender().sendMessage("== Blocks in object =="); + + int n = 0; + for (Material mat : sortedMats) { + int amount = materials.get(mat); + List set = new ArrayList<>(unsorted.get(mat)); + set.sort(Comparator.comparingInt(amounts::get).reversed()); + BlockData data = set.get(0); + int dataAmount = amounts.get(data); + + String string = " - " + mat.toString() + "*" + amount; + if (data.getAsString(true).contains("[")) { + string = string + " --> [" + data.getAsString(true).split("\\[")[1] + .replaceAll("true", ChatColor.GREEN + "true" + ChatColor.GRAY) + .replaceAll("false", ChatColor.RED + "false" + ChatColor.GRAY) + "*" + dataAmount; + } + + sender().sendMessage(string); + + n++; + + if (n >= 10) { + sender().sendMessage(" + " + (sortedMats.size() - n) + " other block types"); + return; + } + } + } +} diff --git a/src/main/java/com/volmit/iris/core/service/LocateSVC.java b/src/main/java/com/volmit/iris/core/service/LocateSVC.java new file mode 100644 index 000000000..1d862a34c --- /dev/null +++ b/src/main/java/com/volmit/iris/core/service/LocateSVC.java @@ -0,0 +1,33 @@ +package com.volmit.iris.core.service; + +import com.volmit.iris.core.tools.IrisToolbelt; +import com.volmit.iris.util.format.C; +import com.volmit.iris.util.plugin.IrisService; +import com.volmit.iris.util.plugin.VolmitSender; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +public class LocateSVC implements IrisService { + + @EventHandler + public void on(final PlayerCommandPreprocessEvent event) { + if (IrisToolbelt.isIrisWorld(event.getPlayer().getWorld())) { + VolmitSender sender = new VolmitSender(event.getPlayer()); + sender.sendMessage(C.YELLOW + "You cannot locate structures in Iris worlds through vanilla commands"); + sender.sendMessage("You can use:"); + // TODO: Convert this to have the correct command prefix + Bukkit.dispatchCommand(event.getPlayer(), "/ird studio find"); + } + } + + @Override + public void onEnable() { + + } + + @Override + public void onDisable() { + + } +} diff --git a/src/main/java/com/volmit/iris/util/decree/handlers/ObjectHandler.java b/src/main/java/com/volmit/iris/util/decree/handlers/ObjectHandler.java new file mode 100644 index 000000000..b4a8ce78a --- /dev/null +++ b/src/main/java/com/volmit/iris/util/decree/handlers/ObjectHandler.java @@ -0,0 +1,67 @@ +package com.volmit.iris.util.decree.handlers; + +import com.volmit.iris.Iris; +import com.volmit.iris.core.project.loader.IrisData; +import com.volmit.iris.engine.object.objects.IrisObject; +import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.collection.KMap; +import com.volmit.iris.util.decree.DecreeParameterHandler; +import com.volmit.iris.util.decree.exceptions.DecreeParsingException; +import com.volmit.iris.util.decree.exceptions.DecreeWhichException; + +import java.io.File; + +public class ObjectHandler implements DecreeParameterHandler { + @Override + public KList getPossibilities() { + KMap p = new KMap<>(); + + //noinspection ConstantConditions + for (File i : Iris.instance.getDataFolder("packs").listFiles()) { + if (i.isDirectory()) { + IrisData data = new IrisData(i, true); + for (IrisObject j : data.getObjectLoader().loadAll(data.getObjectLoader().getPossibleKeys())) { + p.putIfAbsent(j.getLoadKey(), j); + } + + data.close(); + } + } + + return p.v(); + } + + @Override + public String toString(IrisObject irisObject) { + return irisObject.getLoadKey(); + } + + @Override + public IrisObject parse(String in) throws DecreeParsingException, DecreeWhichException { + try { + KList options = getPossibilities(in); + + if (options.isEmpty()) { + throw new DecreeParsingException("Unable to find Object \"" + in + "\""); + } else if (options.size() > 1) { + throw new DecreeWhichException(); + } + + return options.get(0); + } catch (DecreeParsingException e) { + throw e; + } catch (Throwable e) { + throw new DecreeParsingException("Unable to find Object \"" + in + "\" because of an uncaught exception: " + e); + } + } + + @Override + public boolean supports(Class type) { + return type.equals(IrisObject.class); + } + + @Override + public String getRandomDefault() { + return "object"; + } +} diff --git a/src/main/java/com/volmit/iris/util/plugin/VolmitSender.java b/src/main/java/com/volmit/iris/util/plugin/VolmitSender.java index 39731ee1e..504f3a381 100644 --- a/src/main/java/com/volmit/iris/util/plugin/VolmitSender.java +++ b/src/main/java/com/volmit/iris/util/plugin/VolmitSender.java @@ -413,60 +413,64 @@ public class VolmitSender implements CommandSender { } for (VirtualDecreeCommand i : v.getNodes()) { - if (isPlayer()) { - //@builder - String s = ( - " "<#42ecf5>" + f).toString(", ") + "\n" - + "<#3fe05a>✎ <#6ad97d>" + i.getDescription() + "\n" - + "<#bbe03f>✒ <#a8e0a2>" + (i.isNode() - ? ((i.getNode().getParameters().isEmpty() - ? "There are no parameters." - : "Hover over all of the parameters to learn more." + "\n")) - : "This is a command category. Run <#98eda5>" + i.getPath()) - + (i.isNode() - ? (i.getNode().getParameters().isNotEmpty()) - ? "<#aebef2>✦ <#5ef288>" - + i.getParentPath() - + " <#42ecf5>" - + i.getName() + " " - + i.getNode().getParameters().convert((f) - -> "<#d665f0>" + f.example()) - .toString(" ") + "\n" - : "" - : "") - + (i.isNode() ? "" + pickRandoms(Math.min(i.getNode().getParameters().size() + 1, 5), i) + "" : "") - + "'>" - + "<#46826a>⇀ " + i.getName() + "" - + (i.isNode() ? - " " + i.getNode().getParameters().convert((f) - -> " "<#d665f0>" + ff).toString(", ") + "\n" - + "<#3fe05a>✎ <#6ad97d>" + f.getDescription() + "\n" - + (f.isRequired() - ? "<#db4321>⚠ <#faa796>This parameter is required." - : (f.hasDefault() - ? "<#2181db>✔ <#78dcf0>Defaults to \""+f.getParam().defaultValue()+"\" if undefined." - : "<#a73abd>✔ <#78dcf0>This parameter is optional.")) + "\n" - + (f.isContextual() ? "<#ff9900>➱ <#ffcc00>The value may be derived from environment context \n" : "") - + "<#cc00ff>✢ <#ff33cc>This parameter is of type " + f.getType().getSimpleName() - + "'>" - + (f.isRequired() ? "[" : "") - + "" + f.getName() - + (f.isRequired() ? "]" : "") - + "").toString(" ") - : " - Category of Commands" - ) - ); - //@done - sendMessageRaw(s); - System.out.println(s); - } else { - sendMessage(i.getPath() + "()"); - } + sendDecreeHelpNode(i); } } else { sendMessage(C.RED + "There are no subcommands in this group! Contact support, this is a command design issue!"); } } + + public void sendDecreeHelpNode(VirtualDecreeCommand i){ + if (isPlayer()) { + //@builder + String s = ( + " "<#42ecf5>" + f).toString(", ") + "\n" + + "<#3fe05a>✎ <#6ad97d>" + i.getDescription() + "\n" + + "<#bbe03f>✒ <#a8e0a2>" + (i.isNode() + ? ((i.getNode().getParameters().isEmpty() + ? "There are no parameters." + : "Hover over all of the parameters to learn more." + "\n")) + : "This is a command category. Run <#98eda5>" + i.getPath()) + + (i.isNode() + ? (i.getNode().getParameters().isNotEmpty()) + ? "<#aebef2>✦ <#5ef288>" + + i.getParentPath() + + " <#42ecf5>" + + i.getName() + " " + + i.getNode().getParameters().convert((f) + -> "<#d665f0>" + f.example()) + .toString(" ") + "\n" + : "" + : "") + + (i.isNode() ? "" + pickRandoms(Math.min(i.getNode().getParameters().size() + 1, 5), i) + "" : "") + + "'>" + + "<#46826a>⇀ " + i.getName() + "" + + (i.isNode() ? + " " + i.getNode().getParameters().convert((f) + -> " "<#d665f0>" + ff).toString(", ") + "\n" + + "<#3fe05a>✎ <#6ad97d>" + f.getDescription() + "\n" + + (f.isRequired() + ? "<#db4321>⚠ <#faa796>This parameter is required." + : (f.hasDefault() + ? "<#2181db>✔ <#78dcf0>Defaults to \""+f.getParam().defaultValue()+"\" if undefined." + : "<#a73abd>✔ <#78dcf0>This parameter is optional.")) + "\n" + + (f.isContextual() ? "<#ff9900>➱ <#ffcc00>The value may be derived from environment context \n" : "") + + "<#cc00ff>✢ <#ff33cc>This parameter is of type " + f.getType().getSimpleName() + + "'>" + + (f.isRequired() ? "[" : "") + + "" + f.getName() + + (f.isRequired() ? "]" : "") + + "").toString(" ") + : " - Category of Commands" + ) + ); + //@done + sendMessageRaw(s); + System.out.println(s); + } else { + sendMessage(i.getPath() + "()"); + } + } }