Skip to content

Commit

Permalink
Add GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
lewmilburn committed Jun 19, 2024
1 parent fae8b75 commit be59e54
Show file tree
Hide file tree
Showing 8 changed files with 280 additions and 25 deletions.
6 changes: 5 additions & 1 deletion dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>net.lewmc</groupId>
<artifactId>kryptonite</artifactId>
<name>Kryptonite</name>
<version>1.3.0</version>
<version>1.4.0-SNAPSHOT</version>
<build>
<resources>
<resource>
Expand Down Expand Up @@ -52,6 +52,10 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
</repositories>
<dependencies>
<dependency>
Expand Down
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.lewmc</groupId>
<artifactId>kryptonite</artifactId>
<version>1.3.0</version>
<version>1.4.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Kryptonite</name>
Expand Down Expand Up @@ -66,6 +66,10 @@
<id>sonatype</id>
<url>https://oss.sonatype.org/content/groups/public/</url>
</repository>
<repository>
<id>minebench-repo</id>
<url>https://repo.minebench.de/</url>
</repository>
</repositories>

<dependencies>
Expand All @@ -86,5 +90,11 @@
<artifactId>ConfigUpdater</artifactId>
<version>2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>de.themoep</groupId>
<artifactId>inventorygui</artifactId>
<version>1.6.2-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
27 changes: 20 additions & 7 deletions src/main/java/net/lewmc/kryptonite/commands/OptimiseCommand.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package net.lewmc.kryptonite.commands;

import de.themoep.inventorygui.InventoryGui;
import de.themoep.inventorygui.StaticGuiElement;
import net.lewmc.kryptonite.Kryptonite;
import net.lewmc.kryptonite.kos.KOS;
import net.lewmc.kryptonite.kos.gui.KosMainGui;
import net.lewmc.kryptonite.utils.MessageUtil;
import net.lewmc.kryptonite.utils.PermissionUtil;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

import java.util.Objects;

Expand Down Expand Up @@ -42,25 +50,30 @@ public boolean onCommand(CommandSender commandSender, Command command, String s,
message.Info("You'll need to restart the server after completion for changes to be made.");
message.Info("");

KOS kos = new KOS(commandSender, this.plugin);
KOS kos = new KOS(commandSender, this.plugin, this.plugin.getConfig().getString("kos.profile"));
kos.runDefault(true);
} else if (Objects.equals(args[0].toLowerCase(), "no") && perm.isOperator(commandSender)) {
message.Info("Kryptonite will now run it's optimisation system.");
message.Info("You should backup your server before running Kryptonite.");
message.Info("You'll need to restart the server after completion for changes to be made.");
message.Info("");

KOS kos = new KOS(commandSender, this.plugin);
KOS kos = new KOS(commandSender, this.plugin, this.plugin.getConfig().getString("kos.profile"));
kos.runDefault(false);
} else {
message.Error("Unknown command. Use /kos for help.");
}
} else {
message.Info("Have you pregenerated your world and set a vanilla world border?");
message.Info("This will affect which optimisations can be applied.");
message.Info("");
message.Info("To continue enter '/kos yes' if you have pregenerated");
message.Info("or '/kos no' if you have not.");
if (commandSender instanceof Player) {
KosMainGui gui = new KosMainGui(this.plugin, commandSender);
gui.show();
} else {
message.Info("Have you pregenerated your world and set a vanilla world border?");
message.Info("This will affect which optimisations can be applied.");
message.Info("");
message.Info("To continue enter '/kos yes' if you have pregenerated");
message.Info("or '/kos no' if you have not.");
}
}
} else {
message.Error("You do not have the required permissions to run this command.");
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/net/lewmc/kryptonite/kos/KOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,29 @@ public class KOS {
private final LogUtil log;
private final YamlConfiguration kosconfig;
private YamlConfiguration patches;
private final String profile;

public KOS(CommandSender cs, Kryptonite plugin) {
public KOS(CommandSender cs, Kryptonite plugin, String profile) {
this.plugin = plugin;
this.message = new MessageUtil(cs);
this.log = new LogUtil(this.plugin);
this.softwareUtil = new SoftwareUtil(this.plugin);
this.profile = profile;

ConfigurationUtil config = new ConfigurationUtil(this.plugin, cs);

this.kosconfig = config.load("plugins/Kryptonite/config.yml");

File f = new File("plugins/Kryptonite/profiles/"+kosconfig.getString("kos.profile")+".kos");
File f = new File("plugins/Kryptonite/profiles/" + profile + ".kos");
if (f.exists()) {
this.patches = config.load("plugins/Kryptonite/profiles/" + kosconfig.getString("kos.profile") + ".kos");
this.patches = config.load("plugins/Kryptonite/profiles/" + profile + ".kos");
}
}

public void runDefault(boolean pregeneratedWorld) {
File f = new File("plugins/Kryptonite/profiles/"+kosconfig.getString("kos.profile")+".kos");
File f = new File("plugins/Kryptonite/profiles/"+this.profile+".kos");
if (f.exists()) {
this.message.Success("Running the Kryptonite Optimisation System using the '"+kosconfig.getString("kos.profile")+"' profile.");
this.message.Success("Running the Kryptonite Optimisation System using the '"+this.profile+"' profile.");

this.runVanilla();
this.runCraftBukkit();
Expand Down Expand Up @@ -70,9 +72,9 @@ public void runDefault(boolean pregeneratedWorld) {
this.cantOpenConfig(e);
}
} else {
this.log.severe("Unable to load the '"+kosconfig.getString("kos.profile")+"' profile.");
this.log.severe("Unable to load the '"+this.profile+"' profile.");
this.log.severe("Please verify that the file exists and try again.");
this.log.severe("plugins/Kryptonite/profiles/"+kosconfig.getString("kos.profile")+".kos");
this.log.severe("plugins/Kryptonite/profiles/"+this.profile+".kos");
}
}

Expand All @@ -82,10 +84,10 @@ private void runVanilla() {

ServerProperties properties = new ServerProperties();

properties.networkCompressionThreshold(this.patches.getInt("server.network-compression-threshold") + "");
properties.simulationDistance(this.patches.getInt("server.distance.simulation") + "");
properties.viewDistance(this.patches.getInt("server.distance.view") + "");
properties.syncChunkWrites(this.patches.getBoolean("server.sync-chunk-writes"));
properties.setNetworkCompressionThreshold(this.patches.getInt("server.network-compression-threshold") + "");
properties.setSimulationDistance(this.patches.getInt("server.distance.simulation") + "");
properties.setViewDistance(this.patches.getInt("server.distance.view") + "");
properties.setSyncChunkWrites(this.patches.getBoolean("server.sync-chunk-writes"));

properties.save();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,59 @@ public ServerProperties() {
this.util = new PropertiesUtil("server.properties");
}

public void networkCompressionThreshold(String value) {

public enum Config {
NETWORK_COMPRESSION_THRESHOLD {
public String toString() {
return "network-compression-threshold";
}
},
SIMULATION_DISTANCE {
public String toString() {
return "simulation-distance";
}
},
VIEW_DISTANCE {
public String toString() {
return "view-distance";
}
},
SYNC_CHUNK_WRITES {
public String toString() {
return "sync-chunk-writes";
}
}
}

public void set(Config key, String value) {
this.util.setProperty(key.toString(), value);
}

public String getString(Config key) {
return this.util.getProperty(key.toString());
}

public int getInt(Config key) {
return Integer.parseInt(this.util.getProperty(key.toString()));
}

@Deprecated
public void setNetworkCompressionThreshold(String value) {
this.util.setProperty("network-compression-threshold", value);
}

public void simulationDistance(String value) {
@Deprecated
public void setSimulationDistance(String value) {
this.util.setProperty("simulation-distance", value);
}

public void viewDistance(String value) {
@Deprecated
public void setViewDistance(String value) {
this.util.setProperty("view-distance", value);
}

public void syncChunkWrites(boolean value) {
@Deprecated
public void setSyncChunkWrites(boolean value) {
if (value) {
this.util.setProperty("sync-chunk-writes", "true");
} else {
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/net/lewmc/kryptonite/kos/gui/KosMainGui.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package net.lewmc.kryptonite.kos.gui;

import de.themoep.inventorygui.InventoryGui;
import de.themoep.inventorygui.StaticGuiElement;
import net.lewmc.kryptonite.Kryptonite;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;

public class KosMainGui {
private final CommandSender commandSender;
private final Kryptonite plugin;
private InventoryGui gui;

/**
* Constructor for the KosMainGui class.
* @param plugin Kryptonite - Reference to the main plugin class.
* @param commandSender CommandSender - the player who sent the command.
*/
public KosMainGui(Kryptonite plugin, CommandSender commandSender) {
this.plugin = plugin;
this.commandSender = commandSender;
}

/**
* Displays the GUI to the user.
*/
public void show() {
InventoryHolder holder = this.commandSender.getServer().getPlayer(this.commandSender.getName());
this.gui = new InventoryGui(this.plugin, holder, "KOS - Select a Profile", this.getElements());

this.gui.setFiller(new ItemStack(Material.GRAY_STAINED_GLASS_PANE, 1));
this.addElements();

this.gui.build();
this.gui.show((Player) this.commandSender);
}

/**
* Adds pre-programmed elements to the GUI
*/
private void addElements() {
this.gui.addElement(new StaticGuiElement('y',
new ItemStack(Material.OAK_SIGN),
1,
click -> {
click.getGui().close();
KosPregeneratedGui kpg = new KosPregeneratedGui(this.plugin, this.commandSender, "YouHaveTrouble");
kpg.show();
return true;
},
"YouHaveTrouble"
));

this.gui.addElement(new StaticGuiElement('h',
new ItemStack(Material.PAPER),
1,
click -> {
click.getWhoClicked().sendMessage(ChatColor.YELLOW+"https://wiki.lewmc.net/kr-kryptonite-optimisation-system.html");
click.getGui().close();
return true;
},
"Need help?",
"Click here to get a link to our Wiki."
));
}

/**
* Determines where the elements should be positioned.
* @return String[] - The position of the elements.
*/
private String[] getElements() {
return new String[]{
" y ",
" ",
" h "
};
}
}
Loading

0 comments on commit be59e54

Please sign in to comment.