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

Did the requested changes, enabled and fixed JUnit test.

This commit is contained in:
LinoxGH 2020-06-28 16:42:46 +03:00
parent 61067c375c
commit ef1735985e
4 changed files with 71 additions and 62 deletions

View File

@ -1,6 +1,7 @@
package io.github.thebusybiscuit.slimefun4.api.events; package io.github.thebusybiscuit.slimefun4.api.events;
import org.apache.commons.lang.Validate; import org.apache.commons.lang.Validate;
import org.bukkit.block.Block;
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;
@ -26,13 +27,18 @@ public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable
private Vector velocity; private Vector velocity;
private final ClimbingPick pick; private final ClimbingPick pick;
private final ItemStack itemStack;
private final Block block;
private boolean cancelled; private boolean cancelled;
public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick) { public ClimbingPickLaunchEvent(Player player, Vector velocity, ClimbingPick pick, ItemStack itemStack, Block block) {
super(player); super(player);
this.velocity = velocity; this.velocity = velocity;
this.pick = pick; this.pick = pick;
this.itemStack = itemStack;
this.block = block;
} }
/** /**
@ -59,10 +65,28 @@ public class ClimbingPickLaunchEvent extends PlayerEvent implements Cancellable
/** /**
* This returns the {@link ClimbingPick} that was used. * This returns the {@link ClimbingPick} that was used.
* *
* @return The {@link ClimbingPick} that was used
*/
public ClimbingPick getPick() {
return this.pick;
}
/**
* This returns the {@link ItemStack} that was used.
*
* @return The {@link ItemStack} that was used * @return The {@link ItemStack} that was used
*/ */
public ClimbingPick getItem() { public ItemStack getItemStack() {
return this.pick; return this.itemStack;
}
/**
* This returns the {@link Block} that was climbed.
*
* @return The {@link Block} that was climbed
*/
public Block getBlock() {
return this.block;
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package io.github.thebusybiscuit.slimefun4.implementation.items.tools; package io.github.thebusybiscuit.slimefun4.implementation.items.tools;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -8,27 +9,26 @@ import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Effect;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections; import io.github.thebusybiscuit.cscorelib2.materials.MaterialCollections;
import io.github.thebusybiscuit.slimefun4.api.events.ClimbingPickLaunchEvent; import io.github.thebusybiscuit.slimefun4.api.events.ClimbingPickLaunchEvent;
import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem; import io.github.thebusybiscuit.slimefun4.core.attributes.DamageableItem;
import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SimpleSlimefunItem;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockBreakHandler;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler; import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
@ -40,15 +40,15 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
* @author Linox * @author Linox
* *
*/ */
public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements DamageableItem { public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements DamageableItem, RecipeDisplayItem {
private final Map<String, Double> materialSpeedsDef = new HashMap<>(); private final Map<String, Double> materialSpeeds;
private final ItemSetting<Map<String, Double>> materialSpeeds = new ItemSetting<>("launch-amounts", materialSpeedsDef);
private final Set<UUID> users = new HashSet<>(); private final Set<UUID> users = new HashSet<>();
public ClimbingPick(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { public ClimbingPick(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
Map<String, Double> materialSpeedsDef = new HashMap<>();
for (Material mat : MaterialCollections.getAllIceBlocks()) { for (Material mat : MaterialCollections.getAllIceBlocks()) {
materialSpeedsDef.put(mat.name(), 1D); materialSpeedsDef.put(mat.name(), 1D);
} }
@ -61,25 +61,16 @@ public class ClimbingPick extends SimpleSlimefunItem<ItemUseHandler> implements
materialSpeedsDef.put(Material.GRAVEL.name(), 0.4); materialSpeedsDef.put(Material.GRAVEL.name(), 0.4);
materialSpeedsDef.put(Material.SAND.name(), 0.4); materialSpeedsDef.put(Material.SAND.name(), 0.4);
materialSpeedsDef.put(Material.STONE.name(), 0.6D); materialSpeedsDef.put(Material.STONE.name(), 0.6);
materialSpeedsDef.put(Material.DIORITE.name(), 0.6D); materialSpeedsDef.put(Material.DIORITE.name(), 0.6);
materialSpeedsDef.put(Material.GRANITE.name(), 0.6D); materialSpeedsDef.put(Material.GRANITE.name(), 0.6);
materialSpeedsDef.put(Material.ANDESITE.name(), 0.6D); materialSpeedsDef.put(Material.ANDESITE.name(), 0.6);
materialSpeedsDef.put(Material.NETHERRACK.name(), 0.6D); materialSpeedsDef.put(Material.NETHERRACK.name(), 0.6);
if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) {
materialSpeedsDef.put(Material.BLACKSTONE.name(), 0.6D); materialSpeedsDef.put(Material.BLACKSTONE.name(), 0.6);
} }
materialSpeeds = SlimefunPlugin.getItemCfg().getOrSetDefault(getID() + ".launch-amounts", materialSpeedsDef);
addItemSetting(materialSpeeds);
} }
@Override
public void preRegister() {
super.preRegister();
addItemHandler(getBlockBreakHandler());
}
@Override @Override
public ItemUseHandler getItemHandler() { public ItemUseHandler getItemHandler() {
@ -90,58 +81,54 @@ 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().startsWith("TEST_CLIMBING_PICK") && p.getLocation().distance(block.getLocation()) > 1.5) return; if (p.getLocation().distanceSquared(block.getLocation()) > 2.25) 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())) {
Material mat = block.getType(); Material mat = block.getType();
Double launch = materialSpeeds.getValue().get(mat.name()); double launch = materialSpeeds.getOrDefault(mat.name(), 0D);
Vector velocity = new Vector(0, 0, 0); Vector velocity = new Vector(0, 0, 0);
if (launch != null) { if (launch > 0.05) {
Integer efficiencyLevel = item.getEnchantments().get(Enchantment.DIG_SPEED); int efficiencyLevel = item.getEnchantments().getOrDefault(Enchantment.DIG_SPEED, 0);
if (efficiencyLevel != null){ if (efficiencyLevel != 0){
launch += (efficiencyLevel * 0.2); launch += (efficiencyLevel * 0.2);
} }
velocity.setY(launch); velocity.setY(launch);
users.add(p.getUniqueId()); users.add(p.getUniqueId());
Bukkit.getScheduler().runTaskLaterAsynchronously(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L); Bukkit.getScheduler().runTaskLater(SlimefunPlugin.instance, () -> users.remove(p.getUniqueId()), 4L);
} }
ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this); ClimbingPickLaunchEvent event = new ClimbingPickLaunchEvent(p, velocity, this, item, block);
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
p.setVelocity(event.getVelocity()); if (!event.isCancelled()) {
p.playSound(p.getLocation(), Sound.ENTITY_ENDERMAN_TELEPORT, 1F, 1F); p.setVelocity(event.getVelocity());
p.getWorld().playEffect(block.getLocation(), Effect.STEP_SOUND, 1);
if (p.getGameMode() != GameMode.CREATIVE) { if (p.getGameMode() != GameMode.CREATIVE) {
damageItem(p, e.getItem()); damageItem(p, e.getItem());
}
} }
} }
}; };
} }
private BlockBreakHandler getBlockBreakHandler() {
return new BlockBreakHandler() {
@Override
public boolean onBlockBreak(BlockBreakEvent e, ItemStack item, int fortune, List<ItemStack> drops) {
if (isItem(item)) {
e.setCancelled(true);
return true;
}
return false;
}
@Override
public boolean isPrivate() {
return false;
}
};
}
@Override @Override
public boolean isDamageable() { public boolean isDamageable() {
return true; return true;
} }
@Override
public List<ItemStack> getDisplayRecipes() {
List<ItemStack> display = new ArrayList<>();
for (String matName : materialSpeeds.keySet()) {
Material mat = Material.getMaterial(matName);
if (mat == null) continue;
display.add(new CustomItem(mat, "&bCan Climb This Block"));
}
return display;
}
} }

View File

@ -430,7 +430,6 @@ public class SlimefunItem implements Placeable {
if (!handler.isPrivate()) { if (!handler.isPrivate()) {
Set<ItemHandler> handlerset = getPublicItemHandlers(handler.getIdentifier()); Set<ItemHandler> handlerset = getPublicItemHandlers(handler.getIdentifier());
handlerset.add(handler); handlerset.add(handler);
SlimefunPlugin.getRegistry().getPublicItemHandlers().replace(handler.getIdentifier(), handlerset);
} }
} }
} }

View File

@ -1,12 +1,12 @@
package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools; package io.github.thebusybiscuit.slimefun4.testing.tests.items.implementations.tools;
import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.Sound; 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.Disabled;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.params.provider.EnumSource;
@ -49,20 +49,19 @@ public class TestClimbingPick implements SlimefunItemTest<ClimbingPick> {
@ParameterizedTest @ParameterizedTest
@EnumSource(value = BlockFace.class) @EnumSource(value = BlockFace.class)
@Disabled("Player velocity is currently not fully implemented in MockBukkit")
public void testItemUse(BlockFace face) { public void testItemUse(BlockFace face) {
PlayerMock player = server.addPlayer(); PlayerMock player = server.addPlayer();
ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name()); ClimbingPick pick = registerSlimefunItem(plugin, "TEST_CLIMBING_PICK_" + face.name());
Location blockLocation = new Location(player.getLocation().getWorld(), player.getLocation().getBlockX() + 1, player.getLocation().getBlockY(), player.getLocation().getBlockZ());
boolean shouldCancel = false; boolean shouldCancel = face == BlockFace.DOWN || face == BlockFace.UP;
if (face == BlockFace.DOWN || face == BlockFace.UP) shouldCancel = true;
BlockMock block1 = new BlockMock(Material.STONE); BlockMock block1 = new BlockMock(Material.STONE, blockLocation);
simulateRightClickBlock(player, pick, block1, face); simulateRightClickBlock(player, pick, block1, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT);
BlockMock block2 = new BlockMock(Material.DIRT); BlockMock block2 = new BlockMock(Material.DIRT, blockLocation);
simulateRightClickBlock(player, pick, block2, face); simulateRightClickBlock(player, pick, block2, face);
server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class); server.getPluginManager().assertEventFired(ClimbingPickLaunchEvent.class);
if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT); if (!shouldCancel) player.assertSoundHeard(Sound.ENTITY_ENDERMAN_TELEPORT);