This repository has been archived by the owner on Feb 7, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upload of current source files as at 1542606726054
- Loading branch information
1 parent
79f65ce
commit 7e880ea
Showing
6 changed files
with
476 additions
and
0 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
src/main/java/me/shakeforprotein/stoneores/PlayerListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package me.shakeforprotein.stoneores; | ||
|
||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.Material; | ||
import org.bukkit.block.Block; | ||
import org.bukkit.block.BlockFace; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.block.BlockFromToEvent; | ||
import world.bentobox.bentobox.BentoBox; | ||
|
||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
import java.util.UUID; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
|
||
public class PlayerListener implements Listener { | ||
private StoneOres plugin; | ||
private BentoBox api; | ||
|
||
|
||
public PlayerListener(StoneOres instance) { | ||
|
||
instance.getServer().getPluginManager().registerEvents(this, instance); | ||
BentoBox.getInstance().getServer().getPluginManager().registerEvents(this, instance); | ||
this.plugin = instance; | ||
} | ||
|
||
|
||
@EventHandler | ||
public void onBlockFromToEvent(BlockFromToEvent e) { | ||
api = BentoBox.getInstance(); | ||
|
||
int islandLevel = 0; | ||
String generatorGroup = "default"; | ||
|
||
|
||
String blockToMake = null; | ||
String effectedWorld = e.getBlock().getLocation().getWorld().getName(); | ||
String effWorld2 = effectedWorld; | ||
effWorld2.replace("_the_end", "_world").replace("_nether", "_world"); | ||
|
||
|
||
if (e.getBlock().getType() == Material.LAVA || (e.getBlock().getType() == Material.WATER)) { | ||
if (e.getToBlock().getType() == Material.AIR) { | ||
if (wouldMakeCobble(e.getBlock().getType(), e.getToBlock())) { | ||
if (getBlockLocation(e.getBlock().getType(), e.getToBlock()) != null) { | ||
Location location = getBlockLocation(e.getBlock().getType(), e.getToBlock()); | ||
if (plugin.getConfig().getConfigurationSection("world." + effectedWorld) != null) { | ||
UUID ownerUUID = api.getIslands().getIslandAt(e.getBlock().getLocation()).get().getOwner(); | ||
generatorGroup = plugin.getGeneratorGroup(e.getBlock().getWorld(), plugin.readPlayerLevelYaml(ownerUUID, e.getBlock().getWorld())); | ||
String[] blocksList = plugin.getBlockList(e.getBlock().getWorld(), generatorGroup); | ||
|
||
int totalChance = 0, arrayId = 0, i = 0; | ||
String[] blocktypes = new String[blocksList.length]; | ||
String[] cases = new String[50000]; | ||
|
||
for (String item : blocksList) { | ||
totalChance += plugin.getConfig().getInt("world." + effectedWorld + ".blocktypes." + generatorGroup + "." + item); | ||
blocksList[arrayId] = item; | ||
arrayId++; | ||
|
||
while (i < totalChance) { | ||
cases[i] = item; | ||
i++; | ||
} | ||
} | ||
|
||
int random = 1 + (int) (Math.random() * totalChance); | ||
|
||
blockToMake = cases[random]; | ||
|
||
|
||
try{e.getBlock().getWorld().getBlockAt(location).setType(Material.getMaterial(blockToMake));} | ||
catch(NullPointerException err){} | ||
e.setCancelled(true); | ||
|
||
|
||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private final BlockFace[] sides = new BlockFace[]{BlockFace.SELF, BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST}; | ||
|
||
|
||
public boolean wouldMakeCobble(Material mat, Block block) { | ||
for (BlockFace side : sides) { | ||
if (block.getRelative(side, 1).getType() == (mat == Material.WATER ? Material.LAVA : Material.WATER) || block.getRelative(side, 1).getType() == (mat == Material.WATER ? Material.LAVA : Material.WATER)) { | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
public Location getBlockLocation(Material material, Block block) { | ||
Location location = null; | ||
|
||
for (BlockFace side : sides) { | ||
if (block.getRelative(side, 1).getType() == (material == Material.LAVA ? Material.WATER : Material.LAVA)) { | ||
if (block != null && block.getLocation() != null) { | ||
location = block.getLocation(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
return location; | ||
} | ||
|
||
} | ||
|
19 changes: 19 additions & 0 deletions
19
src/main/java/me/shakeforprotein/stoneores/ReusableMethods.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.shakeforprotein.stoneores; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.World; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.Scanner; | ||
import java.util.UUID; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class ReusableMethods{ | ||
/*Todo: Move all possible methods into this class, and work out cross class calling | ||
This will make it easier to follow the event sequences. | ||
*/ | ||
} |
229 changes: 229 additions & 0 deletions
229
src/main/java/me/shakeforprotein/stoneores/StoneOres.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,229 @@ | ||
package me.shakeforprotein.stoneores; | ||
|
||
import java.io.File; | ||
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.plugin.java.JavaPlugin; | ||
import org.bukkit.command.Command; | ||
import org.bukkit.command.CommandSender; | ||
import org.bukkit.entity.Player; | ||
|
||
import world.bentobox.bentobox.BentoBox; | ||
|
||
|
||
|
||
public class StoneOres extends JavaPlugin { | ||
private boolean debug = true; | ||
private BentoBox api; | ||
private File langConfig = null; | ||
private YamlConfiguration lang = new YamlConfiguration(); | ||
|
||
@Override | ||
public void onEnable() { | ||
// Plugin startup logic | ||
System.out.println("StoneOres is Starting"); | ||
getServer().getPluginManager().registerEvents(new PlayerListener(this), this); | ||
getConfig().options().copyDefaults(true); | ||
saveConfig(); | ||
langConfig = new File(getDataFolder(), "lang.yml"); | ||
mkdir(langConfig); | ||
loadYamls(); | ||
|
||
System.out.println("StoneOres has finished loading"); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
// Plugin shutdown logic | ||
System.out.println("StoneOres has Terminated successfully"); | ||
} | ||
|
||
|
||
@Override | ||
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { | ||
String Language = getConfig().getString("language"); | ||
String mp = Language + ".messages."; | ||
api = BentoBox.getInstance(); | ||
Player player = (Player) sender; | ||
int islandLevel = 0; | ||
String currentWorld = player.getLocation().getWorld().getName(); | ||
String effWorld2 = currentWorld; | ||
effWorld2.replace("_the_end", "_world").replace("_nether", "_world"); | ||
|
||
|
||
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); | ||
|
||
|
||
if (getConfig().getConfigurationSection("world." + currentWorld) != null) { | ||
|
||
|
||
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++; | ||
|
||
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('&', '§')); | ||
} | ||
} else { | ||
|
||
String hasPermission = null; | ||
int percent = 0; | ||
|
||
|
||
if (generatorGroup != null) { | ||
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) + "%"); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
} | ||
|
||
|
||
}, 40L); | ||
} else { | ||
player.sendMessage("§3 Sorry, that command does not work in this world"); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private void mkdir(File L){ | ||
if(!L.exists()){ | ||
saveResource("lang.yml", false); | ||
|
||
} | ||
|
||
} | ||
private void loadYamls(){ | ||
try { | ||
lang.load(langConfig); | ||
} | ||
catch(InvalidConfigurationException e){e.printStackTrace();} | ||
catch(FileNotFoundException e){e.printStackTrace();} | ||
catch(IOException e){e.printStackTrace();} | ||
} | ||
|
||
public YamlConfiguration getLang(){ | ||
return lang; | ||
} | ||
|
||
public void saveLang(){ | ||
try{lang.save(langConfig);} | ||
catch (IOException e){e.printStackTrace();} | ||
} | ||
|
||
|
||
public void bentoCallLevel(World world, Player 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");} | ||
} | ||
|
||
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"); | ||
|
||
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(); | ||
} | ||
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; | ||
} | ||
} | ||
|
||
|
||
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; | ||
for (String item : tierKeys) { | ||
Integer tierPoints = getConfig().getInt("world." + worldStr + ".tiers." + item.trim()); | ||
if (tierPoints < isLvl){ | ||
islandTier = item.trim(); | ||
if(debug){Bukkit.broadcast("island Tier = " + islandTier, "*");} | ||
|
||
} | ||
} | ||
return islandTier; | ||
} | ||
|
||
|
||
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.