Allow manual interaction with iron doors in DuneEss
This commit is contained in:
parent
aa9ed1ce9f
commit
0e790bd154
3 changed files with 130 additions and 0 deletions
|
|
@ -90,6 +90,7 @@ public final class DuneEss extends JavaPlugin implements CommandExecutor, TabCom
|
|||
command.setExecutor(this);
|
||||
command.setTabCompleter(this);
|
||||
getServer().getPluginManager().registerEvents(this, this);
|
||||
getServer().getPluginManager().registerEvents(new IronDoorInteraction(), this);
|
||||
basics=new EssentialsBasics(this);
|
||||
basics.register();
|
||||
StaffItemTools.start(this);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,45 @@
|
|||
package com.example.duneess;
|
||||
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.SoundCategory;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.data.Bisected;
|
||||
import org.bukkit.block.data.type.Door;
|
||||
import org.bukkit.event.Event;
|
||||
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.EquipmentSlot;
|
||||
|
||||
/** Give iron doors a manual interaction while retaining their redstone behavior. */
|
||||
final class IronDoorInteraction implements Listener {
|
||||
@EventHandler(priority = EventPriority.HIGHEST)
|
||||
public void interact(PlayerInteractEvent event) {
|
||||
if (event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getHand() != EquipmentSlot.HAND
|
||||
|| event.useInteractedBlock() == Event.Result.DENY) return;
|
||||
var clicked = event.getClickedBlock();
|
||||
if (clicked == null || clicked.getType() != Material.IRON_DOOR
|
||||
|| !(clicked.getBlockData() instanceof Door door)) return;
|
||||
var player = event.getPlayer();
|
||||
if (player.getGameMode() == GameMode.SPECTATOR) return;
|
||||
if (player.isSneaking() && (!player.getInventory().getItemInMainHand().isEmpty()
|
||||
|| !player.getInventory().getItemInOffHand().isEmpty())) return;
|
||||
|
||||
var other = clicked.getRelative(door.getHalf() == Bisected.Half.TOP ? BlockFace.DOWN : BlockFace.UP);
|
||||
if (other.getType() != Material.IRON_DOOR || !(other.getBlockData() instanceof Door otherDoor)
|
||||
|| otherDoor.getHalf() == door.getHalf()) return;
|
||||
boolean open = !door.isOpen();
|
||||
door.setOpen(open);
|
||||
otherDoor.setOpen(open);
|
||||
clicked.setBlockData(door, false);
|
||||
other.setBlockData(otherDoor, false);
|
||||
clicked.getWorld().playSound(clicked.getLocation().add(.5, .5, .5),
|
||||
open ? "minecraft:block.iron_door.open" : "minecraft:block.iron_door.close",
|
||||
SoundCategory.BLOCKS, 1, 1);
|
||||
event.setUseInteractedBlock(Event.Result.DENY);
|
||||
event.setUseItemInHand(Event.Result.DENY);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,84 @@
|
|||
package com.example.duneess;
|
||||
|
||||
import org.bukkit.GameMode;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.SoundCategory;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockFace;
|
||||
import org.bukkit.block.data.Bisected;
|
||||
import org.bukkit.block.data.type.Door;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Event;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.inventory.EquipmentSlot;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class IronDoorInteractionTest {
|
||||
private final IronDoorInteraction listener = new IronDoorInteraction();
|
||||
private final PlayerInteractEvent event = mock(PlayerInteractEvent.class);
|
||||
private final Player player = mock(Player.class);
|
||||
private final Block upper = mock(Block.class);
|
||||
private final Block lower = mock(Block.class);
|
||||
private final Door top = mock(Door.class);
|
||||
private final Door bottom = mock(Door.class);
|
||||
private final World world = mock(World.class);
|
||||
|
||||
@BeforeEach void door() {
|
||||
when(event.getAction()).thenReturn(Action.RIGHT_CLICK_BLOCK);
|
||||
when(event.getHand()).thenReturn(EquipmentSlot.HAND);
|
||||
when(event.useInteractedBlock()).thenReturn(Event.Result.DEFAULT);
|
||||
when(event.getPlayer()).thenReturn(player);
|
||||
when(player.getGameMode()).thenReturn(GameMode.SURVIVAL);
|
||||
when(upper.getType()).thenReturn(Material.IRON_DOOR);
|
||||
when(lower.getType()).thenReturn(Material.IRON_DOOR);
|
||||
when(upper.getBlockData()).thenReturn(top);
|
||||
when(lower.getBlockData()).thenReturn(bottom);
|
||||
when(top.getHalf()).thenReturn(Bisected.Half.TOP);
|
||||
when(bottom.getHalf()).thenReturn(Bisected.Half.BOTTOM);
|
||||
when(upper.getRelative(BlockFace.DOWN)).thenReturn(lower);
|
||||
when(lower.getRelative(BlockFace.UP)).thenReturn(upper);
|
||||
when(upper.getWorld()).thenReturn(world);
|
||||
when(lower.getWorld()).thenReturn(world);
|
||||
when(upper.getLocation()).thenReturn(new Location(world, 1, 2, 1));
|
||||
when(lower.getLocation()).thenReturn(new Location(world, 1, 1, 1));
|
||||
}
|
||||
|
||||
@Test void topClickOpensBothHalvesWithoutChangingPower() {
|
||||
when(event.getClickedBlock()).thenReturn(upper);
|
||||
listener.interact(event);
|
||||
verify(top).setOpen(true);
|
||||
verify(bottom).setOpen(true);
|
||||
verify(top, never()).setPowered(anyBoolean());
|
||||
verify(bottom, never()).setPowered(anyBoolean());
|
||||
verify(upper).setBlockData(top, false);
|
||||
verify(lower).setBlockData(bottom, false);
|
||||
verify(world).playSound(any(Location.class), eq("minecraft:block.iron_door.open"), eq(SoundCategory.BLOCKS), eq(1f), eq(1f));
|
||||
verify(event).setUseItemInHand(Event.Result.DENY);
|
||||
}
|
||||
|
||||
@Test void bottomClickClosesBothHalves() {
|
||||
when(event.getClickedBlock()).thenReturn(lower);
|
||||
when(bottom.isOpen()).thenReturn(true);
|
||||
listener.interact(event);
|
||||
verify(top).setOpen(false);
|
||||
verify(bottom).setOpen(false);
|
||||
verify(world).playSound(any(Location.class), eq("minecraft:block.iron_door.close"), eq(SoundCategory.BLOCKS), eq(1f), eq(1f));
|
||||
}
|
||||
|
||||
@Test void protectedAndOffHandClicksNeverToggle() {
|
||||
when(event.getClickedBlock()).thenReturn(upper);
|
||||
when(event.useInteractedBlock()).thenReturn(Event.Result.DENY);
|
||||
listener.interact(event);
|
||||
when(event.useInteractedBlock()).thenReturn(Event.Result.DEFAULT);
|
||||
when(event.getHand()).thenReturn(EquipmentSlot.OFF_HAND);
|
||||
listener.interact(event);
|
||||
verifyNoInteractions(top, bottom, world);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue

Staff