used json files for saving config

This commit is contained in:
ImproperIssues
2023-07-16 21:27:50 -07:00
parent f616e762c1
commit c5a29b3eb2
46 changed files with 655 additions and 1204 deletions

View File

@@ -45,3 +45,9 @@ processResources {
expand props
}
}
compileJava.options.encoding = 'UTF-8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}

Binary file not shown.

View File

@@ -1,18 +0,0 @@
#
#
# This is ExplosionsControl by ImproperIssues
#
# Please note that all the explosions configurations have been moved to
# "plugins/ExplosionsControl/worldconfigurations" and are no longer
# configured through this config!
#
#
#
# If you do enjoy this plugin, please star it on github!
# Visit and star https://github.com/ItziSpyder/ExplosionsControl
#
#
config:
plugin:
prefix: '§7[§eExplosions§6Control§7] §'

View File

@@ -1,16 +1,7 @@
name: ExplosionsControl
version: '3.0'
main: me.improper.explosionscontrol.ExplosionsControl
main: io.github.itzispyder.explosionscontrol.ExplosionsControl
api-version: 1.17
authors: [ ImproperIssues ]
description: Manager server explosions with just a few simple clicks!
website: github.com/ItziSpyder
commands:
explosions:
description: Opens up the explosions toggle menu
usage: /explosions
permission: explosionscontrol.commands.explosions
loadworlds:
description: Loads in all the configuration files in case they didn't load
usage: /loadworlds
permission: explosionscontrol.commands.loadworlds

View File

@@ -0,0 +1,26 @@
package io.github.itzispyder.explosionscontrol;
import io.github.itzispyder.explosionscontrol.data.ExplosionConfig;
import io.github.itzispyder.explosionscontrol.utils.Text;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.logging.Logger;
public final class ExplosionsControl extends JavaPlugin {
public static final Logger logger = Bukkit.getLogger();
public static final String starter = Text.color("&7[&6Ex&eC&7] &r");
public static ExplosionsControl instance;
@Override
public void onEnable() {
instance = this;
ExplosionConfig.updateAllWorlds();
}
@Override
public void onDisable() {
ExplosionConfig.updateAllWorlds();
}
}

View File

@@ -0,0 +1,14 @@
package io.github.itzispyder.explosionscontrol.commands;
import org.bukkit.command.Command;
public record CmdExHandler(Exception ex, Command cmd) {
public String getHelp() {
String help = "§cError: §7";
if (ex instanceof IndexOutOfBoundsException) help += "Unknown or incomplete command.";
else if (ex instanceof NullPointerException) help += "Command contains a null value.";
else help += ex.getMessage();
return help + "\n§cCaused by: §7" + ex.getClass().getSimpleName() + "\n" + "§cUsage: §7" + cmd.getUsage();
}
}

View File

@@ -0,0 +1,60 @@
package io.github.itzispyder.explosionscontrol.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.util.*;
public class TabComplBuilder {
private Map<Integer,List<String>> entries = new HashMap<>();
private final CommandSender sender;
private final Command command;
private final String alias;
private final String[] args;
public TabComplBuilder(CommandSender sender, Command command, String alias, String[] args) {
this.sender = sender;
this.command = command;
this.alias = alias;
this.args = args;
}
/**
* Adds to the tab completion
* @param atIndex should be a number above or equal to 1
* @param entry string array
* @param condition condition
*/
public TabComplBuilder add(int atIndex, String[] entry, boolean condition) {
if (condition) add(atIndex,entry);
return this;
}
/**
* Adds to the tab completion
* @param atIndex should be a number above or equal to 1
* @param entry string array
*/
public TabComplBuilder add(int atIndex, String[] entry) {
atIndex = Math.max(1,atIndex);
entries.put(atIndex,Arrays.stream(entry).toList());
return this;
}
public TabComplBuilder add(int atIndex, List<String> entry, boolean condition) {
if (condition) add(atIndex,entry);
return this;
}
public TabComplBuilder add(int atIndex, List<String> entry) {
entries.put(atIndex,entry);
return this;
}
public List<String> build() {
List<String> list = new ArrayList<>(entries.get(args.length) != null ? entries.get(args.length) : new ArrayList<>());
list.removeIf(s -> !s.toLowerCase().contains(args[args.length - 1].toLowerCase()));
return list;
}
}

View File

@@ -0,0 +1,164 @@
package io.github.itzispyder.explosionscontrol.data;
import com.google.gson.Gson;
import io.github.itzispyder.explosionscontrol.utils.FileValidationUtils;
import org.bukkit.Bukkit;
import org.bukkit.World;
import java.io.*;
import static io.github.itzispyder.explosionscontrol.ExplosionsControl.instance;
import static io.github.itzispyder.explosionscontrol.ExplosionsControl.logger;
public class ExplosionConfig {
private final String world;
private Mode minecartMode;
private Mode witherMode;
private Mode tntMode;
private Mode fireballMode;
private Mode creeperMode;
private Mode blockMode;
private Mode crystalMode;
public ExplosionConfig(World world) {
this.world = world.getName();
this.minecartMode = Mode.ENABLED;
this.witherMode = Mode.ENABLED;
this.tntMode = Mode.ENABLED;
this.fireballMode = Mode.ENABLED;
this.creeperMode = Mode.ENABLED;
this.crystalMode = Mode.ENABLED;
this.blockMode = Mode.ENABLED;
}
public World getWorld() {
return Bukkit.getWorld(world);
}
public Mode getMinecartMode() {
return minecartMode;
}
public void setMinecartMode(Mode minecartMode) {
this.minecartMode = minecartMode;
}
public Mode getWitherMode() {
return witherMode;
}
public void setWitherMode(Mode witherMode) {
this.witherMode = witherMode;
}
public Mode getTntMode() {
return tntMode;
}
public void setTntMode(Mode tntMode) {
this.tntMode = tntMode;
}
public Mode getFireballMode() {
return fireballMode;
}
public void setFireballMode(Mode fireballMode) {
this.fireballMode = fireballMode;
}
public Mode getCreeperMode() {
return creeperMode;
}
public void setCreeperMode(Mode creeperMode) {
this.creeperMode = creeperMode;
}
public Mode getBlockMode() {
return blockMode;
}
public void setBlockMode(Mode blockMode) {
this.blockMode = blockMode;
}
public Mode getCrystalMode() {
return crystalMode;
}
public void setCrystalMode(Mode crystalMode) {
this.crystalMode = crystalMode;
}
public File getFile() {
return getFileOf(getWorld());
}
public void save() {
save(this);
}
public static File getFileOf(World world) {
return new File(instance.getDataFolder(), "worlds/" + world.getName() + ".json");
}
public static void updateAllWorlds() {
for (World world : Bukkit.getWorlds()) {
load(world).save();
}
}
private static <T> T getOrDef(T val, T def) {
return val != null ? val : def;
}
public static void save(ExplosionConfig config) {
File file = config.getFile();
if (FileValidationUtils.validate(file)) {
try {
Gson gson = new Gson();
String json = gson.toJson(config);
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(json);
bw.close();
}
catch (Exception ex) {
logger.warning("An error occurred while saving config for \"" + config.world + "\"");
ex.printStackTrace();
}
}
}
public static ExplosionConfig load(World world) {
return load(getFileOf(world));
}
public static ExplosionConfig load(File file) {
ExplosionConfig defaultConfig = new ExplosionConfig(Bukkit.getWorlds().get(0));
if (FileValidationUtils.validate(file)) {
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
Gson gson = new Gson();
String json = br.readLine();
ExplosionConfig config = json == null ? defaultConfig : gson.fromJson(json, ExplosionConfig.class);
br.close();
config.save();
return config;
}
catch (Exception ex) {
logger.warning("An error occurred while loading config for \"" + file.getPath() + "\"");
ex.printStackTrace();
}
}
return defaultConfig;
}
}

View File

@@ -0,0 +1,10 @@
package io.github.itzispyder.explosionscontrol.data;
public enum Mode {
ENABLED,
DISABLED,
NONE,
DYNAMIC
}

View File

@@ -0,0 +1,21 @@
package io.github.itzispyder.explosionscontrol.utils;
import java.io.File;
public final class FileValidationUtils {
public static boolean validate(File file) {
try {
if (!file.getParentFile().exists())
if (!file.getParentFile().mkdirs())
return false;
if (!file.exists())
if (!file.createNewFile())
return false;
return true;
}
catch (Exception ex) {
return false;
}
}
}

View File

@@ -1,13 +1,14 @@
package me.improper.explosionscontrol.other;
package io.github.itzispyder.explosionscontrol.utils;
import me.improper.explosionscontrol.ExplosionsControl;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
public class ServerSound {
import static io.github.itzispyder.explosionscontrol.ExplosionsControl.instance;
public class SoundPlayer {
private Location location;
private Sound sound;
@@ -24,7 +25,7 @@ public class ServerSound {
* @param volume float
* @param pitch float
*/
public ServerSound(Location location, Sound sound, float volume, float pitch) {
public SoundPlayer(Location location, Sound sound, float volume, float pitch) {
this.location = location;
this.sound = sound;
this.pitch = pitch;
@@ -57,7 +58,7 @@ public class ServerSound {
*/
public void playWithin(double distance) {
for (Player p : Bukkit.getOnlinePlayers()) {
if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) {
if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distance(this.location) < distance) {
p.playSound(this.location,this.sound,this.volume,this.pitch);
}
}
@@ -70,7 +71,7 @@ public class ServerSound {
*/
public void playWithinAt(double distance) {
for (Player p : Bukkit.getOnlinePlayers()) {
if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distanceSquared(this.location) < distance) {
if (p != null && p.getWorld() == this.location.getWorld() && p.getLocation().distance(this.location) < distance) {
p.playSound(p.getLocation(),this.sound,this.volume,this.pitch);
}
}
@@ -110,7 +111,7 @@ public class ServerSound {
this.cancel();
}
}
}.runTaskTimer(ExplosionsControl.getInstance(),0,tickDelay);
}.runTaskTimer(instance,0,tickDelay);
}
/**
@@ -132,7 +133,7 @@ public class ServerSound {
this.cancel();
}
}
}.runTaskTimer(ExplosionsControl.getInstance(),0,tickDelay);
}.runTaskTimer(instance,0,tickDelay);
}
/**
@@ -153,7 +154,7 @@ public class ServerSound {
this.cancel();
}
}
}.runTaskTimer(ExplosionsControl.getInstance(),0,tickDelay);
}.runTaskTimer(instance,0,tickDelay);
}
/**
@@ -174,7 +175,7 @@ public class ServerSound {
this.cancel();
}
}
}.runTaskTimer(ExplosionsControl.getInstance(),0,tickDelay);
}.runTaskTimer(instance,0,tickDelay);
}
/**
@@ -196,7 +197,7 @@ public class ServerSound {
this.cancel();
}
}
}.runTaskTimer(ExplosionsControl.getInstance(),0,tickDelay);
}.runTaskTimer(instance,0,tickDelay);
}
/**
@@ -218,7 +219,7 @@ public class ServerSound {
this.cancel();
}
}
}.runTaskTimer(ExplosionsControl.getInstance(),0,tickDelay);
}.runTaskTimer(instance,0,tickDelay);
}
public Sound getSound() {

View File

@@ -0,0 +1,96 @@
package io.github.itzispyder.explosionscontrol.utils;
import io.github.itzispyder.explosionscontrol.ExplosionsControl;
public class Text {
public static String of(String s) {
return s;
}
public static String ofAll(String s) {
return builder(s).prefix().color().build();
}
/**
* Replaces all & with § to color the text
* @param s string
* @return result
*/
public static String color(String s) {
return s.replaceAll("&","§");
}
public static String asPath(String s) {
return s.toLowerCase()
.replaceAll(" ","_")
.replaceAll("[^.a-b0-9_-]","")
.trim();
}
public static String asDirectory(String s) {
return s.toLowerCase()
.replaceAll(" ","_")
.replaceAll("[^./a-b0-9_-]","")
.trim();
}
public static String prefixed(String s) {
return ExplosionsControl.starter + s;
}
public static TextBuilder builder(String s) {
return new TextBuilder(s);
}
public static TextBuilder builder() {
return builder("");
}
public static String removeColors(String msg) {
String s = msg;
while (s.length() >= 2 && s.contains("§")) {
int index = s.indexOf("§");
s = s.replaceAll(s.substring(index, index + 2), "");
}
return s;
}
public static class TextBuilder {
private String s;
public TextBuilder(String s) {
this.s = s;
}
public TextBuilder message(String s) {
this.s = s;
return this;
}
public TextBuilder color() {
s = Text.color(s);
return this;
}
public TextBuilder prefix() {
s = Text.prefixed(s);
return this;
}
public TextBuilder asPath() {
s = Text.asPath(s);
return this;
}
public TextBuilder asDirectory() {
s = Text.asDirectory(s);
return this;
}
public String build() {
return s;
}
}
}

View File

@@ -1,51 +0,0 @@
package me.improper.explosionscontrol;
import me.improper.explosionscontrol.commands.Commands;
import me.improper.explosionscontrol.commands.Tabs;
import me.improper.explosionscontrol.data.Config;
import me.improper.explosionscontrol.data.ExplosionConfigFile;
import me.improper.explosionscontrol.data.ExplosionToggle;
import me.improper.explosionscontrol.events.OnExplode;
import me.improper.explosionscontrol.events.OnInventory;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
public final class ExplosionsControl extends JavaPlugin {
public static String STARTER;
@Override
public void onEnable() {
// Plugin startup logic
STARTER = Config.getPluginPrefix();
Bukkit.broadcastMessage(STARTER + "aExplosionsControls has loaded!");
ExplosionConfigFile.setup();
ExplosionToggle.setup();
// Files
getConfig().options().copyDefaults();
saveDefaultConfig();
// Events
Bukkit.getPluginManager().registerEvents(new OnExplode(),this);
Bukkit.getPluginManager().registerEvents(new OnInventory(),this);
// Commands
getCommand("explosions").setExecutor(new Commands());
getCommand("explosions").setTabCompleter(new Tabs());
getCommand("loadworlds").setExecutor(new Commands());
getCommand("loadworlds").setTabCompleter(new Tabs());
}
@Override
public void onDisable() {
// Plugin shutdown logic
Bukkit.broadcastMessage(STARTER + "cExplosionsControls has disabled! If this isn't a reload please consider restarting" +
" as your server is no longer being protected from explosions!");
}
public static Plugin getInstance() {
return Bukkit.getPluginManager().getPlugin("ExplosionsControl");
}
}

View File

@@ -1,43 +0,0 @@
package me.improper.explosionscontrol.commands;
import me.improper.explosionscontrol.ExplosionsControl;
import me.improper.explosionscontrol.data.ExplosionConfigFile;
import me.improper.explosionscontrol.data.ExplosionToggle;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class Commands implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String commandName = command.getName().toLowerCase().trim();
try {
switch (commandName) {
case "explosions" -> {
((Player) sender).openInventory(ExplosionToggle.EXPLOSIONMENU);
return true;
}
case "loadworlds" -> {
ExplosionConfigFile.setup();
sender.sendMessage(ExplosionsControl.STARTER + "6Reloaded all world explosions configurations!");
for (World world : Bukkit.getWorlds()) sender.sendMessage(ChatColor.GOLD + " - " + ChatColor.YELLOW + world.getName());
return true;
}
}
} catch (Exception exception) {
String message = ExplosionsControl.STARTER + ChatColor.DARK_RED + "Command error: " + ChatColor.RED;
if (exception instanceof NullPointerException) message += "Command contains a null value!";
else if (exception instanceof IndexOutOfBoundsException) message += "Incomplete command! Not enough information was provided!";
else message += exception.getMessage();
sender.sendMessage(message);
return true;
}
return false;
}
}

View File

@@ -1,25 +0,0 @@
package me.improper.explosionscontrol.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import java.util.ArrayList;
import java.util.List;
public class Tabs implements TabCompleter {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
List<String> list = new ArrayList<>();
String commandName = command.getName().toLowerCase().trim();
switch (commandName) {
case "explosions","loadworlds" -> {
break;
}
}
list.removeIf(i -> !i.toLowerCase().contains(args[args.length - 1].toLowerCase()));
return list;
}
}

View File

@@ -1,19 +0,0 @@
package me.improper.explosionscontrol.data;
import me.improper.explosionscontrol.ExplosionsControl;
import org.bukkit.configuration.file.FileConfiguration;
public class Config {
private static FileConfiguration CONFIG = ExplosionsControl.getInstance().getConfig();
/**
* This will return the current plugin prefix set
* in the plugin's configuration file.
*
* @return The plugin prefix
*/
public static String getPluginPrefix() {
return CONFIG.getString("config.plugin.prefix");
}
}

View File

@@ -1,18 +0,0 @@
package me.improper.explosionscontrol.data;
import org.bukkit.Bukkit;
import org.bukkit.World;
public class ExplosionConfigFile {
/**
* Creates a configuration for each existing world.
* These are saved to "plugins/ExplosionsControl/worldconfigurations".
*/
public static void setup() {
for (World world : Bukkit.getWorlds()) {
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
configuration.save();
}
}
}

View File

@@ -1,139 +0,0 @@
package me.improper.explosionscontrol.data;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.io.Serializable;
public class ExplosionConfiguration implements Serializable {
private String world;
private ExplosionMode allowTnt, allowCrystal, allowFireball, allowCreeper, allowMinecart, allowWither, allowBlock;
/**
* Constructs a world configuration for explosions.
* Note that if the data file is null or does not exist it will create
* a new instance of this class instead of loading the data.
*
* @param world World
*/
public ExplosionConfiguration(World world) {
try {
File file = new File("plugins/ExplosionsControl/worldconfigurations/" + world.getName() + ".yml");
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
if (!file.exists()) {
file.createNewFile();
this.world = world.getName();
this.allowTnt = ExplosionMode.ENABLED;
this.allowCrystal = ExplosionMode.ENABLED;
this.allowFireball = ExplosionMode.ENABLED;
this.allowCreeper = ExplosionMode.ENABLED;
this.allowMinecart = ExplosionMode.ENABLED;
this.allowWither = ExplosionMode.ENABLED;
this.allowBlock = ExplosionMode.ENABLED;
return;
}
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
this.world = data.getString("worldconfig.world");
this.allowTnt = ExplosionMode.valueOf(data.getString("worldconfig.allowTnt").toUpperCase());
this.allowCrystal = ExplosionMode.valueOf(data.getString("worldconfig.allowCrystal").toUpperCase());
this.allowFireball = ExplosionMode.valueOf(data.getString("worldconfig.allowFireball").toUpperCase());
this.allowCreeper = ExplosionMode.valueOf(data.getString("worldconfig.allowCreeper").toUpperCase());
this.allowMinecart = ExplosionMode.valueOf(data.getString("worldconfig.allowMinecart").toUpperCase());
this.allowWither = ExplosionMode.valueOf(data.getString("worldconfig.allowWither").toUpperCase());
this.allowBlock = ExplosionMode.valueOf(data.getString("worldconfig.allowBlock").toUpperCase());
} catch (Exception exception) {
Bukkit.getLogger().warning(exception.toString());
}
}
/**
* Save the current configuration to a file config.
*/
public void save() {
try {
File file = new File("plugins/ExplosionsControl/worldconfigurations/" + world + ".yml");
if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
if (!file.exists()) file.createNewFile();
FileConfiguration data = YamlConfiguration.loadConfiguration(file);
data.set("worldconfig.world",world);
data.set("worldconfig.allowBlock",allowBlock.name());
data.set("worldconfig.allowCreeper",allowCreeper.name());
data.set("worldconfig.allowCrystal",allowCrystal.name());
data.set("worldconfig.allowFireball",allowFireball.name());
data.set("worldconfig.allowMinecart",allowMinecart.name());
data.set("worldconfig.allowTnt",allowTnt.name());
data.set("worldconfig.allowWither",allowWither.name());
data.save(file);
} catch (Exception exception) {
Bukkit.getLogger().warning(exception.toString());
}
}
public void setWorld(String world) {
this.world = world;
}
public void setAllowBlock(ExplosionMode allowBlock) {
this.allowBlock = allowBlock;
}
public void setAllowCreeper(ExplosionMode allowCreeper) {
this.allowCreeper = allowCreeper;
}
public void setAllowCrystal(ExplosionMode allowCrystal) {
this.allowCrystal = allowCrystal;
}
public void setAllowFireball(ExplosionMode allowFireball) {
this.allowFireball = allowFireball;
}
public void setAllowMinecart(ExplosionMode allowMinecart) {
this.allowMinecart = allowMinecart;
}
public void setAllowTnt(ExplosionMode allowTnt) {
this.allowTnt = allowTnt;
}
public void setAllowWither(ExplosionMode allowWither) {
this.allowWither = allowWither;
}
public String getWorld() {
return world;
}
public ExplosionMode getAllowBlock() {
return allowBlock;
}
public ExplosionMode getAllowCreeper() {
return allowCreeper;
}
public ExplosionMode getAllowCrystal() {
return allowCrystal;
}
public ExplosionMode getAllowFireball() {
return allowFireball;
}
public ExplosionMode getAllowMinecart() {
return allowMinecart;
}
public ExplosionMode getAllowTnt() {
return allowTnt;
}
public ExplosionMode getAllowWither() {
return allowWither;
}
}

View File

@@ -1,44 +0,0 @@
package me.improper.explosionscontrol.data;
import java.io.Serializable;
public enum ExplosionMode implements Serializable {
ENABLED(0),
DISABLED(1),
DYNAMIC(2),
NONE(3);
/**
* Attempts to get an explosion mode from the index value provided.
*
* @param index int
* @return An explosion mode from the index value.
*/
public static ExplosionMode fromIndex(int index) {
for (ExplosionMode mode : ExplosionMode.values()) if (index == mode.getIndex()) return mode;
return fromIndex(0);
}
private final int index;
/**
* Constructs an explosion mode from the index provided.
*
* @param index int
*/
ExplosionMode(int index) {
this.index = index;
}
/**
* Returns the index of the current explosion mode value.
* A number out of bounds will be read as 0!
*
* @return The index of the current value
*/
public int getIndex() {
if (index >= ExplosionMode.values().length || index < 0) return 0;
return index;
}
}

View File

@@ -1,110 +0,0 @@
package me.improper.explosionscontrol.data;
import me.improper.explosionscontrol.ExplosionsControl;
import me.improper.explosionscontrol.other.Item;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class ExplosionToggle {
public static Inventory EXPLOSIONMENU;
/**
* This will prepare the ExplosionsControl main toggle menu.
* Server staff members will be able to edit explosion configurations
* through this menu!
*/
public static void setup() {
Inventory menu = Bukkit.createInventory(null,54, ExplosionsControl.STARTER + "eConfigurations");
Item reload = new Item(new ItemStack(Material.COMPASS));
reload.setDisplayName(ChatColor.AQUA + "Load Worlds");
Item blank = new Item(new ItemStack(Material.DARK_OAK_SIGN));
blank.setDisplayName(ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + "Blank World");
Item b = new Item(new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
b.setDisplayName(" ");
Item o = new Item(new ItemStack(Material.ORANGE_STAINED_GLASS_PANE));
o.setDisplayName(" ");
Item y = new Item(new ItemStack(Material.YELLOW_STAINED_GLASS_PANE));
y.setDisplayName(" ");
Item a = new Item(new ItemStack(Material.AIR));
menu.setContents(new ItemStack[]{
y,y,y,y,y,y,y,y,reload,
b,b,b,b,b,b,b,b,b,
o,a,a,a,a,a,a,a,o,
o,a,a,a,a,a,a,a,o,
o,a,a,a,a,a,a,a,o,
o,o,o,o,o,o,o,o,o
});
for (World world : Bukkit.getWorlds()) {
Item worldIcon = new Item(new ItemStack(Material.OAK_SIGN));
worldIcon.setDisplayName(ChatColor.GRAY + world.getName());
worldIcon.setLore(List.of(ChatColor.DARK_GRAY + "" + ChatColor.ITALIC + "(Click to config)"));
menu.setItem(menu.firstEmpty(),worldIcon);
}
while (menu.firstEmpty() != -1) menu.setItem(menu.firstEmpty(),blank);
EXPLOSIONMENU = menu;
}
/**
* Open the toggle configuration menu for a player!
*
* @param player Player
* @param world World
*/
public static void openToggleMenu(Player player, World world) {
Inventory menu = Bukkit.createInventory(null,54, ExplosionsControl.STARTER + "eEditing " + world.getName());
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
Item b = new Item(new ItemStack(Material.BLACK_STAINED_GLASS_PANE));
b.setDisplayName(" ");
Item o = new Item(new ItemStack(Material.ORANGE_STAINED_GLASS_PANE));
o.setDisplayName(" ");
Item y = new Item(new ItemStack(Material.YELLOW_STAINED_GLASS_PANE));
y.setDisplayName(" ");
Item blank = new Item(new ItemStack(Material.LIGHT_GRAY_STAINED_GLASS_PANE));
blank.setDisplayName(" ");
Item a = new Item(new ItemStack(Material.AIR));
Item back = new Item(new ItemStack(Material.ARROW));
back.setDisplayName("<< Back to world menu");
// toggles
Item creeper = new Item(new ItemStack(Material.CREEPER_HEAD));
creeper.setDisplayName(configuration.getAllowCreeper().name());
Item crystal = new Item(new ItemStack(Material.END_CRYSTAL));
crystal.setDisplayName(configuration.getAllowCrystal().name());
Item tnt = new Item(new ItemStack(Material.TNT));
tnt.setDisplayName(configuration.getAllowTnt().name());
Item minecart = new Item(new ItemStack(Material.TNT_MINECART));
minecart.setDisplayName(configuration.getAllowMinecart().name());
Item anchor = new Item(new ItemStack(Material.RESPAWN_ANCHOR));
anchor.setDisplayName(configuration.getAllowBlock().name());
Item fireball = new Item(new ItemStack(Material.FIRE_CHARGE));
fireball.setDisplayName(configuration.getAllowFireball().name());
Item wither = new Item(new ItemStack(Material.WITHER_SKELETON_SKULL));
wither.setDisplayName(configuration.getAllowWither().name());
menu.setContents(new ItemStack[]{
back,y,y,y,y,y,y,y,y,
b,b,b,b,b,b,b,b,b,
o,a,a,a,a,a,a,a,o,
o,creeper,crystal,tnt,minecart,anchor,fireball,wither,o,
o,a,a,a,a,a,a,a,o,
o,o,o,o,o,o,o,o,o
});
while (menu.firstEmpty() != -1) menu.setItem(menu.firstEmpty(),blank);
player.openInventory(menu);
}
}

View File

@@ -1,216 +0,0 @@
package me.improper.explosionscontrol.events;
import me.improper.explosionscontrol.data.ExplosionConfiguration;
import me.improper.explosionscontrol.data.ExplosionMode;
import me.improper.explosionscontrol.other.ServerSound;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.util.Vector;
public class OnExplode implements Listener {
@EventHandler
public static void EntityExplodeEvent(EntityExplodeEvent e) {
Entity entity = e.getEntity();
Location loc = e.getLocation();
World world = loc.getWorld();
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
switch (entity.getType()) {
case PRIMED_TNT -> {
switch (configuration.getAllowTnt()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block block : e.blockList()) launchBlock(block, loc);
}
}
}
case FIREBALL,DRAGON_FIREBALL,SMALL_FIREBALL -> {
switch (configuration.getAllowFireball()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block block : e.blockList()) launchBlock(block, loc);
}
}
}
case CREEPER -> {
switch (configuration.getAllowCreeper()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block block : e.blockList()) launchBlock(block, loc);
}
}
}
case ENDER_CRYSTAL -> {
switch (configuration.getAllowCrystal()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block block : e.blockList()) launchBlock(block, loc);
}
}
}
case MINECART_TNT -> {
switch (configuration.getAllowMinecart()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block block : e.blockList()) launchBlock(block, loc);
}
}
}
case WITHER_SKULL -> {
switch (configuration.getAllowWither()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block block : e.blockList()) launchBlock(block, loc);
}
}
}
}
}
@EventHandler
public static void BlockExplodeEvent(BlockExplodeEvent e) {
Block block = e.getBlock();
Location loc = block.getLocation();
World world = loc.getWorld();
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
switch (configuration.getAllowBlock()) {
case DISABLED -> {
e.setCancelled(true);
fakeExplode(loc);
}
case NONE -> {
e.setCancelled(true);
}
case DYNAMIC -> {
for (Block b : e.blockList()) launchBlock(b, loc);
}
}
}
@EventHandler
public static void EntityDamageByEntityEvent(EntityDamageByEntityEvent e) {
try {
Entity damager = e.getDamager();
Location loc = e.getDamager().getLocation();
World world = loc.getWorld();
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
switch (damager.getType()) {
case PRIMED_TNT -> {
if (configuration.getAllowTnt().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
case FIREBALL,DRAGON_FIREBALL,SMALL_FIREBALL -> {
if (configuration.getAllowFireball().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
case CREEPER -> {
if (configuration.getAllowCreeper().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
case ENDER_CRYSTAL -> {
if (configuration.getAllowCrystal().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
case MINECART_TNT -> {
if (configuration.getAllowMinecart().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
case WITHER_SKULL -> {
if (configuration.getAllowWither().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
}
} catch (Exception exception) {}
}
@EventHandler
public static void EntityDamageEvent(EntityDamageEvent e) {
try {
Entity entity = e.getEntity();
Location loc = entity.getLocation();
World world = loc.getWorld();
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
EntityDamageEvent.DamageCause cause = e.getCause();
switch (cause) {
case BLOCK_EXPLOSION -> {
if (configuration.getAllowBlock().equals(ExplosionMode.NONE)) e.setCancelled(true);
}
}
} catch (Exception exception) {}
}
/**
* Attempts to launch a block as a falling block according to its original location
* This feature is in beta, expect many bugs!
*
* @param block Block
* @param origin Location
*/
public static void launchBlock(Block block, Location origin) {
Location loc = block.getLocation();
Vector vector = origin
.toVector()
.subtract(loc.toVector())
.normalize()
.add(new Vector(0,1,0))
.multiply(new Vector(-0.7,0.7,-0.7));
FallingBlock fb = origin.getWorld().spawnFallingBlock(block.getLocation(),block.getBlockData());
fb.setVelocity(vector);
block.setType(Material.AIR);
}
/**
* Plays a sound and displays the explosion particles at the provided world location.
*
* @param location Location
*/
public static void fakeExplode(Location location) {
ServerSound explosion = new ServerSound(location,Sound.ENTITY_GENERIC_EXPLODE,10,0.7F);
explosion.playWithin(5000);
location.getWorld().spawnParticle(Particle.EXPLOSION_LARGE,location,1,1,1,1,0);
location.getWorld().spawnParticle(Particle.EXPLOSION_HUGE,location,1,1,1,1,0);
location.getWorld().spawnParticle(Particle.EXPLOSION_NORMAL,location,5,1,1,1,0);
}
}

View File

@@ -1,118 +0,0 @@
package me.improper.explosionscontrol.events;
import me.improper.explosionscontrol.ExplosionsControl;
import me.improper.explosionscontrol.data.ExplosionConfigFile;
import me.improper.explosionscontrol.data.ExplosionConfiguration;
import me.improper.explosionscontrol.data.ExplosionMode;
import me.improper.explosionscontrol.data.ExplosionToggle;
import me.improper.explosionscontrol.other.Item;
import me.improper.explosionscontrol.other.ServerSound;
import org.bukkit.*;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
public class OnInventory implements Listener {
@EventHandler
public static void InventoryClickEvent(InventoryClickEvent e) {
Player p = (Player) e.getWhoClicked();
Inventory inv = e.getClickedInventory();
String title = e.getView().getTitle();
try {
Item item = new Item(e.getCurrentItem());
ServerSound reload = new ServerSound(p.getLocation(),Sound.BLOCK_ENCHANTMENT_TABLE_USE,10,10);
ServerSound click = new ServerSound(p.getLocation(),Sound.UI_BUTTON_CLICK,10,1);
ServerSound edit = new ServerSound(p.getLocation(),Sound.ITEM_DYE_USE,10,10);
if (title.contains(ExplosionsControl.STARTER + "eConfigurations")) {
if (inv.getType().equals(InventoryType.PLAYER)) return;
e.setCancelled(true);
if (item.getDisplayName().equals(" ")) return;
if (item.getDisplayName().equals(ChatColor.AQUA + "Load Worlds")) {
p.closeInventory();
ExplosionConfigFile.setup();
ExplosionToggle.setup();
p.openInventory(ExplosionToggle.EXPLOSIONMENU);
reload.play(p);
return;
}
if (item.getType().equals(Material.DARK_OAK_SIGN)) return;
if (item.getType().equals(Material.OAK_SIGN)) {
World world = Bukkit.getWorld(item.getDisplayName().replaceAll(ChatColor.GRAY + "",""));
ExplosionToggle.openToggleMenu(p,world);
click.play(p);
}
}
if (title.contains(ExplosionsControl.STARTER + "eEditing ")) {
if (inv.getType().equals(InventoryType.PLAYER)) return;
e.setCancelled(true);
if (item.getDisplayName().equals(" ")) return;
if (item.getType().equals(Material.DARK_OAK_SIGN)) return;
if (item.getDisplayName().equals("<< Back to world menu")) {
p.openInventory(ExplosionToggle.EXPLOSIONMENU);
click.play(p);
return;
}
World world = Bukkit.getWorld(title.substring((ExplosionsControl.STARTER + "eEditing ").length()));
ExplosionConfiguration configuration = new ExplosionConfiguration(world);
switch (item.getType()) {
case FIRE_CHARGE -> {
configuration.setAllowFireball(ExplosionMode.fromIndex(configuration.getAllowFireball().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
case CREEPER_HEAD -> {
configuration.setAllowCreeper(ExplosionMode.fromIndex(configuration.getAllowCreeper().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
case TNT -> {
configuration.setAllowTnt(ExplosionMode.fromIndex(configuration.getAllowTnt().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
case TNT_MINECART -> {
configuration.setAllowMinecart(ExplosionMode.fromIndex(configuration.getAllowMinecart().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
case WITHER_SKELETON_SKULL -> {
configuration.setAllowWither(ExplosionMode.fromIndex(configuration.getAllowWither().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
case END_CRYSTAL -> {
configuration.setAllowCrystal(ExplosionMode.fromIndex(configuration.getAllowCrystal().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
case RESPAWN_ANCHOR -> {
configuration.setAllowBlock(ExplosionMode.fromIndex(configuration.getAllowBlock().getIndex() + 1));
configuration.save();
ExplosionToggle.setup();
ExplosionToggle.openToggleMenu(p,world);
edit.play(p);
}
}
}
} catch (Exception exception) {}
}
}

View File

@@ -1,110 +0,0 @@
package me.improper.explosionscontrol.other;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;
import java.util.Set;
public class Item extends ItemStack {
private ItemMeta meta = super.getItemMeta();
/**
* This creates in instance of this class, which is an extension
* or the ItemStack class, aiming to simplify the process of
* modifying an item's item meta.
*
* @param itemStack ItemStack
*/
public Item(ItemStack itemStack) {
super(itemStack);
}
/**
* Set the display name of an item.
*
* @param displayName String
*/
public void setDisplayName(String displayName) {
meta.setDisplayName(displayName);
super.setItemMeta(meta);
}
/**
* Set the lore of an item.
*
* @param lore List<String></String>
*/
public void setLore(List<String> lore) {
meta.setLore(lore);
super.setItemMeta(meta);
}
/**
* Adds item flags to an item.
*
* @param flag ItemFlag...
*/
public void addItemFlags(ItemFlag... flag) {
meta.addItemFlags(flag);
super.setItemMeta(meta);
}
/**
* Sets the unbreakable state of an item.
*
* @param unbreakable boolean
*/
public void setUnbreakable(boolean unbreakable) {
meta.setUnbreakable(unbreakable);
super.setItemMeta(meta);
}
/**
* Sets the custom model data of an item.
*
* @param customModelData String
*/
public void setCustomModelData(int customModelData) {
meta.setCustomModelData(customModelData);
super.setItemMeta(meta);
}
/**
* Returns the display name of the item.
*
* @return A string as the display name.
*/
public String getDisplayName() {
return meta.getDisplayName();
}
/**
* Returns the lore of the item.
*
* @return A string array as the lore.
*/
public List<String> getLore() {
return meta.getLore();
}
/**
* Returns an array of the items flags of the item.
*
* @return An array of the item flags.
*/
public Set<ItemFlag> getItemFlags() {
return meta.getItemFlags();
}
/**
* Returns the custom model data of the item.
*
* @return The custom model data of the item.
*/
public int getCustomModelData() {
return meta.getCustomModelData();
}
}

View File

@@ -1,18 +0,0 @@
#
#
# This is ExplosionsControl by ImproperIssues
#
# Please note that all the explosions configurations have been moved to
# "plugins/ExplosionsControl/worldconfigurations" and are no longer
# configured through this config!
#
#
#
# If you do enjoy this plugin, please star it on github!
# Visit and star https://github.com/ItziSpyder/ExplosionsControl
#
#
config:
plugin:
prefix: '§7[§eExplosions§6Control§7] §'

View File

@@ -1,16 +1,7 @@
name: ExplosionsControl
version: '${version}'
main: me.improper.explosionscontrol.ExplosionsControl
main: io.github.itzispyder.explosionscontrol.ExplosionsControl
api-version: 1.17
authors: [ ImproperIssues ]
description: Manager server explosions with just a few simple clicks!
website: github.com/ItziSpyder
commands:
explosions:
description: Opens up the explosions toggle menu
usage: /explosions
permission: explosionscontrol.commands.explosions
loadworlds:
description: Loads in all the configuration files in case they didn't load
usage: /loadworlds
permission: explosionscontrol.commands.loadworlds