Item drop system rewrite

This commit is contained in:
Esophose 2019-02-06 20:38:12 -07:00
parent 83ceca627c
commit 78e7b4cd7c
4 changed files with 96 additions and 124 deletions

View File

@ -1,98 +1,56 @@
package com.songoda.ultimatetimber.treefall;
import com.songoda.ultimatetimber.utils.LeafToSaplingConverter;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import java.util.HashSet;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
class NoAnimationTreeDestroyer {
/*
Only ever triggers when people have tree falling animations off in the config
*/
static void destroyTree(HashSet<Block> blocks, boolean hasBonusLoot, boolean hasSilkTouch) {
Material leavesType = null;
if (!blocks.stream().filter(b -> b.getType() == Material.BROWN_MUSHROOM_BLOCK).collect(Collectors.toList()).isEmpty()) {
leavesType = Material.BROWN_MUSHROOM_BLOCK;
} else if (!blocks.stream().filter(b -> b.getType() == Material.RED_MUSHROOM_BLOCK).collect(Collectors.toList()).isEmpty()) {
leavesType = Material.RED_MUSHROOM_BLOCK;
}
// DEBUG ---- REMOVE
Set<Block> uniqueBlocks = new HashSet<>();
// Drop loot and plant a new sapling
for (Block block : blocks) {
Material material = LeafToSaplingConverter.convertLeaves(block.getType());
if (material.equals(Material.AIR) || material.equals(Material.VINE)) continue;
ItemStack toDrop = getItem(material);
if (material.equals(Material.ACACIA_SAPLING) ||
material.equals(Material.BIRCH_SAPLING) ||
material.equals(Material.DARK_OAK_SAPLING) ||
material.equals(Material.JUNGLE_SAPLING) ||
material.equals(Material.OAK_SAPLING) ||
material.equals(Material.SPRUCE_SAPLING)) {
if (ThreadLocalRandom.current().nextDouble() < 0.05) {
if (hasBonusLoot) {
block.getWorld().dropItem(block.getLocation(), toDrop.clone());
}
block.getWorld().dropItem(block.getLocation(), toDrop.clone());
block.setType(Material.AIR);
CustomLoot.doCustomItemDrop(block.getLocation());
continue;
} else {
block.setType(Material.AIR);
CustomLoot.doCustomItemDrop(block.getLocation());
continue;
}
}
if (hasSilkTouch) {
if (hasBonusLoot)
block.getWorld().dropItem(block.getLocation(), toDrop.clone());
block.getWorld().dropItem(block.getLocation(), toDrop.clone());
CustomLoot.doCustomItemDrop(block.getLocation());
block.setType(Material.AIR);
continue;
}
if (hasBonusLoot)
block.getWorld().dropItem(block.getLocation(), toDrop.clone());
block.getWorld().dropItem(block.getLocation(), toDrop.clone());
block.setType(Material.AIR);
CustomLoot.doCustomItemDrop(block.getLocation());
if (!uniqueBlocks.contains(block) && block.getType().name().contains("LOG"))
uniqueBlocks.add(block);
TreeLoot.dropTreeLoot(block.getBlockData(), block.getLocation().clone().add(0.5, 0.5, 0.5), hasBonusLoot, hasSilkTouch);
if (leavesType != null) {
TreeReplant.replaceOriginalBlock(block, leavesType);
} else {
TreeReplant.replaceOriginalBlock(block);
}
}
// DEBUG ---- REMOVE
Bukkit.broadcastMessage(uniqueBlocks.size() + "");
}
static ItemStack getItem(Material material) {
if (material == Material.BROWN_MUSHROOM_BLOCK) {
return new ItemStack(Material.BROWN_MUSHROOM, 1);
} else if (material == Material.RED_MUSHROOM_BLOCK) {
return new ItemStack(Material.RED_MUSHROOM, 1);
}
return new ItemStack(material, 1);
}
}

View File

@ -2,6 +2,8 @@ package com.songoda.ultimatetimber.treefall;
import com.songoda.ultimatetimber.UltimateTimber;
import com.songoda.ultimatetimber.configurations.DefaultConfig;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.block.Block;
@ -123,18 +125,16 @@ public class TreeFallAnimation implements Listener {
setHasSilkTouch(false);
for (Block block : blocks) {
FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation().clone().add(0.5, 0, 0.5), block.getBlockData());
fallingBlock.setDropItem(false);
registerFallingBlock(fallingBlock);
/*
Dropping air causes some issues
*/
if (block.getType().equals(Material.AIR)) continue;
FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation().clone().add(0.5, 0, 0.5), block.getBlockData());
fallingBlock.setDropItem(false);
registerFallingBlock(fallingBlock);
/*
Remove original block
*/
@ -233,7 +233,7 @@ public class TreeFallAnimation implements Listener {
FallingBlock fallingBlock = (FallingBlock) event.getEntity();
runFallingBlockImpact(fallingBlock);
}
private void runFallingBlockImpact(FallingBlock fallingBlock) {
@ -243,13 +243,12 @@ public class TreeFallAnimation implements Listener {
if (treeFallAnimation.getAllFallingBlocks().isEmpty())
unregisterTreeFallAnimation();
UltimateTimber plugin = UltimateTimber.getInstance();
FileConfiguration fileConfiguration = plugin.getConfig();
FileConfiguration fileConfiguration = UltimateTimber.getInstance().getConfig();
/*
Run block fall aftermath
*/
TreeLoot.convertFallingBlock(fallingBlock, treeFallAnimation.hasBonusLoot(), treeFallAnimation.hasSilkTouch());
TreeLoot.dropTreeLoot(fallingBlock.getBlockData(), fallingBlock.getLocation(), treeFallAnimation.hasBonusLoot(), treeFallAnimation.hasSilkTouch());
if (UltimateTimber.getInstance().getConfig().getBoolean(DefaultConfig.REPLANT_FROM_LEAVES))
TreeReplant.leafFallReplant(fallingBlock);
if (fileConfiguration.getBoolean(DefaultConfig.DAMAGE_PLAYERS))

View File

@ -47,11 +47,14 @@ public class TreeFallListener implements Listener {
//Call event that tree will fall
TreeFallEvent treeFallEvent = new TreeFallEvent(event.getPlayer(), treeChecker, event.getBlock());
Bukkit.getPluginManager().callEvent(treeFallEvent);
if(treeFallEvent.isCancelled()) return;
if (treeFallEvent.isCancelled()) return;
/*
Everything beyond this point assumes that the tree was valid
*/
// Do not let any items drop, it will be handled later
event.setDropItems(false);
if (fileConfiguration.getBoolean(DefaultConfig.ACCURATE_AXE_DURABILITY))
AxeDurability.adjustAxeDamage(blocks, event.getPlayer());

View File

@ -1,68 +1,80 @@
package com.songoda.ultimatetimber.treefall;
import com.songoda.ultimatetimber.utils.LeafToSaplingConverter;
import java.util.Random;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.FallingBlock;
import org.bukkit.World;
import org.bukkit.block.data.BlockData;
import org.bukkit.inventory.ItemStack;
import java.util.concurrent.ThreadLocalRandom;
import com.songoda.ultimatetimber.utils.LeafToSaplingConverter;
class TreeLoot {
static void convertFallingBlock(FallingBlock fallingBlock, boolean hasBonusLoot, boolean hasSilkTouch) {
Material material = LeafToSaplingConverter.convertLeaves(fallingBlock.getBlockData().getMaterial());
if (material.equals(Material.VINE))
static Random random = new Random();
static void dropTreeLoot(BlockData blockData, Location location, boolean hasBonusLoot, boolean hasSilkTouch) {
World world = location.getWorld();
Material originalMaterial = blockData.getMaterial();
Material material = LeafToSaplingConverter.convertLeaves(originalMaterial);
if (hasSilkTouch) { // No bonus loot for silk touch
world.dropItem(location, new ItemStack(originalMaterial, 1));
return;
if (hasSilkTouch) {
}
switch (material) {
case VINE:
case MUSHROOM_STEM:
break;
case BROWN_MUSHROOM_BLOCK:
case RED_MUSHROOM_BLOCK:
boolean isRed = material.equals(Material.RED_MUSHROOM_BLOCK);
int numToDrop = Math.max(0, random.nextInt(10) - 7); // 80% chance to drop nothing, 10% chance for 1, 10% chance for 2
if (numToDrop != 0)
world.dropItem(location, new ItemStack(isRed ? Material.RED_MUSHROOM : Material.BROWN_MUSHROOM, numToDrop));
break;
case ACACIA_SAPLING:
case BIRCH_SAPLING:
case SPRUCE_SAPLING:
boolean dropChance = random.nextInt(20) == 0; // 1/20 chance to drop sapling
if (dropChance)
world.dropItem(location, new ItemStack(material, 1));
if (hasBonusLoot)
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(fallingBlock.getBlockData().getMaterial(), 1));
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(fallingBlock.getBlockData().getMaterial(), 1));
CustomLoot.doCustomItemDrop(fallingBlock.getLocation());
return;
CustomLoot.doCustomItemDrop(location);
break;
case JUNGLE_SAPLING:
boolean jungleDropChance = random.nextInt(40) == 0; // 1/40 chance to drop sapling
if (jungleDropChance)
world.dropItem(location, new ItemStack(material, 1));
if (hasBonusLoot)
CustomLoot.doCustomItemDrop(location);
break;
case DARK_OAK_SAPLING:
case OAK_SAPLING:
boolean oakDropChance = random.nextInt(20) == 0; // 1/20 chance to drop sapling
if (oakDropChance)
world.dropItem(location, new ItemStack(material, 1));
boolean appleDropChance = random.nextInt(200) == 0; // 1/200 chance to drop apple
if (appleDropChance)
world.dropItem(location, new ItemStack(Material.APPLE, 1));
if (hasBonusLoot)
CustomLoot.doCustomItemDrop(location);
break;
default:
world.dropItem(location, new ItemStack(material, 1));
break;
}
if (material.equals(Material.BROWN_MUSHROOM_BLOCK)) {
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(Material.BROWN_MUSHROOM, 1));
return;
}
if (material.equals(Material.RED_MUSHROOM_BLOCK)) {
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(Material.RED_MUSHROOM, 1));
return;
}
if (material.equals(Material.MUSHROOM_STEM)) {
return;
}
if (material.equals(Material.ACACIA_SAPLING) ||
material.equals(Material.BIRCH_SAPLING) ||
material.equals(Material.DARK_OAK_SAPLING) ||
material.equals(Material.JUNGLE_SAPLING) ||
material.equals(Material.OAK_SAPLING) ||
material.equals(Material.SPRUCE_SAPLING)) {
if (ThreadLocalRandom.current().nextDouble() < 0.05) {
if (hasBonusLoot) {
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(material, 1));
}
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(material, 1));
CustomLoot.doCustomItemDrop(fallingBlock.getLocation());
return;
} else {
CustomLoot.doCustomItemDrop(fallingBlock.getLocation());
return;
}
}
if (hasBonusLoot)
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(material, 1));
fallingBlock.getWorld().dropItem(fallingBlock.getLocation(), new ItemStack(material, 1));
}