Skip to content
This repository has been archived by the owner on Feb 7, 2019. It is now read-only.

Commit

Permalink
Added commands, modified how the lang file is handled to better handl…
Browse files Browse the repository at this point in the history
…e errors.

New:
/value - query value of current main hand held block from bentobox level addon block value list.

/stoneores debug - puts stoneores into debug mode so you can see where a process is failing in the code. (As we didn't want to spam console when their was an error)

/stoneores query <string/int> <yaml standard path> - Queries config to pull specific value.
example usage:
/stoneores query string language
/stoneores query int worlds.Example_world.blocktypes.default.COBBLESTONE

Modified:

/stoneores reload - Now also reloads lang file.
  • Loading branch information
ShakeforProtein committed Nov 26, 2018
1 parent a7df6b3 commit a53a795
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 103 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.ShakeforProtein</groupId>
<artifactId>StoneOres</artifactId>
<version>0.9.1</version>
<version>0.11.2</version>
<packaging>jar</packaging>

<name>StoneOres</name>
Expand Down
260 changes: 160 additions & 100 deletions src/main/java/me/shakeforprotein/stoneores/StoneOres.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.Scanner;

import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.HumanEntity;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand All @@ -21,16 +20,18 @@
import world.bentobox.bentobox.BentoBox;



public class StoneOres extends JavaPlugin {
private boolean debug = true;
private BentoBox api;
private File langConfig = null;
private File playerFile = null;
private YamlConfiguration lang = new YamlConfiguration();
private YamlConfiguration playerYaml = new YamlConfiguration();

@Override
public void onEnable() {
// Plugin startup logic
Boolean debug = false;

System.out.println("StoneOres is Starting");
getServer().getPluginManager().registerEvents(new PlayerListener(this), this);
getConfig().options().copyDefaults(true);
Expand All @@ -55,175 +56,234 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
String mp = Language + ".messages.";
api = BentoBox.getInstance();
Player player = (Player) sender;
Player p = player;
int islandLevel = 0;
String currentWorld = player.getLocation().getWorld().getName();
String effWorld2 = currentWorld;
effWorld2.replace("_the_end", "_world").replace("_nether", "_world");

if (cmd.getName().equalsIgnoreCase("value")) {
HumanEntity hE = (HumanEntity) sender;
Integer itemValue = -1;
Integer itemLimit = -1;
String blockLimit = "";
String itemInHand = hE.getInventory().getItemInMainHand().getType().toString();
try {
itemValue = api.getAddonsManager().getAddonByName("Level").get().getConfig().getInt("blocks." + itemInHand);
itemLimit = api.getAddonsManager().getAddonByName("Level").get().getConfig().getInt("limits." + itemInHand);
if (itemLimit > 0) {
blockLimit = " but is worth nothing after " + itemLimit + " blocks of " + itemInHand + " are on your island";
}
} catch (NullPointerException e) {
}
if (itemValue < 1) {
itemValue = 0;
}
p.sendMessage(itemInHand + " has a value of " + itemValue + blockLimit);

} else if ((cmd.getName().equalsIgnoreCase("stoneores")) || (cmd.getName().equalsIgnoreCase("ores"))) {
if (args.length == 3 && args[0].equalsIgnoreCase("query")) {
if (p.hasPermission("stoneores.reload")) {
if (args[1].equalsIgnoreCase("int")) {
p.sendMessage("" + getConfig().getInt("" + args[2]));
} else if (args[1].equalsIgnoreCase("string")) {
p.sendMessage(getConfig().getString(args[2]));
}
}
}
if (args.length == 1) {
if (args[0].equalsIgnoreCase("all")) {
getAllGeneratorGroups(player);
} else if (args[0].equalsIgnoreCase("reload")) {
if (player.hasPermission("stoneores.reload")) {
try {
lang.load(langConfig);
} catch (InvalidConfigurationException | IOException e) {
}
reloadConfig();
player.sendMessage(getLang().getString(mp + "configReloaded").replace('&', '§'));
} else {
player.sendMessage(getLang().getString(mp + "noPermission").replace('&', '§'));
}
} else if (args[0].equalsIgnoreCase("debug")) {
if (player.hasPermission("stoneores.reload")) {
if (getConfig().get("debug") == "false") {
getConfig().set("debug", "true");
p.sendMessage("debug enabled");
} else {
getConfig().set("debug", "false");
p.sendMessage("debug Disabled");
}
}
} else if (args[0].equalsIgnoreCase("version")) {
p.sendMessage("StoneOres - " + this.getDescription().getVersion());
}
} else {
bentoCallLevel(player.getWorld(), player);
// to set a delay while it runs the level command
getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
public void run() {
Debug("Debug mode enabled", p);
String generatorGroup = "default";
Debug("Generator group = " + generatorGroup, p);
UUID thisIslandOwner = api.getIslands().getIslandAt(player.getLocation()).get().getOwner();
Debug("thisIslandOwner = " + thisIslandOwner, p);

if ((cmd.getName().equalsIgnoreCase("stoneores") || cmd.getName().equalsIgnoreCase("ores")) && sender instanceof Player) {
bentoCallLevel(player.getWorld(), player);
// to set a delay while it runs the level command
getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
public void run() {
String generatorGroup = "default";
UUID thisIslandOwner = api.getIslands().getIslandAt(player.getLocation()).get().getOwner();
generatorGroup = getGeneratorGroup(player.getWorld(), readPlayerLevelYaml(thisIslandOwner, player.getWorld()));
String[] blocksList = getBlockList(player.getWorld(), generatorGroup);
generatorGroup = getGeneratorGroup(player.getWorld(), readPlayerLevelYaml(thisIslandOwner, player.getWorld()));
Debug("Calculated generator group is " + generatorGroup, p);
String[] blocksList = getBlockList(player.getWorld(), generatorGroup);
Debug("Blockslist is " + blocksList, p);


if (getConfig().getConfigurationSection("world." + currentWorld) != null) {
if (getConfig().getConfigurationSection("world." + currentWorld) != null) {


int percentCalc = 0, arrayId = 0, i = 0;
String[] blocktypes = new String[blocksList.length];
String[] cases = new String[50000];
int percentCalc = 0, arrayId = 0, i = 0;
String[] blocktypes = new String[blocksList.length];
String[] cases = new String[50000];


for (String item : blocksList) {
percentCalc += getConfig().getInt("world." + currentWorld + ".blocktypes." + generatorGroup + "." + item);
blocktypes[arrayId] = item;
arrayId++;
for (String item : blocksList) {
percentCalc += getConfig().getInt("world." + currentWorld + ".blocktypes." + generatorGroup + "." + item);
blocktypes[arrayId] = item;
arrayId++;

while (i < percentCalc) {
cases[i] = item;
i++;
}
}
if (args.length == 1 && args[0].equalsIgnoreCase("reload")) {
if (player.hasPermission("stoneores.reload")) {
reloadConfig();
player.sendMessage(getLang().getString(mp + "configReloaded").replace('&', '§'));
} else {
player.sendMessage(getLang().getString(mp + "noPermission").replace('&', '§'));
while (i < percentCalc) {
cases[i] = item;
i++;
}
}
} else {


String hasPermission = null;
int percent = 0;


if (generatorGroup != null) {
Debug("Generator permission granted", p);
player.sendMessage(getLang().getString(mp + "hasTier").replace("{permission}", generatorGroup).replace('&', '§'));
player.sendMessage(getLang().getString(mp + "rates").replace('&', '§'));

}
for (String item : getConfig().getConfigurationSection("world." + currentWorld + ".blocktypes." + generatorGroup).getKeys(false)) {
percent = getConfig().getInt("world." + currentWorld + ".blocktypes." + generatorGroup + "." + item);
double percentDouble = ((double) percent);
player.sendMessage("§3" + item + ": §f" + Math.rint((percentDouble / percentCalc) * 100) + "%");
}

for (String item : getConfig().getConfigurationSection("world." + currentWorld + ".blocktypes." + generatorGroup).getKeys(false)) {
percent = getConfig().getInt("world." + currentWorld + ".blocktypes." + generatorGroup + "." + item);
double percentDouble = ((double) percent);
player.sendMessage("§3" + item + ": §f" + Math.rint((percentDouble / percentCalc) * 100) + "%");
}
}

}

}


}, 40L);
}, 40L);
}
} else {
player.sendMessage("§3 Sorry, that command does not work in this world");
}

return true;
}

private void mkdir(File L){
if(!L.exists()){
private void mkdir(File L) {
if (!L.exists()) {
saveResource("lang.yml", false);

}

}
private void loadYamls(){

private void loadYamls() {
try {
lang.load(langConfig);
} catch (InvalidConfigurationException | IOException e) {
}
catch(InvalidConfigurationException e){e.printStackTrace();}
catch(FileNotFoundException e){e.printStackTrace();}
catch(IOException e){e.printStackTrace();}
}

public YamlConfiguration getLang(){
public YamlConfiguration getLang() {
return lang;
}

public void saveLang(){
try{lang.save(langConfig);}
catch (IOException e){e.printStackTrace();}
public YamlConfiguration getPlayerYaml() {
return playerYaml;
}

public void saveLang() {
try {
lang.save(langConfig);
} catch (IOException e) {
e.printStackTrace();
}
}


public void bentoCallLevel(World world, Player p){
public void bentoCallLevel(World world, Player p) {
Debug("Start bento level call", p);
String worldString = world.getName();
if (worldString.equalsIgnoreCase("AcidIsland_world")){p.performCommand("ai level");}
if (worldString.equalsIgnoreCase("BSkyBlock_world")){p.performCommand("is level");}
if (worldString.equalsIgnoreCase("SkyGrid_world")){p.performCommand("sg level");}
String commandString = getConfig().getString("world." + worldString + ".lvlcommand");
p.performCommand(commandString);
}

public int readPlayerLevelYaml(UUID ownerID, World world){
public int readPlayerLevelYaml(UUID ownerID, World world) {

//TODO: Replace this method to use yaml interpreter instead of relying on file to string
Bukkit.broadcast("playerFile - ","");
File playerFile = new File(Bukkit.getPluginManager().getPlugin("StoneOres").getDataFolder().getParent() + "/BentoBox/database/LevelsData", ownerID.toString() + ".yml");
playerFile = new File(Bukkit.getPluginManager().getPlugin("StoneOres").getDataFolder().getParent() + "/BentoBox/database/LevelsData", ownerID.toString() + ".yml");
playerYaml = new YamlConfiguration();
mkdir(playerFile);
try {
playerYaml.load(playerFile);
} catch (FileNotFoundException e) {
} catch (InvalidConfigurationException | IOException e) {
}

int islandLevel = getPlayerYaml().getInt("levels." + world.getName(), 0);
if (islandLevel < 1) {
islandLevel = 0;
}
return islandLevel;
}

String fileOutput = "";
String worldStr = world.getName();
try {fileOutput = readFile(playerFile.toString());}
catch (IOException IO){}

if ((fileOutput != null) && (fileOutput != "") && (fileOutput.contains(worldStr+ ":"))) {
if(fileOutput.length() < 1) {
playerFile.delete();
public void getAllGeneratorGroups(Player player) {
Debug("Start get all generator groups", player);
String worldStr = player.getWorld().getName();
String tierKeysConf = getConfig().getConfigurationSection("world." + worldStr + ".tiers").getKeys(false).toString();
String[] tierKeys = tierKeysConf.substring(1, tierKeysConf.length() - 1).replaceAll("\\s+", "").split(",");
String generatorGroup = "default";
for (String item : tierKeys) {
Integer tierPoints = getConfig().getInt("world." + worldStr + ".tiers." + item.trim());
player.sendMessage(" ");
player.sendMessage(" ");
player.sendMessage("The generator - " + item + " - requires an island level of " + tierPoints + " and generates ores at the following rates");
generatorGroup = getGeneratorGroup(player.getWorld(), tierPoints + 1);
Integer percent, percentCalc = 0;
for (String item2 : getConfig().getConfigurationSection("world." + worldStr + ".blocktypes." + generatorGroup).getKeys(false)) {
percent = getConfig().getInt("world." + worldStr + ".blocktypes." + generatorGroup + "." + item2);
percentCalc += percent;
double percentDouble = ((double) percent);
player.sendMessage("§3" + item2 + ": §f" + Math.rint((percentDouble / percentCalc) * 100) + "%");
}
Pattern p = Pattern.compile(worldStr + ": \\d+");
Matcher m = p.matcher(fileOutput);
m.find();
String tempLevel = m.group().split(": ")[1];
return Integer.parseInt(tempLevel);
}
else{
return 1;
}
Debug("Finish get all generator groups", player);
}


public String getGeneratorGroup(World world, int isLvl){
public String getGeneratorGroup(World world, int isLvl) {
String worldStr = world.getName();
String tierKeysConf = getConfig().getConfigurationSection("world." + worldStr + ".tiers").getKeys(false).toString();
String[] tierKeys = tierKeysConf.substring(1, tierKeysConf.length() - 1).replaceAll("\\s+", "").split(",");
String islandTier = "";
Integer dummyInt = 0;
String islandTier = "default";
for (String item : tierKeys) {
Integer tierPoints = getConfig().getInt("world." + worldStr + ".tiers." + item.trim());
if (tierPoints < isLvl){
if (tierPoints < isLvl) {
islandTier = item.trim();
if(debug){Bukkit.broadcast("island Tier = " + islandTier, "*");}

}
}
return islandTier;
}


public String[] getBlockList(World world, String genGroup){
public String[] getBlockList(World world, String genGroup) {
return getConfig().getConfigurationSection("world." + world.getName() + ".blocktypes." + genGroup).getKeys(false).toString().substring(1, getConfig().getConfigurationSection("world." + world.getName() + ".blocktypes." + genGroup).getKeys(false).toString().length() - 1).replaceAll("\\s+", "").split(",");
}

public String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");

try {
while(scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
} finally {
scanner.close();
public void Debug(String str, Player p) {
if (getConfig().getString("debug").equalsIgnoreCase("true")) {
p.sendMessage(str + "");
}
}
}
Loading

0 comments on commit a53a795

Please sign in to comment.