1
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:
LinoxGH 2020-06-27 02:22:17 +03:00
parent 9fcbe94553
commit bc074c83ee
7 changed files with 58 additions and 51 deletions

View File

@ -1,9 +1,11 @@
package io.github.thebusybiscuit.slimefun4.api.events;
import org.apache.commons.lang.Validate;
import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;
@ -18,42 +20,23 @@ import io.github.thebusybiscuit.slimefun4.implementation.items.tools.ClimbingPic
* @see ClimbingPick
*
*/
public class ClimbingPickLaunchEvent extends Event implements Cancellable {
public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final Vector velocity;
private final ItemStack pick;
private Vector velocity;
private final ClimbingPick pick;
private boolean cancelled;
public ClimbingPickLaunchEvent(Player player, Vector velocity, ItemStack pick) {
super(false);
public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick) {
super(player);
this.player = player;
this.velocity = velocity;
this.pick = pick;
}
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
/**
* This returns the {@link Player} that used the {@link ClimbingPick}.
*
* @return The {@link Player} that used
*/
public Player getPlayer() {
return player;
}
/**
* This returns the {@link Vector} velocity that was applied to the {@link Player}
* This returns the velocity {@link Vector} that was applied to the {@link Player}
* who used the {@link ClimbingPick}.
*
* @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
*/
public ItemStack getItem() {
public ClimbingPick getItem() {
return this.pick;
}
@ -81,4 +75,8 @@ public class ClimbingPickLaunchEvent extends Event implements Cancellable {
this.cancelled = cancel;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
}

View File

@ -91,7 +91,7 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements
ItemStack item = e.getItem();
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 (!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);
}
ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, item);
ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this);
Bukkit.getPluginManager().callEvent(event);
p.setVelocity(event.getVelocity());

View File

@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
import org.bukkit.block.Block;
import org.bukkit.inventory.ItemStack;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
@ -7,12 +8,13 @@ import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
/**
* The {@link ExplosivePickaxe} is a pickaxe which can destroy blocks in a size of 3 by 3.
* It also creates a explosion animation.
* The {@link ExplosivePickaxe} is a pickaxe which can destroy {@link Block}s
* in a size of 3 by 3. It also creates a explosion animation.
*
* @author TheBusyBiscuit
*
* @see ExplosiveShovel
* @see ExplosiveTool
*
*/
public class ExplosivePickaxe extends ExplosiveTool {

View File

@ -21,6 +21,7 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
*
* @author Linox
*
* @see ExplosivePickaxe
* @see ExplosiveTool
*
*/

View File

@ -59,7 +59,7 @@ class ExplosiveTool extends SimpleSlimefunItem<BlockBreakHandler> implements Not
List<Block> blocks = findBlocks(e.getBlock());
if (callExplosionEvent.getValue()) {
if (callExplosionEvent.getValue().booleanValue()) {
BlockExplodeEvent blockExplodeEvent = new BlockExplodeEvent(e.getBlock(), blocks, 0);
Bukkit.getServer().getPluginManager().callEvent(blockExplodeEvent);

View File

@ -36,14 +36,15 @@ public interface SlimefunItemTest<T extends SlimefunItem> {
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) {
PlayerInteractEvent e = new PlayerInteractEvent(player, Action.RIGHT_CLICK_BLOCK, item.getItem().clone(), block, face, EquipmentSlot.HAND);
PlayerRightClickEvent event = new PlayerRightClickEvent(e);
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()));
}
}

View File

@ -1,11 +1,13 @@
package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
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.ServerMock;
@ -23,7 +25,6 @@ public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
private static ServerMock server;
private static SlimefunPlugin plugin;
private static ClimbingPick pick;
@BeforeAll
public static void load() {
@ -45,19 +46,23 @@ public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
return pick;
}
@Test
public void testItemUse() {
@ParameterizedTest
@EnumSource(value = BlockFace.class)
public void testItemUse(BlockFace face) {
PlayerMock player = server.addPlayer();
if (pick == null) pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK");
ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name());
for (BlockFace face : BlockFace.values()) {
BlockMock block1 = new BlockMock(Material.STONE);
simulateRightClickBlock(player, pick, block1, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, e -> !e.isCancelled());
boolean shouldCancel = false;
if (face == BlockFace.DOWN || face == BlockFace.UP) shouldCancel = true;
BlockMock block2 = new BlockMock(Material.DIRT);
simulateRightClickBlock(player, pick, block2, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class, ClimbingPickLaunchEvent::isCancelled);
}
BlockMock block1 = new BlockMock(Material.STONE);
simulateRightClickBlock(player, pick, block1, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT);
BlockMock block2 = new BlockMock(Material.DIRT);
simulateRightClickBlock(player, pick, block2, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT);
}
}