diff --git a/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/event/NetherTerraFormEvents.java b/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/event/NetherTerraFormEvents.java index 6e0e74eb5..452e02a4e 100644 --- a/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/event/NetherTerraFormEvents.java +++ b/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/event/NetherTerraFormEvents.java @@ -18,7 +18,10 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.ItemSpawnEvent; +import org.bukkit.event.player.PlayerDropItemEvent; import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import us.talabrek.ultimateskyblock.uSkyBlock; import us.talabrek.ultimateskyblock.util.LocationUtil; @@ -40,6 +43,7 @@ public class NetherTerraFormEvents implements Listener { private final uSkyBlock plugin; private final Map> terraFormMap = new HashMap<>(); + private final Map toolWeights = new HashMap<>(); private static final Random RND = new Random(System.currentTimeMillis()); private final double maxScan; private final double chanceWither; @@ -64,6 +68,12 @@ public NetherTerraFormEvents(uSkyBlock plugin) { } } } + config = plugin.getConfig().getConfigurationSection("nether.terraform-weight"); + if (config != null) { + for (String tool : config.getKeys(false)) { + toolWeights.put(tool, config.getDouble(tool, 1d)); + } + } maxScan = plugin.getConfig().getInt("nether.terraform-distance", 7); config = plugin.getConfig().getConfigurationSection("nether.spawn-chances"); if (config != null) { @@ -96,6 +106,12 @@ public void onBlockBreak(BlockBreakEvent event) { if (!terraFormMap.containsKey(block.getType())) { return; // Not a block we terra-form on. } + // TODO: 10/07/2016 - R4zorax: Handle dual-wielding (would break 1.8 compatibility) + ItemStack tool = event.getPlayer().getItemInHand(); + if (tool == null || event.getBlock().getDrops(tool).isEmpty()) { + return; // Only terra-form when stuff is mined correctly + } + double toolWeight = getToolWeight(tool); Location playerLocation = player.getEyeLocation(); Location blockLocation = LocationUtil.centerInBlock(block.getLocation()); Vector v = new Vector(blockLocation.getX(), blockLocation.getY(), blockLocation.getZ()); @@ -103,13 +119,19 @@ public void onBlockBreak(BlockBreakEvent event) { v.normalize(); // Disable spawning above the player... enabling the player to clear a region if (v.getY() <= 0.76) { - List yield = getYield(block.getType()); + List yield = getYield(block.getType(), toolWeight); for (Material mat : yield) { spawnBlock(mat, blockLocation, v); } } } + private Double getToolWeight(ItemStack tool) { + String toolType = MaterialUtil.getToolType(tool.getType()); + Double d = toolWeights.get(toolType); + return d != null ? d : 1d; + } + private void spawnBlock(Material type, Location location, Vector v) { Location spawnLoc = null; if (MaterialUtil.isFallingMaterial(type)) { @@ -187,10 +209,10 @@ private List getLocationsInPlane(Location location, Vector v) { return locs; } - public List getYield(Material material) { + public List getYield(Material material, double toolWeight) { List copy = new ArrayList<>(); for (MaterialUtil.MaterialProbability e : terraFormMap.get(material)) { - if (RND.nextDouble() < e.getProbability()) { + if (RND.nextDouble() < e.getProbability()*toolWeight) { copy.add(e.getMaterial()); } } @@ -211,20 +233,23 @@ public void onGhastExplode(EntityExplodeEvent event) { } } - @EventHandler + /** + * Comes AFTER the SpawnEvents{@link #onCreatureSpawn(CreatureSpawnEvent)} - so cancelled will have effect + * @param e + */ + @EventHandler(priority = EventPriority.HIGH) public void onCreatureSpawn(CreatureSpawnEvent e) { - if (!spawnEnabled ||e == null || e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL || e.getEntity() == null) { + if (!spawnEnabled || e == null || e.isCancelled() || e.getSpawnReason() != CreatureSpawnEvent.SpawnReason.NATURAL || e.getEntity() == null) { return; } if (!plugin.isSkyNether(e.getLocation().getWorld())) { return; } - if (e.getLocation().getBlockY() > 127) { + if (e.getLocation().getBlockY() > plugin.getSkyBlockNetherWorld().getMaxHeight()) { // Block spawning above nether... e.setCancelled(true); return; } - // TODO: 23/09/2015 - R4zorax: obey the spawn-limits if (e.getEntity() instanceof PigZombie) { Block block = e.getLocation().getBlock().getRelative(BlockFace.DOWN); if (isNetherFortressWalkway(block)) { @@ -234,6 +259,7 @@ public void onCreatureSpawn(CreatureSpawnEvent e) { // Spawn Wither. Skeleton mob = (Skeleton) e.getLocation().getWorld().spawnEntity(e.getLocation(), EntityType.SKELETON); mob.setSkeletonType(Skeleton.SkeletonType.WITHER); + mob.getEquipment().setItemInHand(new ItemStack(Material.STONE_SWORD, 1)); } else if (p <= chanceWither+chanceBlaze) { // Spawn Blaze Blaze mob = (Blaze) e.getLocation().getWorld().spawnEntity(e.getLocation(), EntityType.BLAZE); diff --git a/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/util/MaterialUtil.java b/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/util/MaterialUtil.java index e8fa972ae..91055b3d8 100644 --- a/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/util/MaterialUtil.java +++ b/uSkyBlock-Core/src/main/java/us/talabrek/ultimateskyblock/util/MaterialUtil.java @@ -18,6 +18,31 @@ public enum MaterialUtil { ; private static final Pattern MATERIAL_PROBABILITY = Pattern.compile("(\\{p=(?0\\.[0-9]+)\\})?\\s*(?[A-Z_0-9]+)"); private static final Collection SANDS = Arrays.asList(Material.SAND, Material.GRAVEL); + private static final Collection WOOD_TOOLS = Arrays.asList(Material.WOOD_AXE, Material.WOOD_HOE, Material.WOOD_PICKAXE, Material.WOOD_SPADE, Material.WOOD_SWORD); + private static final Collection STONE_TOOLS = Arrays.asList(Material.STONE_AXE, Material.STONE_HOE, Material.STONE_PICKAXE, Material.STONE_SPADE, Material.STONE_SWORD); + private static final Collection IRON_TOOLS = Arrays.asList(Material.IRON_AXE, Material.IRON_HOE, Material.IRON_PICKAXE, Material.IRON_SPADE, Material.IRON_SWORD); + private static final Collection GOLD_TOOLS = Arrays.asList(Material.GOLD_AXE, Material.GOLD_HOE, Material.GOLD_PICKAXE, Material.GOLD_SPADE, Material.GOLD_SWORD); + private static final Collection DIAMOND_TOOLS = Arrays.asList(Material.DIAMOND_AXE, Material.DIAMOND_HOE, Material.DIAMOND_PICKAXE, Material.DIAMOND_SPADE, Material.DIAMOND_SWORD); + private static final Collection TOOLS = new ArrayList<>(); + static { + TOOLS.addAll(WOOD_TOOLS); + TOOLS.addAll(STONE_TOOLS); + TOOLS.addAll(IRON_TOOLS); + TOOLS.addAll(GOLD_TOOLS); + TOOLS.addAll(DIAMOND_TOOLS); + } + + public static boolean isTool(Material type) { + return TOOLS.contains(type); + } + + public static String getToolType(Material tool) { + if (isTool(tool)) { + String enumName = tool.name(); + return enumName.substring(0, enumName.indexOf('_')); + } + return null; + } public static Material getMaterial(String name, Material fallback) { try { diff --git a/uSkyBlock-Core/src/main/resources/config.yml b/uSkyBlock-Core/src/main/resources/config.yml index ebb9213e9..ded749999 100644 --- a/uSkyBlock-Core/src/main/resources/config.yml +++ b/uSkyBlock-Core/src/main/resources/config.yml @@ -338,6 +338,15 @@ nether: - '{p=0.85}GLOWSTONE' - '{p=0.15}GLOWSTONE' + # Weights that is applied to the above terraform chances depending on the tool used + terraform-weight: + WOOD: 1.1 + STONE: 1.0 + IRON: 0.9 + GOLD: 0 + DIAMOND: 0.5 + + # The chances of changing a pigzombie when spawned on a netherbrick spawn-chances: enabled: true wither: 0.20 @@ -369,7 +378,7 @@ placeholder: servercommandplaceholder: false # DO NOT TOUCH THE FIELDS BELOW -version: 52 +version: 53 force-replace: options.party.invite-timeout: 100 options.island.islandTeleportDelay: 5 diff --git a/uSkyBlock-Core/src/test/java/us/talabrek/ultimateskyblock/util/MaterialUtilTest.java b/uSkyBlock-Core/src/test/java/us/talabrek/ultimateskyblock/util/MaterialUtilTest.java new file mode 100644 index 000000000..38e85f797 --- /dev/null +++ b/uSkyBlock-Core/src/test/java/us/talabrek/ultimateskyblock/util/MaterialUtilTest.java @@ -0,0 +1,60 @@ +package us.talabrek.ultimateskyblock.util; + +import org.bukkit.Material; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; +import static org.junit.Assert.*; + +public class MaterialUtilTest { + @Test + public void testIsTool() { + assertThat(MaterialUtil.isTool(Material.WOOD), is(false)); + assertThat(MaterialUtil.isTool(Material.STONE_BUTTON), is(false)); + assertThat(MaterialUtil.isTool(Material.DIAMOND_BARDING), is(false)); + assertThat(MaterialUtil.isTool(Material.GOLDEN_CARROT), is(false)); + assertThat(MaterialUtil.isTool(Material.WOOD_SWORD), is(true)); + assertThat(MaterialUtil.isTool(Material.DIAMOND_AXE), is(true)); + assertThat(MaterialUtil.isTool(Material.STONE_PICKAXE), is(true)); + } + + @Test + public void testGetToolType() { + assertThat(MaterialUtil.getToolType(Material.AIR), nullValue()); + assertThat(MaterialUtil.getToolType(Material.IRON_BARDING), nullValue()); + assertThat(MaterialUtil.getToolType(Material.GOLD_HELMET), nullValue()); + assertThat(MaterialUtil.getToolType(Material.STONE_SLAB2), nullValue()); + assertThat(MaterialUtil.getToolType(Material.GOLD_ORE), nullValue()); + + assertThat(MaterialUtil.getToolType(Material.GOLD_SWORD), is("GOLD")); + assertThat(MaterialUtil.getToolType(Material.GOLD_AXE), is("GOLD")); + assertThat(MaterialUtil.getToolType(Material.GOLD_HOE), is("GOLD")); + assertThat(MaterialUtil.getToolType(Material.GOLD_PICKAXE), is("GOLD")); + assertThat(MaterialUtil.getToolType(Material.GOLD_SPADE), is("GOLD")); + + assertThat(MaterialUtil.getToolType(Material.STONE_SWORD), is("STONE")); + assertThat(MaterialUtil.getToolType(Material.STONE_AXE), is("STONE")); + assertThat(MaterialUtil.getToolType(Material.STONE_HOE), is("STONE")); + assertThat(MaterialUtil.getToolType(Material.STONE_PICKAXE), is("STONE")); + assertThat(MaterialUtil.getToolType(Material.STONE_SPADE), is("STONE")); + + assertThat(MaterialUtil.getToolType(Material.IRON_SWORD), is("IRON")); + assertThat(MaterialUtil.getToolType(Material.IRON_AXE), is("IRON")); + assertThat(MaterialUtil.getToolType(Material.IRON_HOE), is("IRON")); + assertThat(MaterialUtil.getToolType(Material.IRON_PICKAXE), is("IRON")); + assertThat(MaterialUtil.getToolType(Material.IRON_SPADE), is("IRON")); + + assertThat(MaterialUtil.getToolType(Material.WOOD_SWORD), is("WOOD")); + assertThat(MaterialUtil.getToolType(Material.WOOD_AXE), is("WOOD")); + assertThat(MaterialUtil.getToolType(Material.WOOD_HOE), is("WOOD")); + assertThat(MaterialUtil.getToolType(Material.WOOD_PICKAXE), is("WOOD")); + assertThat(MaterialUtil.getToolType(Material.WOOD_SPADE), is("WOOD")); + + assertThat(MaterialUtil.getToolType(Material.DIAMOND_SWORD), is("DIAMOND")); + assertThat(MaterialUtil.getToolType(Material.DIAMOND_AXE), is("DIAMOND")); + assertThat(MaterialUtil.getToolType(Material.DIAMOND_HOE), is("DIAMOND")); + assertThat(MaterialUtil.getToolType(Material.DIAMOND_PICKAXE), is("DIAMOND")); + assertThat(MaterialUtil.getToolType(Material.DIAMOND_SPADE), is("DIAMOND")); + } +} \ No newline at end of file