Moved the Nether trees conversion to its own listener

This commit is contained in:
Florian CUNY 2019-02-14 11:08:54 +01:00
parent 1a3049b3b9
commit 1eac578ba1
4 changed files with 52 additions and 81 deletions

View File

@ -25,6 +25,7 @@ import world.bentobox.bentobox.listeners.DeathListener;
import world.bentobox.bentobox.listeners.JoinLeaveListener;
import world.bentobox.bentobox.listeners.NetherPortals;
import world.bentobox.bentobox.listeners.PanelListenerManager;
import world.bentobox.bentobox.listeners.flags.NetherTreesListener;
import world.bentobox.bentobox.managers.AddonsManager;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.FlagsManager;
@ -195,7 +196,7 @@ public class BentoBox extends JavaPlugin {
}
/**
* Register listeners
* Registers listeners.
*/
private void registerListeners() {
PluginManager manager = getServer().getPluginManager();
@ -205,6 +206,8 @@ public class BentoBox extends JavaPlugin {
manager.registerEvents(new PanelListenerManager(), this);
// Nether portals
manager.registerEvents(new NetherPortals(this), this);
// Nether trees conversion
manager.registerEvents(new NetherTreesListener(this), this);
// End dragon blocking
manager.registerEvents(new BlockEndDragon(this), this);
// Banned visitor commands

View File

@ -257,24 +257,4 @@ public class NetherPortals implements Listener {
e.setCancelled(true);
}
}
/**
* Converts trees to gravel and glowstone
*
* @param e - event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onTreeGrow(StructureGrowEvent e) {
if (!plugin.getIWM().isNetherTrees(e.getWorld()) || !e.getWorld().getEnvironment().equals(Environment.NETHER)) {
return false;
}
for (BlockState b : e.getBlocks()) {
if (Tag.LOGS.isTagged(b.getType())) {
b.setType(Material.GRAVEL);
} else if (Tag.LEAVES.isTagged(b.getType())) {
b.setType(Material.GLOWSTONE);
}
}
return true;
}
}

View File

@ -0,0 +1,48 @@
package world.bentobox.bentobox.listeners.flags;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.BlockState;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.world.StructureGrowEvent;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.configuration.WorldSettings;
/**
* Handles conversion of trees in the Nether if {@link WorldSettings#isNetherTrees()} is {@code true}.
*
* @author tastybento
*/
public class NetherTreesListener implements Listener {
private BentoBox plugin;
public NetherTreesListener(BentoBox plugin) {
this.plugin = plugin;
}
/**
* Converts trees to gravel and glowstone.
*
* @param e event
*/
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onTreeGrow(StructureGrowEvent e) {
// Don't do anything if we're not in the right place.
if (!plugin.getIWM().isNetherTrees(e.getWorld()) || !e.getWorld().getEnvironment().equals(World.Environment.NETHER)) {
return;
}
// Modify everything!
for (BlockState b : e.getBlocks()) {
if (Tag.LOGS.isTagged(b.getType())) {
b.setType(Material.GRAVEL);
} else if (Tag.LEAVES.isTagged(b.getType())) {
b.setType(Material.GLOWSTONE);
}
}
}
}

View File

@ -1,6 +1,3 @@
/**
*
*/
package world.bentobox.bentobox.listeners;
import static org.junit.Assert.assertFalse;
@ -70,9 +67,6 @@ public class NetherPortalsTest {
private World nether;
private World end;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
@ -159,14 +153,6 @@ public class NetherPortalsTest {
when(iwm.inWorld(any(Location.class))).thenReturn(false);
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.NetherPortals#NetherPortals(world.bentobox.bentobox.BentoBox)}.
*/
@Test
public void testNetherPortals() {
assertNotNull(new NetherPortals(plugin));
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.NetherPortals#onBlockBreak(org.bukkit.event.block.BlockBreakEvent)}.
*/
@ -739,50 +725,4 @@ public class NetherPortalsTest {
Mockito.verify(block).getLocation();
assertTrue(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.NetherPortals#onTreeGrow(org.bukkit.event.world.StructureGrowEvent)}.
*/
@Test
public void testOnTreeGrow() {
NetherPortals np = new NetherPortals(plugin);
Location loc = mock(Location.class);
// Wrong world to start
when(loc.getWorld()).thenReturn(world);
BlockState log = mock(BlockState.class);
when(log.getType()).thenReturn(Material.OAK_LOG);
BlockState log2 = mock(BlockState.class);
when(log2.getType()).thenReturn(Material.ACACIA_LOG);
BlockState leaves = mock(BlockState.class);
when(leaves.getType()).thenReturn(Material.OAK_LEAVES);
BlockState leaves2 = mock(BlockState.class);
when(leaves2.getType()).thenReturn(Material.OAK_LEAVES);
List<BlockState> blocks = new ArrayList<>();
blocks.add(log);
blocks.add(log2);
blocks.add(leaves);
blocks.add(leaves2);
StructureGrowEvent e = new StructureGrowEvent(loc, TreeType.ACACIA, false, null, blocks);
// No nether trees
when(iwm.isNetherTrees(world)).thenReturn(false);
assertFalse(np.onTreeGrow(e));
// nether trees, wrong world
e = new StructureGrowEvent(loc, TreeType.ACACIA, false, null, blocks);
when(iwm.isNetherTrees(world)).thenReturn(true);
assertFalse(np.onTreeGrow(e));
// Make the world nether
when(iwm.isNetherTrees(nether)).thenReturn(true);
when(loc.getWorld()).thenReturn(nether);
e = new StructureGrowEvent(loc, TreeType.ACACIA, false, null, blocks);
/*
* Temporary
* TODO: Fix for 1.13
assertTrue(np.onTreeGrow(e));
Mockito.verify(log).setType(Material.GRAVEL);
Mockito.verify(log2).setType(Material.GRAVEL);
Mockito.verify(leaves).setType(Material.GLOWSTONE);
Mockito.verify(leaves2).setType(Material.GLOWSTONE);
*/
}
}