diff --git a/src/com/massivecraft/mcore/util/IdUtil.java b/src/com/massivecraft/mcore/util/IdUtil.java index 003c6324..e5667d4e 100644 --- a/src/com/massivecraft/mcore/util/IdUtil.java +++ b/src/com/massivecraft/mcore/util/IdUtil.java @@ -958,7 +958,8 @@ public class IdUtil implements Listener, Runnable } catch (Exception e) { - e.printStackTrace(); + String message = Txt.parse("NBT file couldn't be loaded: %s. Error: %s.", file.toString(), e.getMessage()); + MCore.get().log(message); return null; } } diff --git a/src/com/massivecraft/mcore/util/MUtil.java b/src/com/massivecraft/mcore/util/MUtil.java index 51f234d4..69dfd742 100644 --- a/src/com/massivecraft/mcore/util/MUtil.java +++ b/src/com/massivecraft/mcore/util/MUtil.java @@ -24,6 +24,7 @@ import java.util.TreeSet; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.command.CommandSender; import org.bukkit.entity.Entity; @@ -142,6 +143,46 @@ public class MUtil return recurseResolveMap(output, map); } + // -------------------------------------------- // + // GET BLOCKS + // -------------------------------------------- // + + public static List getBlocks(Location location, int halfWidth) + { + return getBlocks(location.getBlock(), halfWidth); + } + + public static List getBlocks(Block block, int halfWidth) + { + int xmin = block.getX() - halfWidth; + int ymin = block.getY() - halfWidth; + int zmin = block.getZ() - halfWidth; + + int xmax = block.getX() + halfWidth; + int ymax = block.getY() + halfWidth; + int zmax = block.getZ() + halfWidth; + + return getBlocks(block.getWorld(), xmin, ymin, zmin, xmax, ymax, zmax); + } + + public static List getBlocks(World world, int xmin, int ymin, int zmin, int xmax, int ymax, int zmax) + { + List blocks = new ArrayList(); + + for (int x = xmin; x <= xmax; x++) + { + for (int y = ymin; y <= ymax; y++) + { + for (int z = zmin; z <= zmax; z++) + { + blocks.add(world.getBlockAt(x, y, z)); + } + } + } + + return blocks; + } + // -------------------------------------------- // // LOCATIONS COMPARISON // -------------------------------------------- // diff --git a/src/com/massivecraft/mcore/util/PeriodUtil.java b/src/com/massivecraft/mcore/util/PeriodUtil.java new file mode 100644 index 00000000..9f4bbe33 --- /dev/null +++ b/src/com/massivecraft/mcore/util/PeriodUtil.java @@ -0,0 +1,69 @@ +package com.massivecraft.mcore.util; + +import java.util.HashMap; +import java.util.Map; + +public class PeriodUtil +{ + // -------------------------------------------- // + // MILLIS STORE + // -------------------------------------------- // + + private static Map objectToMillis = new HashMap(); + + public static long getMillis(Object object) + { + Long ret = objectToMillis.get(object); + if (ret == null) ret = 0L; + return ret; + } + + public static void setMillis(Object object, Long millis) + { + if (millis == null || millis == 0) + { + objectToMillis.remove(object); + } + else + { + objectToMillis.put(object, millis); + } + } + + // -------------------------------------------- // + // RANDOM SIMPLE + // -------------------------------------------- // + + public static long getPeriod(long length, long now) + { + return now / length; + } + + public static long getPeriod(long length) + { + return getPeriod(length, System.currentTimeMillis()); + } + + public static long getLastPeriod(Object object, long length) + { + return getPeriod(length, getMillis(object)); + } + + public static boolean isNewPeriod(Object object, long length, long now) + { + long currentPeriod = getPeriod(length, now); + long lastPeriod = getLastPeriod(object, length); + + if (currentPeriod == lastPeriod) return false; + + setMillis(object, now); + + return true; + } + + public static boolean isNewPeriod(Object object, long length) + { + return isNewPeriod(object, length, System.currentTimeMillis()); + } + +}