From ba8ad228893bc85d217faccfc3ff462e2ef5bcc9 Mon Sep 17 00:00:00 2001 From: alisolarflare Date: Thu, 8 Dec 2016 21:01:04 -0500 Subject: [PATCH] Added CowSpawnLoop Spawns cows every few nights if there are no cows near the player in question --- .settings/org.eclipse.core.resources.prefs | 1 - pom.xml | 3 - src/alisolarflare/AliPresents.java | 2 + .../CreativeBoundariesAPI.java | 5 ++ .../letterdyes/LetterDyeListener.java | 6 +- .../components/metrics/MetricsComponent.java | 2 +- .../components/metrics/files/MetricsFile.java | 64 ++++++------------- .../components/spawn/SpawnComponent.java | 15 +++++ .../components/spawn/cows/CowSpawnLoop.java | 47 ++++++++++++++ .../alisolarflare/resources}/metrics.txt | 0 .../alisolarflare/resources}/playerLogins.txt | 0 11 files changed, 93 insertions(+), 52 deletions(-) create mode 100644 src/alisolarflare/components/spawn/SpawnComponent.java create mode 100644 src/alisolarflare/components/spawn/cows/CowSpawnLoop.java rename {metrics => src/alisolarflare/resources}/metrics.txt (100%) rename {metrics => src/alisolarflare/resources}/playerLogins.txt (100%) diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index e18f795..1df21ac 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -1,4 +1,3 @@ eclipse.preferences.version=1 encoding/=UTF-8 -encoding/metrics=UTF-8 encoding/src=UTF-8 diff --git a/pom.xml b/pom.xml index 62a9e7d..b296628 100644 --- a/pom.xml +++ b/pom.xml @@ -21,9 +21,6 @@ *.yml - - metrics - diff --git a/src/alisolarflare/AliPresents.java b/src/alisolarflare/AliPresents.java index f283e7b..6db2501 100644 --- a/src/alisolarflare/AliPresents.java +++ b/src/alisolarflare/AliPresents.java @@ -13,6 +13,7 @@ import alisolarflare.components.flaircolouring.FlairColouringComponent; import alisolarflare.components.gpowers.GPowerComponent; import alisolarflare.components.insurance.InsuranceComponent; import alisolarflare.components.magic.MagicComponent; +import alisolarflare.components.spawn.SpawnComponent; public class AliPresents extends JavaPlugin{ public void onEnable(){ @@ -30,6 +31,7 @@ public class AliPresents extends JavaPlugin{ new InsuranceComponent().register(this); new MagicComponent().register(this); //new MetricsComponent().register(this); + new SpawnComponent().register(this); logger.info(pdfFile.getName() + " has fully registered (V." + pdfFile.getVersion()+ ")."); diff --git a/src/alisolarflare/components/creativeboundaries/CreativeBoundariesAPI.java b/src/alisolarflare/components/creativeboundaries/CreativeBoundariesAPI.java index 77b7996..7658e14 100644 --- a/src/alisolarflare/components/creativeboundaries/CreativeBoundariesAPI.java +++ b/src/alisolarflare/components/creativeboundaries/CreativeBoundariesAPI.java @@ -23,6 +23,11 @@ public class CreativeBoundariesAPI { cbCreatives.remove(player); } public static boolean toCreative(Player player){ + if (player.getWorld().getName().equalsIgnoreCase("World")){ + player.sendMessage("Creative Boundaries is disabled on the New Map!"); + return true; + } + TownBlock tb = TownyUniverse.getTownBlock(player.getLocation()); if (tb == null) { diff --git a/src/alisolarflare/components/flaircolouring/letterdyes/LetterDyeListener.java b/src/alisolarflare/components/flaircolouring/letterdyes/LetterDyeListener.java index be65de6..ec16b12 100644 --- a/src/alisolarflare/components/flaircolouring/letterdyes/LetterDyeListener.java +++ b/src/alisolarflare/components/flaircolouring/letterdyes/LetterDyeListener.java @@ -24,10 +24,12 @@ public class LetterDyeListener implements Listener{ try{ if((event.getAction() != Action.RIGHT_CLICK_AIR)) return; ItemStack item = event.getItem(); + if(item.getType() != Material.INK_SACK) return; - if(item.getItemMeta().hasEnchant(Enchantment.LUCK) && (item.getItemMeta().getEnchantLevel(Enchantment.LUCK) == 10)) return; + if(item.getItemMeta().hasEnchant(Enchantment.LUCK)) return; + if(item.getItemMeta().getEnchantLevel(Enchantment.LUCK) == 10) return; if(item.getItemMeta().getDisplayName() == "LetterDye") return; - + Dye dye = (Dye) event.getItem().getData(); User user = essentials.getUser(event.getPlayer()); user.setNickname(FlairColouringAPI.colourShiftRight(user._getNickname(), dye.getColor())); diff --git a/src/alisolarflare/components/metrics/MetricsComponent.java b/src/alisolarflare/components/metrics/MetricsComponent.java index 3bb855f..34bd000 100644 --- a/src/alisolarflare/components/metrics/MetricsComponent.java +++ b/src/alisolarflare/components/metrics/MetricsComponent.java @@ -11,7 +11,7 @@ import alisolarflare.components.metrics.files.MetricsFile; import alisolarflare.components.metrics.output.GetLoginMetrics; public class MetricsComponent extends Component{ - String defaultPath = "metrics"; + String defaultPath = "src/alisolarflare/resources"; String defaultFilePath = (defaultPath + "/metrics.txt"); String playerLoginsFilePath = (defaultPath + "/playerLogins.txt"); diff --git a/src/alisolarflare/components/metrics/files/MetricsFile.java b/src/alisolarflare/components/metrics/files/MetricsFile.java index 5fa2d98..baab52e 100644 --- a/src/alisolarflare/components/metrics/files/MetricsFile.java +++ b/src/alisolarflare/components/metrics/files/MetricsFile.java @@ -13,54 +13,28 @@ import java.util.List; import buttondevteam.lib.TBMCCoreAPI; public class MetricsFile { - private String fileName = "AliPresents/metrics/playerLogins.txt"; - public MetricsFile(String directory){ - this.fileName = directory; + private String fileName = "src/alisolarflare/resources/metrics/playerLogins.txt"; + public MetricsFile(String filePath){ + this.fileName = filePath; } public void AddLine(String string){ BufferedWriter inputStream = null; - String[] fileNames = { - "/AliPresents/metrics/playerLogins.txt", - "AliPresents/metrics/playerLogins.txt", - "/metrics/playerLogins.txt", - "metrics/playerLogins.txt", - "/playerLogins.txt", - "playerLogins.txt", - - - "/AliPresents/metrics/playerLogins", - "AliPresents/metrics/playerLogins", - "/metrics/playerLogins", - "metrics/playerLogins", - "/playerLogins", - "playerLogins", - - ".AliPresents.metrics.playerLogins.txt", - "AliPresents.metrics.playerLogins.txt", - ".metrics.playerLogins.txt", - "metrics.playerLogins.txt", - ".playerLogins.txt", - "playerLogins.txt", - - }; - for(String fileName: fileNames){ - try { - TBMCCoreAPI.sendDebugMessage("Trying to Open File: "+fileName+"..."); - File file = new File(fileName); - TBMCCoreAPI.sendDebugMessage("File Exists: " + file.exists()); - inputStream = new BufferedWriter(new FileWriter(fileName, true)); - TBMCCoreAPI.sendDebugMessage("Input Stream Created!"); - TBMCCoreAPI.sendDebugMessage("Trying to write to file..."); - inputStream.write(string); - TBMCCoreAPI.sendDebugMessage("Ending with newLine..."); - inputStream.newLine(); - TBMCCoreAPI.sendDebugMessage("Closing File..."); - inputStream.close(); - TBMCCoreAPI.sendDebugMessage("File Closed!"); - } catch (IOException e) { - TBMCCoreAPI.sendDebugMessage(fileName +" Input Stream could not be created! "); - TBMCCoreAPI.sendDebugMessage(e.toString()); - } + try { + TBMCCoreAPI.sendDebugMessage("Trying to Open File: "+fileName+"..."); + File file = new File(fileName); + TBMCCoreAPI.sendDebugMessage("File Exists: " + file.exists()); + inputStream = new BufferedWriter(new FileWriter(fileName, true)); + TBMCCoreAPI.sendDebugMessage("Input Stream Created!"); + TBMCCoreAPI.sendDebugMessage("Trying to write to file..."); + inputStream.write(string); + TBMCCoreAPI.sendDebugMessage("Ending with newLine..."); + inputStream.newLine(); + TBMCCoreAPI.sendDebugMessage("Closing File..."); + inputStream.close(); + TBMCCoreAPI.sendDebugMessage("File Closed!"); + } catch (IOException e) { + TBMCCoreAPI.sendDebugMessage(fileName +" Input Stream could not be created! "); + TBMCCoreAPI.sendDebugMessage(e.toString()); } } public List toArrayList(){ diff --git a/src/alisolarflare/components/spawn/SpawnComponent.java b/src/alisolarflare/components/spawn/SpawnComponent.java new file mode 100644 index 0000000..60bd317 --- /dev/null +++ b/src/alisolarflare/components/spawn/SpawnComponent.java @@ -0,0 +1,15 @@ +package alisolarflare.components.spawn; + +import org.bukkit.plugin.java.JavaPlugin; + +import alisolarflare.architecture.Component; +import alisolarflare.components.spawn.cows.CowSpawnLoop; + +public class SpawnComponent extends Component { + + @Override + public void register(JavaPlugin plugin) { + registerListener(plugin, new CowSpawnLoop(plugin)); + } + +} diff --git a/src/alisolarflare/components/spawn/cows/CowSpawnLoop.java b/src/alisolarflare/components/spawn/cows/CowSpawnLoop.java new file mode 100644 index 0000000..b7ded14 --- /dev/null +++ b/src/alisolarflare/components/spawn/cows/CowSpawnLoop.java @@ -0,0 +1,47 @@ +package alisolarflare.components.spawn.cows; + +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.entity.Cow; +import org.bukkit.entity.Entity; +import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.event.Listener; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.util.Vector; + +public class CowSpawnLoop extends BukkitRunnable implements Listener{ + + private JavaPlugin plugin; + + public CowSpawnLoop(JavaPlugin plugin) { + this.plugin = plugin; + this.runTaskTimer(plugin, 30200, 30200); + } + + @Override + public void run() { + boolean foodTrue; + for (Player player : plugin.getServer().getOnlinePlayers()){ + foodTrue = false; + for (Entity entity : player.getNearbyEntities(128, 128, 128)){ + if (entity instanceof Cow){ + foodTrue = true; + } + } + if (foodTrue){ + Location location = player.getLocation().add(new Vector(Math.random()*20, 0, Math.random()*20).normalize().multiply(30)); + Block attemptedCowSpawn = player.getWorld().getHighestBlockAt(location); + if (attemptedCowSpawn.getType().equals(Material.GRASS)){ + player.getWorld().spawnEntity(attemptedCowSpawn.getLocation().add(new Vector (0,1,0)), EntityType.COW); + player.getWorld().spawnEntity(attemptedCowSpawn.getLocation().add(new Vector (0,1,0)), EntityType.COW); + player.getWorld().spawnEntity(attemptedCowSpawn.getLocation().add(new Vector (0,1,0)), EntityType.COW); + } + } + } + + } + +} diff --git a/metrics/metrics.txt b/src/alisolarflare/resources/metrics.txt similarity index 100% rename from metrics/metrics.txt rename to src/alisolarflare/resources/metrics.txt diff --git a/metrics/playerLogins.txt b/src/alisolarflare/resources/playerLogins.txt similarity index 100% rename from metrics/playerLogins.txt rename to src/alisolarflare/resources/playerLogins.txt