Skip to content

Commit

Permalink
Ore drops affected by Fortune enchantment
Browse files Browse the repository at this point in the history
  • Loading branch information
aramperes committed Nov 20, 2016
1 parent 36cd546 commit 8f39334
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/main/java/net/glowstone/block/GlowBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,15 +450,18 @@ public String toString() {
* @return true if the block was destroyed
*/
public boolean breakNaturally(float yield) {
return breakNaturally(yield, ItemTable.instance().getBlock(getType()).getMinedDrops(this));
}

public boolean breakNaturally(float yield, Collection<ItemStack> drops) {
Random r = new Random();

if (getType() == Material.AIR) {
return false;
}

Location location = getLocation();
Collection<ItemStack> toDrop = ItemTable.instance().getBlock(getType()).getMinedDrops(this);
toDrop.stream().filter(stack -> r.nextFloat() < yield).forEach(stack -> getWorld().dropItemNaturally(location, stack));
drops.stream().filter(stack -> r.nextFloat() < yield).forEach(stack -> getWorld().dropItemNaturally(location, stack));

setType(Material.AIR);
return true;
Expand All @@ -471,8 +474,9 @@ public boolean breakNaturally() {

@Override
public boolean breakNaturally(ItemStack tool) {
if (!getDrops(tool).isEmpty()) {
return breakNaturally();
Collection<ItemStack> drops = getDrops(tool);
if (!drops.isEmpty()) {
return breakNaturally(1.0f, drops);
} else {
return setTypeId(Material.AIR.getId());
}
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/glowstone/block/ItemTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ private void registerBuiltins() {
reg(Material.MOSSY_COBBLESTONE, new BlockDirectDrops(ToolType.PICKAXE));
reg(Material.STONE, new BlockStone());
reg(Material.OBSIDIAN, new BlockDirectDrops(ToolType.DIAMOND_PICKAXE));
reg(Material.COAL_ORE, new BlockDirectDrops(Material.COAL, ToolType.PICKAXE));
reg(Material.COAL_ORE, new BlockOre(Material.COAL, ToolType.PICKAXE));
reg(Material.COAL_BLOCK, new BlockDirectDrops(ToolType.PICKAXE));
reg(Material.IRON_ORE, new BlockDirectDrops(ToolType.STONE_PICKAXE));
reg(Material.IRON_BLOCK, new BlockDirectDrops(ToolType.STONE_PICKAXE));
reg(Material.GOLD_ORE, new BlockDirectDrops(ToolType.IRON_PICKAXE));
reg(Material.GOLD_BLOCK, new BlockDirectDrops(ToolType.IRON_PICKAXE));
reg(Material.DIAMOND_ORE, new BlockDirectDrops(Material.DIAMOND, ToolType.IRON_PICKAXE));
reg(Material.DIAMOND_ORE, new BlockOre(Material.DIAMOND, ToolType.IRON_PICKAXE));
reg(Material.DIAMOND_BLOCK, new BlockDirectDrops(ToolType.IRON_PICKAXE));
reg(Material.EMERALD_ORE, new BlockDirectDrops(Material.EMERALD, ToolType.IRON_PICKAXE));
reg(Material.EMERALD_ORE, new BlockOre(Material.EMERALD, ToolType.IRON_PICKAXE));
reg(Material.EMERALD_BLOCK, new BlockDirectDrops(ToolType.PICKAXE));
reg(Material.LAPIS_ORE, new BlockRandomDrops(Material.INK_SACK, 4, 4, 8, ToolType.STONE_PICKAXE));
reg(Material.LAPIS_ORE, new BlockOre(Material.INK_SACK, ToolType.STONE_PICKAXE, 4, 4, 8));
reg(Material.LAPIS_BLOCK, new BlockDirectDrops(ToolType.STONE_PICKAXE));
reg(Material.QUARTZ_ORE, new BlockDirectDrops(Material.QUARTZ, ToolType.PICKAXE));
reg(Material.QUARTZ_ORE, new BlockOre(Material.QUARTZ, ToolType.PICKAXE));
reg(Material.REDSTONE_ORE, new BlockRedstoneOre());
reg(Material.GLOWING_REDSTONE_ORE, new BlockLitRedstoneOre());
reg(Material.REDSTONE_BLOCK, new BlockDirectDrops(ToolType.PICKAXE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected MaterialMatcher getNeededMiningTool(GlowBlock block) {
return neededTool;
}

private ItemStack getDrops(GlowBlock block) {
protected ItemStack getDrops(GlowBlock block) {
if (dropType == null) {
return new ItemStack(block.getType(), amount, block.getData());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public abstract class BlockNeedsTool extends BlockType {
@Override
public final Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) {
public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) {
MaterialMatcher neededTool = getNeededMiningTool(block);
if (neededTool != null &&
(tool == null || !neededTool.matches(tool.getType())))
Expand Down
102 changes: 102 additions & 0 deletions src/main/java/net/glowstone/block/blocktype/BlockOre.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package net.glowstone.block.blocktype;

import net.glowstone.block.GlowBlock;
import net.glowstone.inventory.MaterialMatcher;
import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemStack;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

public class BlockOre extends BlockNeedsTool {
private final Material dropType;
private final MaterialMatcher neededTool;
private final int minCount, maxCount;
private final int data;

public BlockOre(Material dropType, MaterialMatcher neededTool, int data, int minCount, int maxCount) {
this.dropType = dropType;
this.neededTool = neededTool;
this.minCount = minCount;
this.maxCount = maxCount;
this.data = data;
}

public BlockOre(Material dropType, MaterialMatcher neededTool, int data, int count) {
this(dropType, neededTool, data, count, count);
}

public BlockOre(Material dropType, MaterialMatcher neededTool, int data) {
this(dropType, neededTool, data, 1);
}

public BlockOre(Material dropType, MaterialMatcher neededTool) {
this(dropType, neededTool, 1, 1);
}

public Collection<ItemStack> getDrops(GlowBlock block, ItemStack tool) {
ThreadLocalRandom random = ThreadLocalRandom.current();
int count = minCount;
if (maxCount > minCount) {
count += random.nextInt(maxCount - minCount);
}
ItemStack stack = new ItemStack(dropType, count, (short) data);
if (tool == null) {
return Collections.unmodifiableList(Arrays.asList(stack));
}
Collection<ItemStack> drops = super.getDrops(block, tool);
if (drops.size() == 0) {
return drops;
}
if (tool.containsEnchantment(Enchantment.LOOT_BONUS_BLOCKS)) {
stack.setAmount(count * getMultiplicator(random, tool.getEnchantmentLevel(Enchantment.LOOT_BONUS_BLOCKS)));
}
return Collections.unmodifiableList(Arrays.asList(stack));
}

@Override
public Collection<ItemStack> getMinedDrops(GlowBlock block) {
return getDrops(block, null);
}

@Override
protected MaterialMatcher getNeededMiningTool(GlowBlock block) {
return neededTool;
}

private int getMultiplicator(Random random, int level) {
if (level <= 0) {
return 1;
}
double r = random.nextDouble();
if (level == 1) {
if (r < 0.33) {
return 2;
}
}
if (level == 2) {
if (r < 0.25) {
return 2;
}
if (r < 0.5) {
return 3;
}
}
if (level == 3) {
if (r < 0.2) {
return 2;
}
if (r < 0.4) {
return 3;
}
if (r < 0.6) {
return 4;
}
}
return 1;
}
}

0 comments on commit 8f39334

Please sign in to comment.