1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added a toggle for #1924 and fixes #1933

This commit is contained in:
TheBusyBiscuit 2020-05-24 01:11:10 +02:00
parent bbc851a117
commit 0af8c41eeb
4 changed files with 157 additions and 182 deletions

View File

@ -24,6 +24,7 @@
* Added /sf backpack to restore lost backpacks
* Added automated Unit Tests
* Added WaypointCreateEvent
* Added an option to call an explosion event when using explosive tools
#### Changes
* Little performance improvements
@ -55,6 +56,7 @@
* Fixed file errors with PerWorldSettingsService
* Fixed ChestTerminals deleting items from Cargo networks (TheBusyBiscuit/ChestTerminal#25)
* Fixed #1926
* Fixed #1933
## Release Candidate 11 (25 Apr 2020)

View File

@ -1,125 +1,24 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.HandledBlock;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
public class ExplosivePickaxe extends SimpleSlimefunItem<BlockBreakHandler> implements NotPlaceable, DamageableItem {
private final ItemSetting<Boolean> damageOnUse = new ItemSetting<>("damage-on-use", true);
/**
* The {@link ExplosivePickaxe} is a pickaxe which can destroy blocks in a size of 3 by 3.
* It also creates a explosion animation.
*
* @author TheBusyBiscuit
*
* @see ExplosiveShovel
*
*/
public class ExplosivePickaxe extends ExplosiveTool {
public ExplosivePickaxe(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
addItemSetting(damageOnUse);
}
@Override
public BlockBreakHandler getItemHandler() {
return new BlockBreakHandler() {
@Override
public boolean isPrivate() {
return false;
}
@Override
public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
if (isItem(item)) {
if (Slimefun.hasUnlocked(e.getPlayer(), ExplosivePickaxe.this, true)) {
e.getBlock().getWorld().createExplosion(e.getBlock().getLocation(), 0.0F);
e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.3F, 1F);
List<Block> blocks = new ArrayList<>();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
// We can skip the center block since that will break as usual
if (x == 0 && y == 0 && z == 0) {
continue;
}
blocks.add(e.getBlock().getRelative(x, y, z));
}
}
}
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0);
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);
if (!blockExplodeEvent.isCancelled()) {
blockExplodeEvent.blockList().forEach(b -> breakBlock(e.getPlayer(), item, b, fortune, drops));
}
}
return true;
}
else {
return false;
}
}
};
}
private void breakBlock(Player p, ItemStack item, Block b, int fortune, List<ItemStack> drops) {
if (b.getType() != Material.AIR && !b.isLiquid() && !MaterialCollections.getAllUnbreakableBlocks().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK);
b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
SlimefunItem sfItem = BlockStorage.check(b);
if (sfItem != null && !(sfItem instanceof HandledBlock)) {
SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getID());
if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) {
drops.add(BlockStorage.retrieve(b));
}
}
else if (b.getType() == Material.PLAYER_HEAD || b.getType().name().endsWith("_SHULKER_BOX")) {
b.breakNaturally();
}
else {
for (ItemStack drop : b.getDrops(getItem())) {
b.getWorld().dropItemNaturally(b.getLocation(), (b.getType().toString().endsWith("_ORE") && b.getType() != Material.IRON_ORE && b.getType() != Material.GOLD_ORE) ? new CustomItem(drop, fortune) : drop);
}
b.setType(Material.AIR);
}
damageItem(p, item);
}
}
@Override
public boolean isDamageable() {
return damageOnUse.getValue();
}
}

View File

@ -1,28 +1,18 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.materials.MaterialTools;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
@ -34,77 +24,28 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
* @see ExplosivePickaxe
*
*/
public class ExplosiveShovel extends SimpleSlimefunItem<BlockBreakHandler> implements NotPlaceable, DamageableItem {
private final ItemSetting<Boolean> damageOnUse = new ItemSetting<>("damage-on-use", true);
public class ExplosiveShovel extends ExplosiveTool {
public ExplosiveShovel(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
addItemSetting(damageOnUse);
}
@Override
public BlockBreakHandler getItemHandler() {
return new BlockBreakHandler() {
protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List<ItemStack> drops) {
if (MaterialTools.getBreakableByShovel().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK);
@Override
public boolean isPrivate() {
return false;
}
b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
@Override
public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
if (isItem(item)) {
if (Slimefun.hasUnlocked(e.getPlayer(), ExplosiveShovel.this, true)) {
e.getBlock().getWorld().createExplosion(e.getBlock().getLocation(), 0.0F);
e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.3F, 1F);
List<Block> blocks = new ArrayList<>();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
if (x == 0 && y == 0 && z == 0) {
continue;
}
blocks.add(e.getBlock().getRelative(x, y, z));
}
}
}
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0);
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);
if (!blockExplodeEvent.isCancelled()) {
blockExplodeEvent.blockList().forEach(b -> {
if (MaterialTools.getBreakableByShovel().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(e.getPlayer(), b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
SlimefunPlugin.getProtectionManager().logAction(e.getPlayer(), b, ProtectableAction.BREAK_BLOCK);
b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
for (ItemStack drop : b.getDrops(getItem())) {
if (drop != null) {
b.getWorld().dropItemNaturally(b.getLocation(), drop);
}
}
b.setType(Material.AIR);
damageItem(e.getPlayer(), item);
}
});
}
}
return true;
for (ItemStack drop : b.getDrops(getItem())) {
if (drop != null) {
b.getWorld().dropItemNaturally(b.getLocation(), drop);
}
else return false;
}
};
}
@Override
public boolean isDamageable() {
return damageOnUse.getValue();
b.setType(Material.AIR);
damageItem(p, item);
}
}
}

View File

@ -0,0 +1,133 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections;
import io.github.thebusybiscuit.cscorelib2.protection.ProtectableAction;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunBlockHandler;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.HandledBlock;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.UnregisterReason;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.api.BlockStorage;
import me.mrCookieSlime.Slimefun.api.Slimefun;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
class ExplosiveTool extends SimpleSlimefunItem<BlockBreakHandler> implements NotPlaceable, DamageableItem {
private final ItemSetting<Boolean> damageOnUse = new ItemSetting<>("damage-on-use", true);
private final ItemSetting<Boolean> callExplosionEvent = new ItemSetting<>("call-explosion-event", false);
public ExplosiveTool(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe);
addItemSetting(damageOnUse, callExplosionEvent);
}
@Override
public BlockBreakHandler getItemHandler() {
return new BlockBreakHandler() {
@Override
public boolean isPrivate() {
return false;
}
@Override
public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
if (isItem(item)) {
if (Slimefun.hasUnlocked(e.getPlayer(), ExplosiveTool.this, true)) {
e.getBlock().getWorld().createExplosion(e.getBlock().getLocation(), 0.0F);
e.getBlock().getWorld().playSound(e.getBlock().getLocation(), Sound.ENTITY_GENERIC_EXPLODE, 0.2F, 1F);
List<Block> blocks = new ArrayList<>();
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
for (int z = -1; z <= 1; z++) {
// We can skip the center block since that will break as usual
if (x == 0 && y == 0 && z == 0) {
continue;
}
blocks.add(e.getBlock().getRelative(x, y, z));
}
}
}
if (callExplosionEvent.getValue().booleanValue()) {
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0);
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);
if (!blockExplodeEvent.isCancelled()) {
blockExplodeEvent.blockList().forEach(b -> breakBlock(e.getPlayer(), item, b, fortune, drops));
}
}
else {
for (Block b : blocks) {
breakBlock(e.getPlayer(), item, b, fortune, drops);
}
}
}
return true;
}
else {
return false;
}
}
};
}
@Override
public boolean isDamageable() {
return damageOnUse.getValue();
}
protected void breakBlock(Player p, ItemStack item, Block b, int fortune, List<ItemStack> drops) {
if (b.getType() != Material.AIR && !b.isLiquid() && !MaterialCollections.getAllUnbreakableBlocks().contains(b.getType()) && SlimefunPlugin.getProtectionManager().hasPermission(p, b.getLocation(), ProtectableAction.BREAK_BLOCK)) {
SlimefunPlugin.getProtectionManager().logAction(p, b, ProtectableAction.BREAK_BLOCK);
b.getWorld().playEffect(b.getLocation(), Effect.STEP_SOUND, b.getType());
SlimefunItem sfItem = BlockStorage.check(b);
if (sfItem != null && !(sfItem instanceof HandledBlock)) {
SlimefunBlockHandler handler = SlimefunPlugin.getRegistry().getBlockHandlers().get(sfItem.getID());
if (handler != null && !handler.onBreak(p, b, sfItem, UnregisterReason.PLAYER_BREAK)) {
drops.add(BlockStorage.retrieve(b));
}
}
else if (b.getType() == Material.PLAYER_HEAD || b.getType().name().endsWith("_SHULKER_BOX")) {
b.breakNaturally();
}
else {
for (ItemStack drop : b.getDrops(getItem())) {
b.getWorld().dropItemNaturally(b.getLocation(), (b.getType().toString().endsWith("_ORE") && b.getType() != Material.IRON_ORE && b.getType() != Material.GOLD_ORE) ? new CustomItem(drop, fortune) : drop);
}
b.setType(Material.AIR);
}
damageItem(p, item);
}
}
}