Skip to content

Commit

Permalink
Merge pull request #267 from Multiverse/fix-266
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 authored Aug 25, 2024
2 parents 7a46c67 + 44da520 commit 83b8f38
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void loadConfig() {
this.setTeleportingEntities(this.isTeleportingEntities());
this.setSendingNoDestinationMessage(this.isSendingNoDestinationMessage());
this.setSendingDisabledPortalMessage(this.isSendingDisabledPortalMessage());
this.setEndPlatformDropBlocks(this.isEndPlatformDropBlocks());

this.setNetherPrefix(this.getNetherPrefix());
this.setNetherSuffix(this.getNetherSuffix());
Expand Down Expand Up @@ -334,6 +335,14 @@ public void setSendingNoDestinationMessage(boolean sendNoDestinationMessage) {
this.MVNPConfiguration.set("send_no_destination_message", sendNoDestinationMessage);
}

public boolean isEndPlatformDropBlocks() {
return this.MVNPConfiguration.getBoolean("end_platform_drop_blocks", true);
}

public void setEndPlatformDropBlocks(boolean endPlatformDropBlocks) {
this.MVNPConfiguration.set("end_platform_drop_blocks", endPlatformDropBlocks);
}

public boolean isHandledByNetherPortals(Location l) {
if (multiversePortals != null) {
// Catch errors which could occur if classes aren't present or are missing methods.
Expand Down Expand Up @@ -398,6 +407,7 @@ public String dumpVersionInfo(String buffer) {
buffer += logAndAddToPasteBinBuffer("Teleport Entities: " + this.isTeleportingEntities());
buffer += logAndAddToPasteBinBuffer("Send Disabled Portal Message: " + this.isSendingDisabledPortalMessage());
buffer += logAndAddToPasteBinBuffer("Send No Destination Message: " + this.isSendingNoDestinationMessage());
buffer += logAndAddToPasteBinBuffer("End platform drops blocks: " + this.isEndPlatformDropBlocks());
buffer += logAndAddToPasteBinBuffer("Special Code: FRN001");
return buffer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.onarandombox.MultiverseCore.event.MVPlayerTouchedPortalEvent;
import com.onarandombox.MultiverseCore.utils.PermissionTools;
import com.onarandombox.MultiverseNetherPortals.MultiverseNetherPortals;
import com.onarandombox.MultiverseNetherPortals.utils.EndPlatformCreator;
import com.onarandombox.MultiverseNetherPortals.utils.MVEventRecord;
import com.onarandombox.MultiverseNetherPortals.utils.MVLinkChecker;
import com.onarandombox.MultiverseNetherPortals.utils.MVNameChecker;
Expand All @@ -31,7 +32,6 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

public class MVNPEntityListener implements Listener {

Expand Down Expand Up @@ -299,14 +299,30 @@ public void onEntityPortal(EntityPortalEvent event) {
return;
}

Entity e = event.getEntity();
Location originalTo = this.locationManipulation.getBlockLocation(event.getTo());
Location currentLocation = this.locationManipulation.getBlockLocation(event.getFrom());
// Some shortcuts for later
Entity entity = event.getEntity();

@Nullable Location toLocation = event.getTo();
Location fromLocation = event.getFrom();

if (toLocation == null) {
Logging.warning("ToLocation in EntityPortalEvent is null.");
return;
}

MultiverseWorld fromWorld = this.worldManager.getMVWorld(fromLocation.getWorld().getName());
MultiverseWorld toWorld = this.worldManager.getMVWorld(toLocation.getWorld().getName());

Location originalTo = this.locationManipulation.getBlockLocation(toLocation);
Location currentLocation = this.locationManipulation.getBlockLocation(fromLocation);


// Don't mess with other people's stuff
if (!plugin.isHandledByNetherPortals(currentLocation)) {
return;
}

// This is the entity event, don't teleport entities if we're not supposed to
if (!this.plugin.isTeleportingEntities()) {
event.setCancelled(true);
return;
Expand All @@ -323,18 +339,19 @@ public void onEntityPortal(EntityPortalEvent event) {
return;
}

// Are we allowed to use the nether portal travel agent?
if (type == PortalType.NETHER) {
try {
Class.forName("org.bukkit.TravelAgent");
event.useTravelAgent(true);
} catch (ClassNotFoundException ignore) {
Logging.fine("TravelAgent not available for EntityPortalEvent for " + e.getName());
Logging.fine("TravelAgent not available for EntityPortalEvent for " + entity.getName());
}
}

String currentWorld = currentLocation.getWorld().getName();
String linkedWorld = this.plugin.getWorldLink(currentWorld, type);
Location newTo = getLocation(e, currentLocation, type, currentWorld, linkedWorld);
Location newTo = getLocation(entity, currentLocation, type, currentWorld, linkedWorld); // Gets the player spawn location from the portal spawn location

if (newTo != null) {
event.setTo(newTo);
Expand All @@ -343,57 +360,49 @@ public void onEntityPortal(EntityPortalEvent event) {
return;
}

MultiverseWorld fromWorld = this.worldManager.getMVWorld(event.getFrom().getWorld().getName());
MultiverseWorld toWorld = this.worldManager.getMVWorld(event.getTo().getWorld().getName());

if (!event.isCancelled()) {
if (fromWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
Logging.fine("Entity '" + e.getName() + "' will be teleported to the spawn of '" + toWorld.getName() + "' since they used an end exit portal.");
try {
Class.forName("org.bukkit.TravelAgent");
event.getPortalTravelAgent().setCanCreatePortal(false);
} catch (ClassNotFoundException ignore) {
Logging.fine("TravelAgent not available for EntityPortalEvent for " + e.getName() + ". There may be a portal created at spawn.");
}
// If we are going to the overworld from the end
if (fromWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
Logging.fine("Entity '" + entity.getName() + "' will be teleported to the spawn of '" + toWorld.getName() + "' since they used an end exit portal.");
try {
Class.forName("org.bukkit.TravelAgent");
event.getPortalTravelAgent().setCanCreatePortal(false);
} catch (ClassNotFoundException ignore) {
Logging.fine("TravelAgent not available for EntityPortalEvent for " + entity.getName() + ". There may be a portal created at spawn.");
}

event.setTo(toWorld.getSpawnLocation());
} else if (fromWorld.getEnvironment() == World.Environment.NETHER && type == PortalType.NETHER) {
try {
Class.forName("org.bukkit.TravelAgent");
event.getPortalTravelAgent().setCanCreatePortal(true);
event.setTo(event.getPortalTravelAgent().findOrCreate(event.getTo()));
} catch (ClassNotFoundException ignore) {
Logging.fine("TravelAgent not available for EntityPortalEvent for " + e.getName() + ". Their destination may not be correct.");
event.setTo(event.getTo());
}
} else if (toWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
Location loc = new Location(event.getTo().getWorld(), 100, 50, 0); // This is the vanilla location for obsidian platform.
event.setTo(loc);
Block block = loc.getBlock();
for (int x = block.getX() - 2; x <= block.getX() + 2; x++) {
for (int z = block.getZ() - 2; z <= block.getZ() + 2; z++) {
Block platformBlock = loc.getWorld().getBlockAt(x, block.getY() - 1, z);
if (platformBlock.getType() != Material.OBSIDIAN) {
platformBlock.setType(Material.OBSIDIAN);
}
for (int yMod = 1; yMod <= 3; yMod++) {
Block b = platformBlock.getRelative(BlockFace.UP, yMod);
if (b.getType() != Material.AIR) {
b.setType(Material.AIR);
}
}
}
}
event.setTo(toWorld.getSpawnLocation());
return;
}

// If we are going to the overworld from the nether
if (fromWorld.getEnvironment() == World.Environment.NETHER && type == PortalType.NETHER) {
try {
Class.forName("org.bukkit.TravelAgent");
event.getPortalTravelAgent().setCanCreatePortal(true);
event.setTo(event.getPortalTravelAgent().findOrCreate(toLocation));
} catch (ClassNotFoundException ignore) {
Logging.fine("TravelAgent not available for EntityPortalEvent for " + entity.getName() + ". Their destination may not be correct.");
event.setTo(toLocation);
}

return;
}

// If we are going to the end from anywhere
if (toWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
Location spawnLocation = EndPlatformCreator.getVanillaLocation(toWorld);
event.setTo(spawnLocation);
EndPlatformCreator.createEndPlatform(spawnLocation, plugin.isEndPlatformDropBlocks());
return;
}
}

@EventHandler
public void onEntityPortalExit(EntityPortalExitEvent event) {
if (event.getEntity() instanceof Player) {
Player p = (Player) event.getEntity();
eventRecord.removeFromRecord(PortalType.ENDER, p.getUniqueId());
eventRecord.removeFromRecord(PortalType.NETHER, p.getUniqueId());
Player player = (Player) event.getEntity();
eventRecord.removeFromRecord(PortalType.ENDER, player.getUniqueId());
eventRecord.removeFromRecord(PortalType.NETHER, player.getUniqueId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseCore.utils.PermissionTools;
import com.onarandombox.MultiverseNetherPortals.MultiverseNetherPortals;
import com.onarandombox.MultiverseNetherPortals.utils.EndPlatformCreator;
import com.onarandombox.MultiverseNetherPortals.utils.MVLinkChecker;
import com.onarandombox.MultiverseNetherPortals.utils.MVNameChecker;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.PortalType;
import org.bukkit.World;
import org.bukkit.advancement.Advancement;
import org.bukkit.advancement.AdvancementProgress;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -138,23 +136,9 @@ public void onPlayerPortal(PlayerPortalEvent event) {
event.setTo(event.getTo());
}
} else if (toWorld.getEnvironment() == World.Environment.THE_END && type == PortalType.ENDER) {
Location loc = new Location(event.getTo().getWorld(), 100, 50, 0); // This is the vanilla location for obsidian platform.
event.setTo(loc);
Block block = loc.getBlock();
for (int x = block.getX() - 2; x <= block.getX() + 2; x++) {
for (int z = block.getZ() - 2; z <= block.getZ() + 2; z++) {
Block platformBlock = loc.getWorld().getBlockAt(x, block.getY() - 1, z);
if (platformBlock.getType() != Material.OBSIDIAN) {
platformBlock.setType(Material.OBSIDIAN);
}
for (int yMod = 1; yMod <= 3; yMod++) {
Block b = platformBlock.getRelative(BlockFace.UP, yMod);
if (b.getType() != Material.AIR) {
b.setType(Material.AIR);
}
}
}
}
Location spawnLocation = EndPlatformCreator.getVanillaLocation(event.getTo().getWorld());
event.setTo(spawnLocation);
EndPlatformCreator.createEndPlatform(spawnLocation, plugin.isEndPlatformDropBlocks());
}

// Advancements need to be triggered manually
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.onarandombox.MultiverseNetherPortals.utils;

import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.api.MultiverseWorld;
import com.onarandombox.MultiverseNetherPortals.MultiverseNetherPortals;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;

public class EndPlatformCreator {

/**
* Creates an end platform at the specified {@code Block}
*
* @param spawnLocation The {@code Block} that the player will spawn at.
* @param dropEndBlocks If the platform should drop the broken blocks or delete them
*/
public static void createEndPlatform(Block spawnLocation, boolean dropEndBlocks) {
Logging.fine("Creating an end platform at " + spawnLocation.toString());

for (int x = spawnLocation.getX() - 2; x <= spawnLocation.getX() + 2; x++) {
for (int z = spawnLocation.getZ() - 2; z <= spawnLocation.getZ() + 2; z++) {
Block platformBlock = spawnLocation.getWorld().getBlockAt(x, spawnLocation.getY() - 1, z);
Logging.finest("Placing blocks at " + platformBlock);

// Create platform
if (platformBlock.getType() != Material.OBSIDIAN) {
platformBlock.setType(Material.OBSIDIAN);
Logging.finest("Placing obsidian at " + platformBlock);
}

// Clear space above platform
for (int yMod = 1; yMod <= 3; yMod++) {
Block block = platformBlock.getRelative(BlockFace.UP, yMod);
if (block.getType() != Material.AIR) {
if (dropEndBlocks) {
block.breakNaturally();
} else {
block.setType(Material.AIR);
}

Logging.finest("Breaking block at " + platformBlock);
}
}
}
}
}


/**
* Creates an end platform at the specified {@code Location}
* @param spawnLocation The {@code Location} that the player will spawn at.
*/
public static void createEndPlatform(Location spawnLocation, boolean dropEndBlocks) {
createEndPlatform(spawnLocation.getBlock(), dropEndBlocks);
}

/**
* The default vanilla location for the end platform
*/
public static Location getVanillaLocation(World world) {
return new Location(world, 100, 49, 0, 90, 0);
}

/**
* The default vanilla location for the end platform
*/
public static Location getVanillaLocation(MultiverseWorld world) {
return getVanillaLocation(world.getCBWorld());
}
}

0 comments on commit 83b8f38

Please sign in to comment.