Moved some Utility to Util Class

This commit is contained in:
Sn0wStorm 2019-08-18 15:42:22 +02:00
parent 9567feed81
commit 930d4a2028
9 changed files with 171 additions and 141 deletions

View File

@ -181,7 +181,7 @@ public class BCauldron {
}
public static void save(ConfigurationSection config, ConfigurationSection oldData) {
P.p.createWorldSections(config);
Util.createWorldSections(config);
if (!bcauldrons.isEmpty()) {
int id = 0;
@ -190,7 +190,7 @@ public class BCauldron {
String prefix;
if (worldName.startsWith("DXL_")) {
prefix = P.p.getDxlName(worldName) + "." + id;
prefix = Util.getDxlName(worldName) + "." + id;
} else {
prefix = cauldron.block.getWorld().getUID().toString() + "." + id;
}

View File

@ -57,7 +57,7 @@ public class BPlayer {
public static BPlayer get(Player player) {
if (!players.isEmpty()) {
return players.get(P.playerString(player));
return players.get(Util.playerString(player));
}
return null;
}
@ -105,18 +105,18 @@ public class BPlayer {
}
public static boolean hasPlayer(Player player) {
return players.containsKey(P.playerString(player));
return players.containsKey(Util.playerString(player));
}
// Create a new BPlayer and add it to the list
public static BPlayer addPlayer(Player player) {
BPlayer bPlayer = new BPlayer();
players.put(P.playerString(player), bPlayer);
players.put(Util.playerString(player), bPlayer);
return bPlayer;
}
public static void remove(Player player) {
players.remove(P.playerString(player));
players.remove(Util.playerString(player));
}
public static int numDrunkPlayers() {
@ -319,7 +319,7 @@ public class BPlayer {
}
hangoverEffects(player);
// wird der spieler noch gebraucht?
players.remove(P.playerString(player));
players.remove(Util.playerString(player));
} else if (offlineDrunk - drunkeness >= 30) {
Location randomLoc = Wakeup.getRandom(player.getLocation());
@ -545,7 +545,7 @@ public class BPlayer {
if (bplayer.drunkeness > 30) {
if (bplayer.offlineDrunk == 0) {
Player player = P.getPlayerfromString(name);
Player player = Util.getPlayerfromString(name);
if (player != null) {
bplayer.drunkEffects(player);
@ -573,7 +573,7 @@ public class BPlayer {
// Prevent 0 drunkeness
soberPerMin++;
}
if (bplayer.drain(P.getPlayerfromString(name), soberPerMin)) {
if (bplayer.drain(Util.getPlayerfromString(name), soberPerMin)) {
iter.remove();
}
}

View File

@ -526,7 +526,7 @@ public class Barrel implements InventoryHolder {
// Saves all data
public static void save(ConfigurationSection config, ConfigurationSection oldData) {
P.p.createWorldSections(config);
Util.createWorldSections(config);
if (!barrels.isEmpty()) {
int id = 0;
@ -536,7 +536,7 @@ public class Barrel implements InventoryHolder {
String prefix;
if (worldName.startsWith("DXL_")) {
prefix = P.p.getDxlName(worldName) + "." + id;
prefix = Util.getDxlName(worldName) + "." + id;
} else {
prefix = barrel.spigot.getWorld().getUID().toString() + "." + id;
}

View File

@ -1,23 +1,16 @@
package com.dre.brewery;
import com.dre.brewery.filedata.*;
import com.dre.brewery.filedata.ConfigUpdater;
import com.dre.brewery.filedata.DataSave;
import com.dre.brewery.filedata.DataUpdater;
import com.dre.brewery.filedata.LanguageReader;
import com.dre.brewery.filedata.UpdateChecker;
import com.dre.brewery.integration.LogBlockBarrel;
import com.dre.brewery.integration.WGBarrel;
import com.dre.brewery.integration.WGBarrel7;
import com.dre.brewery.integration.WGBarrelNew;
import com.dre.brewery.integration.WGBarrelOld;
import com.dre.brewery.listeners.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.UUID;
import org.apache.commons.lang.math.NumberUtils;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
@ -36,6 +29,15 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class P extends JavaPlugin {
public static P p;
public static final String configVersion = "1.8";
@ -535,7 +537,7 @@ public class P extends JavaPlugin {
for (World world : p.getServer().getWorlds()) {
if (world.getName().startsWith("DXL_")) {
loadWorldData(getDxlName(world.getName()), world);
loadWorldData(Util.getDxlName(world.getName()), world);
} else {
loadWorldData(world.getUID().toString(), world);
}
@ -696,7 +698,7 @@ public class P extends JavaPlugin {
return false;
}
try {
saveFile(defconf, getDataFolder(), "config.yml", false);
Util.saveFile(defconf, getDataFolder(), "config.yml", false);
} catch (IOException e) {
e.printStackTrace();
return false;
@ -717,8 +719,8 @@ public class P extends JavaPlugin {
for (String l : new String[] {"de", "en", "fr", "it"}) {
File lfold = new File(configs, l);
try {
saveFile(getResource("config/" + (use1_13 ? "v13/" : "v12/") + l + "/config.yml"), lfold, "config.yml", overwrite);
saveFile(getResource("languages/" + l + ".yml"), languages, l + ".yml", false); // Never overwrite languages for now
Util.saveFile(getResource("config/" + (use1_13 ? "v13/" : "v12/") + l + "/config.yml"), lfold, "config.yml", overwrite);
Util.saveFile(getResource("languages/" + l + ".yml"), languages, l + ".yml", false); // Never overwrite languages for now
} catch (IOException e) {
e.printStackTrace();
}
@ -731,53 +733,6 @@ public class P extends JavaPlugin {
return NumberUtils.toInt(string, 0);
}
// gets the Name of a DXL World
public String getDxlName(String worldName) {
File dungeonFolder = new File(worldName);
if (dungeonFolder.isDirectory()) {
for (File file : dungeonFolder.listFiles()) {
if (!file.isDirectory()) {
if (file.getName().startsWith(".id_")) {
return file.getName().substring(1).toLowerCase();
}
}
}
}
return worldName;
}
// create empty World save Sections
public void createWorldSections(ConfigurationSection section) {
for (World world : p.getServer().getWorlds()) {
String worldName = world.getName();
if (worldName.startsWith("DXL_")) {
worldName = getDxlName(worldName);
} else {
worldName = world.getUID().toString();
}
section.createSection(worldName);
}
}
// prints a list of Strings at the specified page
public void list(CommandSender sender, ArrayList<String> strings, int page) {
int pages = (int) Math.ceil(strings.size() / 7F);
if (page > pages || page < 1) {
page = 1;
}
sender.sendMessage(color("&7-------------- &f" + languageReader.get("Etc_Page") + " &6" + page + "&f/&6" + pages + " &7--------------"));
ListIterator<String> iter = strings.listIterator((page - 1) * 7);
for (int i = 0; i < 7; i++) {
if (iter.hasNext()) {
sender.sendMessage(color(iter.next()));
} else {
break;
}
}
}
// Returns true if the Block can be destroyed by the Player or something else (null)
public boolean blockDestroy(Block block, Player player) {
@ -831,60 +786,9 @@ public class P extends JavaPlugin {
}
public String color(String msg) {
if (msg != null) {
msg = ChatColor.translateAlternateColorCodes('&', msg);
}
return msg;
return Util.color(msg);
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void saveFile(InputStream in, File dest, String name, boolean overwrite) throws IOException {
if (in == null) return;
if (!dest.exists()) {
dest.mkdirs();
}
File result = new File(dest, name);
if (result.exists()) {
if (overwrite) {
result.delete();
} else {
return;
}
}
OutputStream out = new FileOutputStream(result);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
}
// Returns either uuid or Name of player, depending on bukkit version
public static String playerString(Player player) {
if (useUUID) {
return player.getUniqueId().toString();
} else {
return player.getName();
}
}
// returns the Player if online
public static Player getPlayerfromString(String name) {
if (useUUID) {
try {
return Bukkit.getPlayer(UUID.fromString(name));
} catch (Exception e) {
return Bukkit.getPlayerExact(name);
}
}
return Bukkit.getPlayerExact(name);
}
// Runnables

View File

@ -1,12 +1,135 @@
package com.dre.brewery;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.ListIterator;
import java.util.UUID;
public class Util {
/********** Bukkit Utils **********/
// Check if the Chunk of a Block is loaded !without loading it in the process!
public static boolean isChunkLoaded(Block block) {
return block.getWorld().isChunkLoaded(block.getX() >> 4, block.getZ() >> 4);
}
public static String color(String msg) {
if (msg != null) {
msg = ChatColor.translateAlternateColorCodes('&', msg);
}
return msg;
}
// Returns either uuid or Name of player, depending on bukkit version
public static String playerString(Player player) {
if (P.useUUID) {
return player.getUniqueId().toString();
} else {
return player.getName();
}
}
// returns the Player if online
public static Player getPlayerfromString(String name) {
if (P.useUUID) {
try {
return Bukkit.getPlayer(UUID.fromString(name));
} catch (Exception e) {
return Bukkit.getPlayerExact(name);
}
}
return Bukkit.getPlayerExact(name);
}
/********** Other Utils **********/
// prints a list of Strings at the specified page
public static void list(CommandSender sender, ArrayList<String> strings, int page) {
int pages = (int) Math.ceil(strings.size() / 7F);
if (page > pages || page < 1) {
page = 1;
}
sender.sendMessage(color("&7-------------- &f" + P.p.languageReader.get("Etc_Page") + " &6" + page + "&f/&6" + pages + " &7--------------"));
ListIterator<String> iter = strings.listIterator((page - 1) * 7);
for (int i = 0; i < 7; i++) {
if (iter.hasNext()) {
sender.sendMessage(color(iter.next()));
} else {
break;
}
}
}
// gets the Name of a DXL World
public static String getDxlName(String worldName) {
File dungeonFolder = new File(worldName);
if (dungeonFolder.isDirectory()) {
for (File file : dungeonFolder.listFiles()) {
if (!file.isDirectory()) {
if (file.getName().startsWith(".id_")) {
return file.getName().substring(1).toLowerCase();
}
}
}
}
return worldName;
}
// create empty World save Sections
public static void createWorldSections(ConfigurationSection section) {
for (World world : P.p.getServer().getWorlds()) {
String worldName = world.getName();
if (worldName.startsWith("DXL_")) {
worldName = getDxlName(worldName);
} else {
worldName = world.getUID().toString();
}
section.createSection(worldName);
}
}
@SuppressWarnings("ResultOfMethodCallIgnored")
public static void saveFile(InputStream in, File dest, String name, boolean overwrite) throws IOException {
if (in == null) return;
if (!dest.exists()) {
dest.mkdirs();
}
File result = new File(dest, name);
if (result.exists()) {
if (overwrite) {
result.delete();
} else {
return;
}
}
OutputStream out = new FileOutputStream(result);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}

View File

@ -140,7 +140,7 @@ public class Wakeup {
locs.add("&6" + s + id + "&f" + s + ": " + world + " " + x + "," + y + "," + z);
}
}
p.list(sender, locs, page);
Util.list(sender, locs, page);
}
public static void check(CommandSender sender, int id, boolean all) {
@ -211,7 +211,7 @@ public class Wakeup {
checkPlayer.teleport(wakeup.loc);
} else {
p.msg(checkPlayer, p.languageReader.get("Player_WakeFilled", "" + checkId, world, "" + x , "" + y, "" + z));
}
}
p.msg(checkPlayer, p.languageReader.get("Player_WakeHint1"));
p.msg(checkPlayer, p.languageReader.get("Player_WakeHint2"));
}
@ -228,7 +228,7 @@ public class Wakeup {
public static void save(ConfigurationSection section, ConfigurationSection oldData) {
p.createWorldSections(section);
Util.createWorldSections(section);
// loc is saved as a String in world sections with format x/y/z/pitch/yaw
if (!wakeups.isEmpty()) {
@ -245,7 +245,7 @@ public class Wakeup {
String prefix;
if (worldName.startsWith("DXL_")) {
prefix = p.getDxlName(worldName) + "." + id;
prefix = Util.getDxlName(worldName) + "." + id;
} else {
prefix = wakeup.loc.getWorld().getUID().toString() + "." + id;
}
@ -264,4 +264,4 @@ public class Wakeup {
}
}
}
}

View File

@ -4,6 +4,7 @@ package com.dre.brewery.filedata;
import java.io.File;
import com.dre.brewery.MCBarrel;
import com.dre.brewery.Util;
import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
@ -153,7 +154,7 @@ public class DataSave extends BukkitRunnable {
for (World world : P.p.getServer().getWorlds()) {
String worldName = world.getName();
if (worldName.startsWith("DXL_")) {
worldName = P.p.getDxlName(worldName);
worldName = Util.getDxlName(worldName);
root.set("Worlds." + worldName, 0);
} else {
worldName = world.getUID().toString();

View File

@ -2,6 +2,7 @@ package com.dre.brewery.listeners;
import java.util.ArrayList;
import com.dre.brewery.Util;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -154,10 +155,10 @@ public class CommandListener implements CommandExecutor {
ArrayList<String> commands = getCommands(sender);
if (page == 1) {
p.msg(sender, "&6" + p.getDescription().getName() + " v" + p.getDescription().getVersion());
p.msg(sender, "&6" + p.getDescription().getName() + " v" + p.getDescription().getVersion());
}
p.list(sender, commands, page);
Util.list(sender, commands, page);
}
@ -200,6 +201,7 @@ public class CommandListener implements CommandExecutor {
}
if (sender.hasPermission("brewery.cmd.reload")) {
cmds.add(p.languageReader.get("Help_Configname"));
cmds.add(p.languageReader.get("Help_Reload"));
}

View File

@ -1,16 +1,16 @@
package com.dre.brewery.listeners;
import com.dre.brewery.BCauldron;
import com.dre.brewery.Barrel;
import com.dre.brewery.P;
import com.dre.brewery.Util;
import com.dre.brewery.filedata.DataSave;
import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.World;
import com.dre.brewery.P;
import com.dre.brewery.BCauldron;
import com.dre.brewery.Barrel;
import com.dre.brewery.filedata.DataSave;
public class WorldListener implements Listener {
@ -19,7 +19,7 @@ public class WorldListener implements Listener {
World world = event.getWorld();
if (world.getName().startsWith("DXL_")) {
P.p.loadWorldData(P.p.getDxlName(world.getName()), world);
P.p.loadWorldData(Util.getDxlName(world.getName()), world);
} else {
P.p.loadWorldData(world.getUID().toString(), world);
}