diff --git a/.travis.yml b/.travis.yml index 6478810..aaf9818 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ sudo: false language: java -jdk: oraclejdk7 +jdk: openjdk7 cache: directories: diff --git a/README.md b/README.md index 8610e65..053ba13 100644 --- a/README.md +++ b/README.md @@ -122,11 +122,3 @@ SnowFall: #this is only in biomes where snow falls naturally * Make deeper snow slow down players and/or mobs. * Create custom snow related recipes (ideas welcome) * Enable for snow on mountains not in a snowy biome - -## -*This plugin utilizes Hidendra's plugin metrics system, which means some anonymous information will be collected and sent to mcstats.org. -This allows me to track how many servers are actually running the plugin, and the larger user base I have the more likely I am to add features. -If you wish to opt out of this service, it can be done by editing plugins/Plugin Metrics/config.yml* - - -## diff --git a/pom.xml b/pom.xml index 1d86dd1..831d1b3 100644 --- a/pom.xml +++ b/pom.xml @@ -3,12 +3,32 @@ 4.0.0 com.zmanww SnowControl - 1.1.1 + 1.1.3 jar SnowControl 2013 https://github.com/zwollner/SnowControl + + UTF-8 + + + + + spigot-repo + https://hub.spigotmc.org/nexus/content/groups/public/ + + + + + + org.bukkit + bukkit + 1.12-R0.1-SNAPSHOT + provided + + + SnowControl clean install @@ -27,7 +47,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.0 + 3.1 1.7 1.7 @@ -67,7 +87,6 @@ com.zmanww.* - org.mcstats.*:* @@ -82,56 +101,4 @@ - - - UTF-8 - - - - - Plugin Metrics - http://repo.mcstats.org/content/repositories/public - - - Spigot Public - https://hub.spigotmc.org/nexus/content/groups/public - - - - - - - org.bukkit - bukkit - - - junit - junit - test - - - org.mcstats.bukkit - metrics - compile - - - - - - org.bukkit - bukkit - 1.8.8-R0.1-SNAPSHOT - - - org.mcstats.bukkit - metrics - LATEST - - - junit - junit - LATEST - - - diff --git a/src/main/java/com/zmanww/bukkit/SnowControl/Config.java b/src/main/java/com/zmanww/bukkit/SnowControl/Config.java index 6a048fa..502b397 100644 --- a/src/main/java/com/zmanww/bukkit/SnowControl/Config.java +++ b/src/main/java/com/zmanww/bukkit/SnowControl/Config.java @@ -59,9 +59,8 @@ public static Config getInstance() { public void reload() { plugin.reloadConfig(); // Force reload instance = null;// Force all objects to reload. - - // restart the Monitor - plugin.startScheduler(); + plugin.load(); + plugin.startScheduler(); // restart the Monitor } private void loadKeys() { @@ -152,11 +151,6 @@ public boolean debugEnabled() { return plugin.getConfig().getBoolean("debug", false); } - public boolean isMetricsEnabled() - { - return plugin.getConfig().getBoolean("Metrics", true); - } - public boolean isAccumulationEnabled() { return plugin.getConfig().getBoolean("SnowFall.AccumulationEnabled", true); } @@ -201,4 +195,8 @@ public int getMinLightLevel() { return plugin.getConfig().getInt("SnowFall.MinLightLevelToMelt", 12); } + public int getMaxChunksPerCheck() + { + return Math.min(plugin.getConfig().getInt("SnowFall.MaxChunksPerWorldAndCheck", 441), 1); + } } diff --git a/src/main/java/com/zmanww/bukkit/SnowControl/PlayerListener.java b/src/main/java/com/zmanww/bukkit/SnowControl/PlayerListener.java index 28ba959..0cd7e01 100644 --- a/src/main/java/com/zmanww/bukkit/SnowControl/PlayerListener.java +++ b/src/main/java/com/zmanww/bukkit/SnowControl/PlayerListener.java @@ -26,6 +26,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockDamageEvent; @@ -43,7 +44,7 @@ public PlayerListener(SnowControl instance) { plugin = instance; } - @EventHandler() + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void blockBreak(BlockBreakEvent event) { Block block = event.getBlock(); if (block.getType() == Material.ICE) { @@ -130,8 +131,7 @@ public void onBlockDamage(BlockDamageEvent event) { event.getPlayer().sendMessage("MinSurrounding=" + SnowManager.getMinSurrounding(block, (byte) -1)); event.getPlayer().sendMessage("MaxSurrounding=" + SnowManager.getMaxSurrounding(block, (byte) -1)); event.getPlayer().sendMessage("canSnowBeAdded=" + SnowManager.canSnowBeAdded(block)); - event.getPlayer().sendMessage( - "canSnowBeAddedAbove=" + SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))); + event.getPlayer().sendMessage("canSnowBeAddedAbove=" + SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))); List snowBlocks = SnowManager.getBlocksToIncreaseUnder(event.getBlock()); for (Block blk : snowBlocks) { @@ -142,10 +142,8 @@ public void onBlockDamage(BlockDamageEvent event) { } else if (event.getItemInHand().getType() == Material.SNOW_BLOCK) { event.getPlayer().sendMessage("Increasing Snow Level"); - boolean canIncrease = false; - if (SnowManager.canSnowBeAdded(block)) { - canIncrease = true; - } else if (SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))) { + boolean canIncrease = SnowManager.canSnowBeAdded(block); + if (!canIncrease && SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))) { block = block.getRelative(BlockFace.UP); canIncrease = true; } @@ -172,8 +170,7 @@ public void onBlockDamage(BlockDamageEvent event) { } if (blk.getLightFromSky() >= 12) { // Melt it down - SnowManager.decreaseSnowLevel(new Location(event.getBlock().getWorld(), blk.getX(), blk.getY(), - blk.getZ())); + SnowManager.decreaseSnowLevel(blk); } } event.getPlayer().sendMessage("**"); diff --git a/src/main/java/com/zmanww/bukkit/SnowControl/SnowControl.java b/src/main/java/com/zmanww/bukkit/SnowControl/SnowControl.java index b4654fd..eeb10b5 100644 --- a/src/main/java/com/zmanww/bukkit/SnowControl/SnowControl.java +++ b/src/main/java/com/zmanww/bukkit/SnowControl/SnowControl.java @@ -19,17 +19,16 @@ */ package com.zmanww.bukkit.SnowControl; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.logging.Level; - import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.plugin.java.JavaPlugin; -import org.mcstats.Metrics; -public class SnowControl extends JavaPlugin implements Listener { +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Level; + +public class SnowControl extends JavaPlugin implements Listener +{ public static SnowControl plugin; private final PlayerListener playerListener = new PlayerListener(this); @@ -41,26 +40,22 @@ public class SnowControl extends JavaPlugin implements Listener { public static Map pendingCommand = new HashMap<>(); + private boolean debug = false; + private SnowMonitor snowMonitor; public void onEnable() { plugin = this; + load(); getServer().getPluginManager().registerEvents(playerListener, this); getServer().getPluginManager().registerEvents(worldListener, this); getCommand("snowcontrol").setExecutor(new CommandManager()); startScheduler(); - if(Config.getInstance().isMeltingEnabled()) - { - try - { - Metrics metrics = new Metrics(this); - metrics.start(); - } - catch(IOException e) - { - // Failed to submit the stats :-( - } - } + } + + public void load() + { + debug = Config.getInstance().debugEnabled(); } public void startScheduler() @@ -82,10 +77,11 @@ public void onDisable() { this.saveConfig(); } - public void debugLog(String string) { - if (Config.getInstance().debugEnabled()) { + public void debugLog(String string) + { + if (debug) + { this.getLogger().log(Level.INFO, " " + string); } - } } \ No newline at end of file diff --git a/src/main/java/com/zmanww/bukkit/SnowControl/SnowManager.java b/src/main/java/com/zmanww/bukkit/SnowControl/SnowManager.java index c13ca7c..06515a1 100644 --- a/src/main/java/com/zmanww/bukkit/SnowControl/SnowManager.java +++ b/src/main/java/com/zmanww/bukkit/SnowControl/SnowManager.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Random; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import org.bukkit.block.Biome; @@ -85,20 +84,10 @@ public static int getSnowDepth(Block block) { } public static boolean canSnowInBiome(Biome biome) { - if (biome.name().toUpperCase().contains("ICE_")) { - return true; - } - if (biome.name().toUpperCase().contains("COLD_")) { - return true; - } - if (biome.name().toUpperCase().contains("FROZEN_")) { - return true; - } - return false; + return biome.name().contains("ICE_") || biome.name().contains("COLD_") || biome.name().contains("_COLD") || biome.name().contains("FROZEN_"); } - public static void decreaseSnowLevel(Location loc) { - Block block = loc.getBlock(); + public static void decreaseSnowLevel(Block block) { if (block.getType() == Material.SNOW || block.getType() == Material.SNOW_BLOCK) { byte blkData = getSnowValue(block); if (blkData >= getMinSurrounding(block, (byte) 0) && blkData > getMaxSurrounding(block, (byte) 0, true) - 2) { @@ -252,10 +241,7 @@ public static boolean canAccumulateOn(final Block block) { // This is essentially a full block return true; } - if (Config.getInstance().canAccumulateOn.contains(block.getType())) { - return true; - } - return false; + return Config.getInstance().canAccumulateOn.contains(block.getType()); } public static boolean canSnowBeAdded(Block block) { @@ -263,9 +249,7 @@ public static boolean canSnowBeAdded(Block block) { return true; } if (canAccumulateOn(block.getRelative(BlockFace.DOWN))) { - if (Config.getInstance().canReplace.contains(block.getType())) { - return true; - } + return Config.getInstance().canReplace.contains(block.getType()); } return false; } diff --git a/src/main/java/com/zmanww/bukkit/SnowControl/SnowMonitor.java b/src/main/java/com/zmanww/bukkit/SnowControl/SnowMonitor.java index be2c5be..7011d4a 100644 --- a/src/main/java/com/zmanww/bukkit/SnowControl/SnowMonitor.java +++ b/src/main/java/com/zmanww/bukkit/SnowControl/SnowMonitor.java @@ -35,23 +35,32 @@ public class SnowMonitor extends BukkitRunnable { private SnowControl plugin; private Random rnd = new Random(); - private float chanceToAccumulate, chanceToMelt; + private float chanceToAccumulateBlock, chanceToMeltBlock; + private int blocksPerChunkToAccumulate, blocksPerChunkToMelt, maxChunks, minLightLevel; private boolean accumulation, melt; public SnowMonitor(SnowControl plugin) { super(); this.plugin = plugin; - chanceToAccumulate = (float) Math.sqrt(Config.getInstance().getChanceToAccumulate()) * 10f / 100f; - chanceToMelt = (float) Math.sqrt(Config.getInstance().getChanceToMelt()) * 10f / 100f; + chanceToAccumulateBlock = Config.getInstance().getChanceToAccumulate() / 100f; + chanceToMeltBlock = Config.getInstance().getChanceToMelt() / 100f; accumulation = Config.getInstance().isAccumulationEnabled(); melt = Config.getInstance().isMeltingEnabled(); + maxChunks = Config.getInstance().getMaxChunksPerCheck(); + minLightLevel = Config.getInstance().getMinLightLevel(); + + // Calculate stuff, we don't need to do this every time we run our loops + blocksPerChunkToAccumulate = (int) Math.ceil(256 * chanceToAccumulateBlock); + blocksPerChunkToMelt = (int) Math.ceil(256 * chanceToMeltBlock); } private Chunk[] getRandomElements(int count, Chunk... items) { if(count > items.length) count = items.length; + if(count == items.length) + return items; List list = new ArrayList<>(count); Chunk data; while(count > 0) @@ -63,7 +72,9 @@ private Chunk[] getRandomElements(int count, Chunk... items) count--; } } - return list.toArray(new Chunk[count]); + items = list.toArray(new Chunk[count]); + list.clear(); + return items; } private List generateRandom2DPosList(int count) @@ -75,11 +86,13 @@ private List generateRandom2DPosList(int minX, int maxX, int minY, int ma { if(count > (maxX - minX + 1) * (maxY - minY + 1)) count = (maxX - minX + 1) * (maxY - minY + 1); + maxX = maxX - minX + 1; + maxY = maxY - minY + 1; List list = new ArrayList<>(count); Pos2D randPos; while(count > 0) { - randPos = new Pos2D(rnd.nextInt(maxX - minX + 1) + minX, rnd.nextInt(maxY - minY + 1) + minY); + randPos = new Pos2D(rnd.nextInt(maxX) + minX, rnd.nextInt(maxY) + minY); if(!list.contains(randPos)) { list.add(randPos); @@ -122,27 +135,22 @@ public void run() for(String worldName : Config.getInstance().enabledWorlds) { World world = plugin.getServer().getWorld(worldName); - if(world == null) - continue; + if(world == null || !((accumulation && world.hasStorm()) || (melt && !world.hasStorm()))) continue; Chunk[] chunks = world.getLoadedChunks(); plugin.debugLog("Checking " + chunks.length + " chunks in " + worldName); + chunks = getRandomElements(Math.min(chunks.length, maxChunks), chunks); if(accumulation && world.hasStorm()) { - chunks = getRandomElements((int) Math.ceil(chunks.length * chanceToAccumulate), chunks); // I know it's not perfect, but we have to sacrifice precision for performance for(Chunk chunk : chunks) { - List blocks = generateRandom2DPosList((int) Math.ceil(256 * chanceToAccumulate)); // I know it's not perfect, but we have to sacrifice precision for performance + List blocks = generateRandom2DPosList(blocksPerChunkToAccumulate); for(Pos2D pos : blocks) { Block block = SnowManager.getHighestNonAirBlock(pos.getX() + chunk.getX() * 16, pos.getY() + chunk.getZ() * 16, world); if(block.getType() != Material.AIR && SnowManager.canSnowInBiome(block.getBiome())) { - boolean canIncrease = false; - if(SnowManager.canSnowBeAdded(block)) - { - canIncrease = true; - } - else if(SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))) + boolean canIncrease = SnowManager.canSnowBeAdded(block); + if(!canIncrease && SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))) { block = block.getRelative(BlockFace.UP); canIncrease = true; @@ -161,10 +169,9 @@ else if(SnowManager.canSnowBeAdded(block.getRelative(BlockFace.UP))) } else if(melt && !world.hasStorm()) { - chunks = getRandomElements((int) Math.ceil(chunks.length * chanceToMelt), chunks); // I know it's not perfect, but we have to sacrifice precision for performance for(final org.bukkit.Chunk chunk : chunks) { - List blocks = generateRandom2DPosList((int) Math.ceil(256 * chanceToMelt)); // I know it's not perfect, but we have to sacrifice precision for performance + List blocks = generateRandom2DPosList(blocksPerChunkToMelt); for(Pos2D pos : blocks) { Block block = SnowManager.getHighestNonAirBlock(pos.getX() + chunk.getX() * 16, pos.getY() + chunk.getZ() * 16, world); @@ -185,9 +192,9 @@ else if(melt && !world.hasStorm()) //noinspection deprecation blk.setData((byte) 7); } - if(blk.getType() == Material.SNOW && blk.getLightFromSky() >= Config.getInstance().getMinLightLevel()) + if(blk.getType() == Material.SNOW && blk.getLightFromSky() >= minLightLevel) { - SnowManager.decreaseSnowLevel(new Location(world, blk.getX(), blk.getY(), blk.getZ())); // Melt it down + SnowManager.decreaseSnowLevel(blk); // Melt it down } } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 74ffdc1..f696f5c 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -9,6 +9,7 @@ SnowFall: #this is only in biomes where snow falls naturally CheckEvery: 10 #number in seconds + MaxChunksPerWorldAndCheck: 441 #max number of chunks to check per world every check AccumulationEnabled: true AccumulationChance: 1 #percent 0-100 MaxAccumulationDefault: 7 # number of 'layers' of snow @@ -60,6 +61,4 @@ SnowFall: #this is only in biomes where snow falls naturally - SOIL - STONE - WOOD - - WOOL -# Please don't turn it off -Metrics: true \ No newline at end of file + - WOOL \ No newline at end of file