Moved obsidian scooping to be a world flag in settings

This commit is contained in:
tastybento 2019-01-03 09:46:56 -08:00
parent a5831947c2
commit 5351b162c5
8 changed files with 134 additions and 89 deletions

View File

@ -18,7 +18,6 @@ import world.bentobox.bentobox.listeners.BlockEndDragon;
import world.bentobox.bentobox.listeners.DeathListener;
import world.bentobox.bentobox.listeners.JoinLeaveListener;
import world.bentobox.bentobox.listeners.NetherPortals;
import world.bentobox.bentobox.listeners.ObsidianScoopingListener;
import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager;
@ -192,8 +191,6 @@ public class BentoBox extends JavaPlugin {
manager.registerEvents(new PanelListenerManager(), this);
// Nether portals
manager.registerEvents(new NetherPortals(this), this);
// Obsidian to lava helper
manager.registerEvents(new ObsidianScoopingListener(this), this);
// End dragon blocking
manager.registerEvents(new BlockEndDragon(this), this);
// Banned visitor commands

View File

@ -146,15 +146,6 @@ public interface WorldSettings {
*/
String getWorldName();
/**
* Allow obsidian to be scooped up with an empty bucket back into lava.
* This only works if there is a single block of obsidian (no obsidian within 10 blocks).
* Recommendation is to keep this true so that newbies don't bother you or reset their
* island unnecessarily.
* @return whether obsidian scooping is allowed or not.
*/
boolean isAllowObsidianScooping();
/**
* @return the dragonSpawn
*/

View File

@ -9,28 +9,20 @@ import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.flags.FlagListener;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags;
/**
* Enables changing of obsidian back into lava
* For {@link world.bentobox.bentobox.lists.Flags#OBSIDIAN_SCOOPING}
* @author tastybento
*/
public class ObsidianScoopingListener implements Listener {
private BentoBox plugin;
/**
* @param plugin plugin
*/
public ObsidianScoopingListener(BentoBox plugin) {
this.plugin = plugin;
}
public class ObsidianScoopingListener extends FlagListener {
/**
* Enables changing of obsidian back into lava
@ -40,8 +32,8 @@ public class ObsidianScoopingListener implements Listener {
*/
@EventHandler(priority = EventPriority.NORMAL)
public boolean onPlayerInteract(final PlayerInteractEvent e) {
if (!plugin.getIWM().inWorld(e.getPlayer().getLocation())
|| !plugin.getIWM().isAllowObsidianScooping(e.getPlayer().getWorld())
if (!getIWM().inWorld(e.getPlayer().getLocation())
|| !Flags.OBSIDIAN_SCOOPING.isSetForWorld(e.getPlayer().getWorld())
|| !e.getPlayer().getGameMode().equals(GameMode.SURVIVAL)
|| !e.getAction().equals(Action.RIGHT_CLICK_BLOCK)
|| !(e.getItem() != null && e.getItem().getType().equals(Material.BUCKET))
@ -49,7 +41,7 @@ public class ObsidianScoopingListener implements Listener {
return false;
}
User user = User.getInstance(e.getPlayer());
if (plugin.getIslands().userIsOnIsland(user.getWorld(), user)) {
if (getIslands().userIsOnIsland(user.getWorld(), user)) {
// Look around to see if this is a lone obsidian block
Block b = e.getClickedBlock();

View File

@ -10,6 +10,7 @@ import org.bukkit.Material;
import world.bentobox.bentobox.api.flags.Flag;
import world.bentobox.bentobox.api.flags.Flag.Type;
import world.bentobox.bentobox.api.flags.clicklisteners.CycleClick;
import world.bentobox.bentobox.listeners.ObsidianScoopingListener;
import world.bentobox.bentobox.listeners.flags.BlockInteractionListener;
import world.bentobox.bentobox.listeners.flags.BreakBlocksListener;
import world.bentobox.bentobox.listeners.flags.BreedingListener;
@ -138,7 +139,7 @@ public final class Flags {
public static final Flag LEASH = new Flag.Builder("LEASH", Material.LEAD).listener(new LeashListener()).build();
// Portal use protection
public static final Flag NETHER_PORTAL = new Flag.Builder("NETHER_PORTAL", Material.OBSIDIAN).listener(new PortalListener()).build();
public static final Flag NETHER_PORTAL = new Flag.Builder("NETHER_PORTAL", Material.NETHERRACK).listener(new PortalListener()).build();
public static final Flag END_PORTAL = new Flag.Builder("END_PORTAL", Material.END_PORTAL_FRAME).listener(new PortalListener()).build();
// Shearing
@ -238,6 +239,8 @@ public final class Flags {
public static final Flag PREVENT_TELEPORT_WHEN_FALLING = new Flag.Builder("PREVENT_TELEPORT_WHEN_FALLING", Material.FEATHER).type(Type.WORLD_SETTING).build();
public static final Flag OBSIDIAN_SCOOPING = new Flag.Builder("OBSIDIAN_SCOOPING", Material.OBSIDIAN).type(Type.WORLD_SETTING)
.listener(new ObsidianScoopingListener()).defaultSetting(true).build();
/**
* @return List of all the flags in this class
*/

View File

@ -251,13 +251,6 @@ public class IslandWorldManager {
return gameModes.get(Util.getWorld(world)).getWorldSettings().getWorldName();
}
/**
* @return whether obsidian scooping is allowed or not.
*/
public boolean isAllowObsidianScooping(World world) {
return gameModes.get(Util.getWorld(world)).getWorldSettings().isAllowObsidianScooping();
}
/**
* @return the endGenerate
*/

View File

@ -659,6 +659,13 @@ protection:
description: "Toggle use"
name: "Note block"
hint: "No note block use"
OBSIDIAN_SCOOPING:
name: "Obsidian scooping"
description: |
Toggle scooping
Allow obsidian to be scooped up
with an empty bucket back into lava.
Protects newbies. Reduces resets.
OFFLINE_REDSTONE:
description: |-
&aWhen disabled, redstone

View File

@ -561,6 +561,14 @@ protection:
description: "トグル使用"
name: "音符ブロック"
hint: "音符ブロックを使用しない"
OBSIDIAN_SCOOPING:
name: "黒曜石スクープ"
description: |
スクープを切り替える
黒曜石を空のバケツですくい上げて
溶岩に戻すことができます。
初心者を保護します。
リセットを減らします。
OFFLINE_REDSTONE:
description: |-
&aレッドストーンは

View File

@ -1,4 +1,4 @@
package world.bentobox.bentobox.listeners;
package world.bentobox.bentobox.listeners.flags;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@ -7,9 +7,10 @@ import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
@ -25,33 +26,44 @@ import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.plugin.PluginManager;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.bentobox.api.configuration.WorldSettings;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.listeners.ObsidianScoopingListener;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest({PlayerEvent.class, PlayerInteractEvent.class})
@PrepareForTest({BentoBox.class, PlayerEvent.class, PlayerInteractEvent.class})
public class ObsidianScoopingListenerTest {
private static World world;
private World world;
private ObsidianScoopingListener listener;
private ItemStack item;
private Block clickedBlock;
private BentoBox plugin;
private Player who;
private IslandWorldManager iwm;
private IslandsManager im;
private LocalesManager lm;
private Material inHand;
private Material block;
@Test
public void testObsidianToLava() {
BentoBox plugin = mock(BentoBox.class);
assertNotNull(new ObsidianScoopingListener(plugin));
}
@Before
public void setUp() throws Exception {
// Set up plugin
plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
@Test
public void testOnPlayerInteract() {
// Mock world
world = mock(World.class);
@ -69,16 +81,13 @@ public class ObsidianScoopingListenerTest {
when(server.getItemFactory()).thenReturn(itemFactory);
// Set the server to the mock
Bukkit.setServer(server);
// Mock plugin
BentoBox plugin = mock(BentoBox.class);
//Bukkit.setServer(server);
// Create new object
ObsidianScoopingListener listener = new ObsidianScoopingListener(plugin);
listener = new ObsidianScoopingListener();
// Mock player
Player who = mock(Player.class);
who = mock(Player.class);
when(who.getWorld()).thenReturn(world);
Location location = mock(Location.class);
@ -91,24 +100,25 @@ public class ObsidianScoopingListenerTest {
when(who.getInventory()).thenReturn(mock(PlayerInventory.class));
// Worlds
IslandWorldManager iwm = mock(IslandWorldManager.class);
iwm = mock(IslandWorldManager.class);
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getIslandWorld(Mockito.any())).thenReturn(world);
when(iwm.getNetherWorld(Mockito.any())).thenReturn(world);
when(iwm.getEndWorld(Mockito.any())).thenReturn(world);
when(iwm.isAllowObsidianScooping(Mockito.any())).thenReturn(true); // Allow scooping
// Mock up IslandsManager
IslandsManager im = mock(IslandsManager.class);
im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im);
// Mock up items and blocks
ItemStack item = mock(ItemStack.class);
Block clickedBlock = mock(Block.class);
item = mock(ItemStack.class);
clickedBlock = mock(Block.class);
when(clickedBlock.getX()).thenReturn(0);
when(clickedBlock.getY()).thenReturn(0);
when(clickedBlock.getZ()).thenReturn(0);
when(clickedBlock.getWorld()).thenReturn(world);
when(item.getAmount()).thenReturn(1);
// Users
User.setPlugin(plugin);
@ -122,61 +132,90 @@ public class ObsidianScoopingListenerTest {
when(who.getGameMode()).thenReturn(GameMode.SURVIVAL);
// Locales
LocalesManager lm = mock(LocalesManager.class);
lm = mock(LocalesManager.class);
when(plugin.getLocalesManager()).thenReturn(lm);
when(lm.get(any(), any())).thenReturn("mock translation");
// World settings Flag
WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
Map<String, Boolean> map = new HashMap<>();
map.put("OBSIDIAN_SCOOPING", true);
when(ws.getWorldFlags()).thenReturn(map);
}
// Test all possible actions
//for (Action action: Action.values()) {
Action action = Action.RIGHT_CLICK_BLOCK;
@Test
public void testObsidianToLava() {
assertNotNull(new ObsidianScoopingListener());
}
@Test
public void testOnPlayerInteract() {
// Test incorrect items
Material inHand = Material.ACACIA_DOOR;
Material block = Material.BROWN_MUSHROOM;
when(item.getType()).thenReturn(inHand);
when(item.getAmount()).thenReturn(1);
when(clickedBlock.getType()).thenReturn(block);
inHand = Material.ACACIA_DOOR;
block = Material.BROWN_MUSHROOM;
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
// Test with bucket in hand
testEvent();
}
@Test
public void testOnPlayerInteractBucketInHand() {
// Test incorrect items
inHand = Material.BUCKET;
block = Material.BROWN_MUSHROOM;
when(item.getType()).thenReturn(inHand);
when(clickedBlock.getType()).thenReturn(block);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
testEvent();
}
@Test
public void testOnPlayerInteractObsidianAnvilInHand() {
// Test with obsidian in hand
inHand = Material.ANVIL;
block = Material.OBSIDIAN;
when(item.getType()).thenReturn(inHand);
when(clickedBlock.getType()).thenReturn(block);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
testEvent();
}
@Test
public void testOnPlayerInteractObsidianBucketInHand() {
// Positive test with 1 bucket in the stack
inHand = Material.BUCKET;
block = Material.OBSIDIAN;
when(item.getType()).thenReturn(inHand);
when(clickedBlock.getType()).thenReturn(block);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
testEvent();
}
@Test
public void testOnPlayerInteractObsidianManyBucketsInHand() {
// Positive test with 1 bucket in the stack
inHand = Material.BUCKET;
block = Material.OBSIDIAN;
// Positive test with 32 bucket in the stack
when(item.getAmount()).thenReturn(32);
// Create the event
testEvent(plugin, who, action, item, clickedBlock);
PlayerInteractEvent event = new PlayerInteractEvent(who, action, item, clickedBlock, BlockFace.EAST);
testEvent();
}
@Test
public void testOnPlayerInteractNotInWorld() {
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
// Test not in world
when(iwm.inWorld(any(World.class))).thenReturn(false);
when(iwm.inWorld(any(Location.class))).thenReturn(false);
assertFalse(listener.onPlayerInteract(event));
}
@Test
public void testOnPlayerInteractInWorld() {
// Put player in world
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
}
@Test
public void testOnPlayerInteractGameModes() {
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
// Test different game modes
for (GameMode gm : GameMode.values()) {
@ -185,23 +224,38 @@ public class ObsidianScoopingListenerTest {
assertFalse(listener.onPlayerInteract(event));
}
}
}
@Test
public void testOnPlayerInteractSurvivalNotOnIsland() {
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
// Set as survival
when(who.getGameMode()).thenReturn(GameMode.SURVIVAL);
// Positive test with 1 bucket in the stack
inHand = Material.BUCKET;
block = Material.OBSIDIAN;
when(item.getType()).thenReturn(inHand);
when(clickedBlock.getType()).thenReturn(block);
// Test when player is not on island
when(im.userIsOnIsland(Mockito.any(), Mockito.any())).thenReturn(false);
assertFalse(listener.onPlayerInteract(event));
}
private void testEvent(BentoBox plugin, Player who, Action action, ItemStack item, Block clickedBlock) {
private void testEvent() {
when(item.getType()).thenReturn(inHand);
when(clickedBlock.getType()).thenReturn(block);
Block obsidianBlock = mock(Block.class);
when(obsidianBlock.getType()).thenReturn(Material.OBSIDIAN);
Block airBlock = mock(Block.class);
when(airBlock.getType()).thenReturn(Material.AIR);
ObsidianScoopingListener listener = new ObsidianScoopingListener(plugin);
PlayerInteractEvent event = new PlayerInteractEvent(who, action, item, clickedBlock, BlockFace.EAST);
if (!action.equals(Action.RIGHT_CLICK_BLOCK) || !item.getType().equals(Material.BUCKET) || !clickedBlock.getType().equals(Material.OBSIDIAN)) {
ObsidianScoopingListener listener = new ObsidianScoopingListener();
PlayerInteractEvent event = new PlayerInteractEvent(who, Action.RIGHT_CLICK_BLOCK, item, clickedBlock, BlockFace.EAST);
if (!item.getType().equals(Material.BUCKET)
|| !clickedBlock.getType().equals(Material.OBSIDIAN)) {
assertFalse(listener.onPlayerInteract(event));
} else {
// Test with obby close by in any of the possible locations