Skip to content

Commit

Permalink
Added test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Nov 23, 2024
1 parent 62c0d71 commit 02d69b8
Show file tree
Hide file tree
Showing 4 changed files with 595 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,25 @@ protected boolean isPastingMissingIslands(World overWorld)
* Set of entities that currently is in teleportation.
*/
protected final Set<UUID> inTeleport;

/**
* @return the inTeleport
*/
public Set<UUID> getInTeleport() {
return inTeleport;
}

/**
* @return the inPortal
*/
public Set<UUID> getInPortal() {
return inPortal;
}

/**
* @return the teleportOrigin
*/
public Map<UUID, World> getTeleportOrigin() {
return teleportOrigin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,6 @@ public PlayerTeleportListener(@NonNull BentoBox plugin)
// Section: Listeners
// ---------------------------------------------------------------------


/**
* This listener checks player portal events and triggers appropriate methods to transfer
* players to the correct location in other dimension.
* <p>
* This event is triggered when player is about to being teleported because of contact with the
* nether portal or end gateway portal (exit portal triggers respawn).
* <p>
* This event is not called if nether/end is disabled in server settings.
*
* @param event the player portal event.
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerPortalEvent(PlayerPortalEvent event)
{
switch (event.getCause())
{
case NETHER_PORTAL -> this.portalProcess(event, World.Environment.NETHER);
case END_PORTAL, END_GATEWAY -> this.portalProcess(event, World.Environment.THE_END);
default -> { // Do nothing, ignore

}
/*
* Other potential reasons: CHORUS_FRUIT , COMMAND, DISMOUNT,
* ENDER_PEARL, EXIT_BED, PLUGIN, SPECTATE , UNKNOWN
*/
}
}


/**
* Fires the event if nether or end is disabled at the system level
*
Expand All @@ -102,7 +72,6 @@ public void onPlayerPortal(EntityPortalEnterEvent event)
// This handles only players.
return;
}

Entity entity = event.getEntity();
Material type = event.getLocation().getBlock().getType();
UUID uuid = entity.getUniqueId();
Expand All @@ -112,7 +81,6 @@ public void onPlayerPortal(EntityPortalEnterEvent event)
{
return;
}

this.inPortal.add(uuid);
// Add original world for respawning.
this.teleportOrigin.put(uuid, event.getLocation().getWorld());
Expand All @@ -139,7 +107,6 @@ public void onPlayerPortal(EntityPortalEnterEvent event)
}, 40);
return;
}

// End portals are instant transfer
if (!Bukkit.getAllowEnd() && (type.equals(Material.END_PORTAL) || type.equals(Material.END_GATEWAY)))
{
Expand Down Expand Up @@ -232,6 +199,31 @@ public void onPlayerExitPortal(PlayerRespawnEvent event)
});
}

/**
* This listener checks player portal events and triggers appropriate methods to transfer
* players to the correct location in other dimension.
* <p>
* This event is triggered when player is about to being teleported because of contact with the
* nether portal or end gateway portal (exit portal triggers respawn).
* <p>
* This event is not called if nether/end is disabled in server settings.
*
* @param event the player portal event.
*/
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerPortalEvent(PlayerPortalEvent event) {
switch (event.getCause()) {
case NETHER_PORTAL -> this.portalProcess(event, World.Environment.NETHER);
case END_PORTAL, END_GATEWAY -> this.portalProcess(event, World.Environment.THE_END);
default -> { // Do nothing, ignore

}
/*
* Other potential reasons: CHORUS_FRUIT , COMMAND, DISMOUNT,
* ENDER_PEARL, EXIT_BED, PLUGIN, SPECTATE , UNKNOWN
*/
}
}


// ---------------------------------------------------------------------
Expand All @@ -254,26 +246,22 @@ private void portalProcess(PlayerPortalEvent event, World.Environment environmen
// Not teleporting from/to bentobox worlds.
return;
}

if (!this.isAllowedInConfig(overWorld, environment))
{
// World is disabled in config. Do not teleport player.
event.setCancelled(true);
return;
}

if (!this.isAllowedOnServer(environment))
{
// World is disabled in bukkit. Event is not triggered, but cancel by chance.
event.setCancelled(true);
}

if (this.inTeleport.contains(event.getPlayer().getUniqueId()))
{
// Player is already in teleportation.
return;
}

this.inTeleport.add(event.getPlayer().getUniqueId());

if (fromWorld.equals(overWorld) && !this.isIslandWorld(overWorld, environment))
Expand All @@ -282,15 +270,13 @@ private void portalProcess(PlayerPortalEvent event, World.Environment environmen
this.handleToStandardNetherOrEnd(event, overWorld, environment);
return;
}

if (!fromWorld.equals(overWorld) && !this.isIslandWorld(overWorld, environment))
{
// If entering a portal in the other world, teleport to a portal in overworld if
// there is one
this.handleFromStandardNetherOrEnd(event, overWorld, environment);
return;
}

// To the nether/end or overworld.
World toWorld = !fromWorld.getEnvironment().equals(environment) ?
this.getNetherEndWorld(overWorld, environment) : overWorld;
Expand Down Expand Up @@ -326,13 +312,11 @@ private void portalProcess(PlayerPortalEvent event, World.Environment environmen
// If there is no island, then processor already created island. Nothing to do more.
return;
}

if (!event.isCancelled() && event.getCanCreatePortal())
{
// Let the server teleport
return;
}

if (World.Environment.THE_END.equals(environment))
{
// Prevent death from hitting the ground while calculating location.
Expand Down Expand Up @@ -372,7 +356,7 @@ private void portalProcess(PlayerPortalEvent event, World.Environment environmen
* @param overWorld - over world
* @param environment - environment involved
*/
private void handleToStandardNetherOrEnd(PlayerPortalEvent event,
void handleToStandardNetherOrEnd(PlayerPortalEvent event,
World overWorld,
World.Environment environment)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.entity.Player.Spigot;
import org.bukkit.event.entity.EntityExplodeEvent;
Expand Down Expand Up @@ -143,6 +144,7 @@ public void setUp() throws Exception {
when(mockPlayer.getName()).thenReturn("tastybento");
when(mockPlayer.getInventory()).thenReturn(inv);
when(mockPlayer.spigot()).thenReturn(spigot);
when(mockPlayer.getType()).thenReturn(EntityType.PLAYER);

User.setPlugin(plugin);
User.clearUsers();
Expand Down
Loading

0 comments on commit 02d69b8

Please sign in to comment.