Skip to content

Commit

Permalink
Issue #822: Tweak terra-forming to allow for different weight dependi…
Browse files Browse the repository at this point in the history
…ng on tool-type
  • Loading branch information
rlf committed Jul 10, 2016
1 parent 03c45a0 commit dbadfe1
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -40,6 +43,7 @@
public class NetherTerraFormEvents implements Listener {
private final uSkyBlock plugin;
private final Map<Material,List<MaterialUtil.MaterialProbability>> terraFormMap = new HashMap<>();
private final Map<String, Double> toolWeights = new HashMap<>();
private static final Random RND = new Random(System.currentTimeMillis());
private final double maxScan;
private final double chanceWither;
Expand All @@ -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) {
Expand Down Expand Up @@ -96,20 +106,32 @@ 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());
v.subtract(new Vector(playerLocation.getX(), playerLocation.getY(), playerLocation.getZ()));
v.normalize();
// Disable spawning above the player... enabling the player to clear a region
if (v.getY() <= 0.76) {
List<Material> yield = getYield(block.getType());
List<Material> 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)) {
Expand Down Expand Up @@ -187,10 +209,10 @@ private List<Location> getLocationsInPlane(Location location, Vector v) {
return locs;
}

public List<Material> getYield(Material material) {
public List<Material> getYield(Material material, double toolWeight) {
List<Material> 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());
}
}
Expand All @@ -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)) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,31 @@ public enum MaterialUtil {
;
private static final Pattern MATERIAL_PROBABILITY = Pattern.compile("(\\{p=(?<prob>0\\.[0-9]+)\\})?\\s*(?<id>[A-Z_0-9]+)");
private static final Collection<Material> SANDS = Arrays.asList(Material.SAND, Material.GRAVEL);
private static final Collection<Material> WOOD_TOOLS = Arrays.asList(Material.WOOD_AXE, Material.WOOD_HOE, Material.WOOD_PICKAXE, Material.WOOD_SPADE, Material.WOOD_SWORD);
private static final Collection<Material> STONE_TOOLS = Arrays.asList(Material.STONE_AXE, Material.STONE_HOE, Material.STONE_PICKAXE, Material.STONE_SPADE, Material.STONE_SWORD);
private static final Collection<Material> IRON_TOOLS = Arrays.asList(Material.IRON_AXE, Material.IRON_HOE, Material.IRON_PICKAXE, Material.IRON_SPADE, Material.IRON_SWORD);
private static final Collection<Material> GOLD_TOOLS = Arrays.asList(Material.GOLD_AXE, Material.GOLD_HOE, Material.GOLD_PICKAXE, Material.GOLD_SPADE, Material.GOLD_SWORD);
private static final Collection<Material> DIAMOND_TOOLS = Arrays.asList(Material.DIAMOND_AXE, Material.DIAMOND_HOE, Material.DIAMOND_PICKAXE, Material.DIAMOND_SPADE, Material.DIAMOND_SWORD);
private static final Collection<Material> 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 {
Expand Down
11 changes: 10 additions & 1 deletion uSkyBlock-Core/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}

0 comments on commit dbadfe1

Please sign in to comment.