mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 19:55:48 +00:00
Did the requested changes.
This commit is contained in:
parent
9fcbe94553
commit
bc074c83ee
@ -1,9 +1,11 @@
|
|||||||
package io.github.thebusybiscuit.slimefun4.api.events;
|
package io.github.thebusybiscuit.slimefun4.api.events;
|
||||||
|
|
||||||
|
import org.apache.commons.lang.Validate;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.Cancellable;
|
import org.bukkit.event.Cancellable;
|
||||||
import org.bukkit.event.Event;
|
import org.bukkit.event.Event;
|
||||||
import org.bukkit.event.HandlerList;
|
import org.bukkit.event.HandlerList;
|
||||||
|
import org.bukkit.event.player.PlayerEvent;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.util.Vector;
|
import org.bukkit.util.Vector;
|
||||||
|
|
||||||
@ -18,42 +20,23 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.tools.ClimbingPic
|
|||||||
* @see ClimbingPick
|
* @see ClimbingPick
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ClimbingPickLaunchEvent extends Event implements Cancellable {
|
public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable {
|
||||||
|
|
||||||
private static final HandlerList handlers = new HandlerList();
|
private static final HandlerList handlers = new HandlerList();
|
||||||
|
|
||||||
private final Player player;
|
private Vector velocity;
|
||||||
private final Vector velocity;
|
private final ClimbingPick pick;
|
||||||
private final ItemStack pick;
|
|
||||||
private boolean cancelled;
|
private boolean cancelled;
|
||||||
|
|
||||||
public ClimbingPickLaunchEvent(Player player, Vector velocity, ItemStack pick) {
|
public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick) {
|
||||||
super(false);
|
super(player);
|
||||||
|
|
||||||
this.player = player;
|
|
||||||
this.velocity = velocity;
|
this.velocity = velocity;
|
||||||
this.pick = pick;
|
this.pick = pick;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HandlerList getHandlers() {
|
|
||||||
return handlers;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static HandlerList getHandlerList() {
|
|
||||||
return handlers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This returns the {@link Player} that used the {@link ClimbingPick}.
|
* This returns the velocity {@link Vector} that was applied to the {@link Player}
|
||||||
*
|
|
||||||
* @return The {@link Player} that used
|
|
||||||
*/
|
|
||||||
public Player getPlayer() {
|
|
||||||
return player;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This returns the {@link Vector} velocity that was applied to the {@link Player}
|
|
||||||
* who used the {@link ClimbingPick}.
|
* who used the {@link ClimbingPick}.
|
||||||
*
|
*
|
||||||
* @return The {@link Vector} of the applied velocity
|
* @return The {@link Vector} of the applied velocity
|
||||||
@ -63,11 +46,22 @@ public class ClimbingPickLaunchEvent extends Event implements Cancellable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This returns the {@link ClimbingPick} {@link ItemStack} that was used.
|
* Use this to change the velocity {@link Vector} applied to the {@link Player}.
|
||||||
|
*
|
||||||
|
* @param velocity
|
||||||
|
* The {@link Vector} velocity to apply
|
||||||
|
*/
|
||||||
|
public void setVelocity(Vector velocity) {
|
||||||
|
Validate.notNull(velocity);
|
||||||
|
this.velocity = velocity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This returns the {@link ClimbingPick} that was used.
|
||||||
*
|
*
|
||||||
* @return The {@link ItemStack} that was used
|
* @return The {@link ItemStack} that was used
|
||||||
*/
|
*/
|
||||||
public ItemStack getItem() {
|
public ClimbingPick getItem() {
|
||||||
return this.pick;
|
return this.pick;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,4 +75,8 @@ public class ClimbingPickLaunchEvent extends Event implements Cancellable {
|
|||||||
this.cancelled = cancel;
|
this.cancelled = cancel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public HandlerList getHandlers() {
|
||||||
|
return handlers;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -91,7 +91,7 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements
|
|||||||
ItemStack item = e.getItem();
|
ItemStack item = e.getItem();
|
||||||
Player p = e.getPlayer();
|
Player p = e.getPlayer();
|
||||||
|
|
||||||
if (!getID().equals("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return;
|
if (!getID().startsWith("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return;
|
||||||
if (e.getClickedFace() == BlockFace.DOWN || e.getClickedFace() == BlockFace.UP) return;
|
if (e.getClickedFace() == BlockFace.DOWN || e.getClickedFace() == BlockFace.UP) return;
|
||||||
|
|
||||||
if (!users.contains(p.getUniqueId())) {
|
if (!users.contains(p.getUniqueId())) {
|
||||||
@ -110,7 +110,7 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements
|
|||||||
Bukkit.getScheduler().runTaskLaterAsynchronously(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L);
|
Bukkit.getScheduler().runTaskLaterAsynchronously(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L);
|
||||||
}
|
}
|
||||||
|
|
||||||
ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, item);
|
ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this);
|
||||||
Bukkit.getPluginManager().callEvent(event);
|
Bukkit.getPluginManager().callEvent(event);
|
||||||
|
|
||||||
p.setVelocity(event.getVelocity());
|
p.setVelocity(event.getVelocity());
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
|
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
|
||||||
|
|
||||||
|
import org.bukkit.block.Block;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
|
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
|
||||||
@ -7,12 +8,13 @@ import me.mrCookieSlime.Slimefun.Objects.Category;
|
|||||||
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
|
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link ExplosivePickaxe} is a pickaxe which can destroy blocks in a size of 3 by 3.
|
* The {@link ExplosivePickaxe} is a pickaxe which can destroy {@link Block}s
|
||||||
* It also creates a explosion animation.
|
* in a size of 3 by 3. It also creates a explosion animation.
|
||||||
*
|
*
|
||||||
* @author TheBusyBiscuit
|
* @author TheBusyBiscuit
|
||||||
*
|
*
|
||||||
* @see ExplosiveShovel
|
* @see ExplosiveShovel
|
||||||
|
* @see ExplosiveTool
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class ExplosivePickaxe extends ExplosiveTool {
|
public class ExplosivePickaxe extends ExplosiveTool {
|
||||||
|
@ -21,6 +21,7 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
|
|||||||
*
|
*
|
||||||
* @author Linox
|
* @author Linox
|
||||||
*
|
*
|
||||||
|
* @see ExplosivePickaxe
|
||||||
* @see ExplosiveTool
|
* @see ExplosiveTool
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -59,7 +59,7 @@ class ExplosiveTool extends SimpleSlimefunItem<BlockBreakHandler> implements Not
|
|||||||
|
|
||||||
List<Block> blocks = findBlocks(e.getBlock());
|
List<Block> blocks = findBlocks(e.getBlock());
|
||||||
|
|
||||||
if (callExplosionEvent.getValue()) {
|
if (callExplosionEvent.getValue().booleanValue()) {
|
||||||
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0);
|
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0);
|
||||||
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);
|
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);
|
||||||
|
|
||||||
|
@ -36,14 +36,15 @@ public interface SlimefunItemTest<T extends SlimefunItem> {
|
|||||||
item.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(event));
|
item.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
default void simulateConsumption(Player player, T item) {
|
|
||||||
PlayerItemConsumeEvent event = new PlayerItemConsumeEvent(player, item.getItem().clone());
|
|
||||||
item.callItemHandler(ItemConsumptionHandler.class, handler -> handler.onConsume(event, player, event.getItem()));
|
|
||||||
}
|
|
||||||
|
|
||||||
default void simulateRightClickBlock(Player player, T item, BlockMock block, BlockFace face) {
|
default void simulateRightClickBlock(Player player, T item, BlockMock block, BlockFace face) {
|
||||||
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item.getItem().clone(), block, face, EquipmentSlot.HAND);
|
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item.getItem().clone(), block, face, EquipmentSlot.HAND);
|
||||||
PlayerRightClickEvent event = new PlayerRightClickEvent(e);
|
PlayerRightClickEvent event = new PlayerRightClickEvent(e);
|
||||||
item.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(event));
|
item.callItemHandler(ItemUseHandler.class, handler -> handler.onRightClick(event));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void simulateConsumption(Player player, T item) {
|
||||||
|
PlayerItemConsumeEvent event = new PlayerItemConsumeEvent(player, item.getItem().clone());
|
||||||
|
item.callItemHandler(ItemConsumptionHandler.class, handler -> handler.onConsume(event, player, event.getItem()));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,11 +1,13 @@
|
|||||||
package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools;
|
package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools;
|
||||||
|
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.Sound;
|
||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.junit.jupiter.api.AfterAll;
|
import org.junit.jupiter.api.AfterAll;
|
||||||
import org.junit.jupiter.api.BeforeAll;
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.EnumSource;
|
||||||
|
|
||||||
import be.seeseemelk.mockbukkit.MockBukkit;
|
import be.seeseemelk.mockbukkit.MockBukkit;
|
||||||
import be.seeseemelk.mockbukkit.ServerMock;
|
import be.seeseemelk.mockbukkit.ServerMock;
|
||||||
@ -23,7 +25,6 @@ public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
|
|||||||
|
|
||||||
private static ServerMock server;
|
private static ServerMock server;
|
||||||
private static SlimefunPlugin plugin;
|
private static SlimefunPlugin plugin;
|
||||||
private static ClimbingPick pick;
|
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
public static void load() {
|
public static void load() {
|
||||||
@ -45,19 +46,23 @@ public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
|
|||||||
return pick;
|
return pick;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@ParameterizedTest
|
||||||
public void testItemUse() {
|
@EnumSource(value = BlockFace.class)
|
||||||
|
public void testItemUse(BlockFace face) {
|
||||||
PlayerMock player = server.addPlayer();
|
PlayerMock player = server.addPlayer();
|
||||||
if (pick == null) pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK");
|
ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name());
|
||||||
|
|
||||||
|
boolean shouldCancel = false;
|
||||||
|
if (face == BlockFace.DOWN || face == BlockFace.UP) shouldCancel = true;
|
||||||
|
|
||||||
for (BlockFace face : BlockFace.values()) {
|
|
||||||
BlockMock block1 = new BlockMock(Material.STONE);
|
BlockMock block1 = new BlockMock(Material.STONE);
|
||||||
simulateRightClickBlock(player, pick, block1, face);
|
simulateRightClickBlock(player, pick, block1, face);
|
||||||
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, e -> !e.isCancelled());
|
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
|
||||||
|
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT);
|
||||||
|
|
||||||
BlockMock block2 = new BlockMock(Material.DIRT);
|
BlockMock block2 = new BlockMock(Material.DIRT);
|
||||||
simulateRightClickBlock(player, pick, block2, face);
|
simulateRightClickBlock(player, pick, block2, face);
|
||||||
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, ClimbingPickLaunchEvent::isCancelled);
|
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
|
||||||
}
|
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user