feat: portals now trigger and nether portals are cancelled

This commit is contained in:
Sekwah 2024-02-04 04:53:29 +00:00
parent 7b3004ea35
commit 7dde87dfcc
17 changed files with 107 additions and 56 deletions

View File

@ -164,13 +164,16 @@ public class CoreListeners {
return portalServices.inPortalRegion(entity.getBlockLoc(), 2); return portalServices.inPortalRegion(entity.getBlockLoc(), 2);
} }
public boolean portalEvent(PlayerContainer player) {
return !portalServices.inPortalRegion(player.getBlockLoc(), 1)
&& (!(player.getHeight() > 1) || !portalServices.inPortalRegion(player.getBlockLoc().addY((int) player.getHeight()), 1));
}
public boolean entityPortalEvent(EntityContainer entity) { public boolean entityPortalEvent(EntityContainer entity) {
return !portalServices.inPortalRegion(entity.getBlockLoc(), 1) var pos = entity.getBlockLoc();
&& (!(entity.getHeight() > 1) || !portalServices.inPortalRegion(entity.getBlockLoc().addY((int) entity.getHeight()), 1)); if(entity instanceof PlayerContainer player) {
var playerData = playerDataServices.getPlayerData(player);
if(playerData.isPortalCooldown()) {
return false;
}
}
var feetInPortal = portalServices.inPortalRegion(pos, 1);
var headInPortal = portalServices.inPortalRegion(pos.addY((int) entity.getHeight()), 1);
return !(feetInPortal || headInPortal);
} }
} }

View File

@ -16,6 +16,7 @@ import com.sekwah.advancedportals.core.util.Lang;
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this. * This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this.
@ -47,7 +48,7 @@ public class ShowDestiSubCommand implements SubCommand, SubCommand.SubCommandOnI
return; return;
} }
var tempData = tempDataServices.getPlayerTempData(sender.getPlayerContainer()); var tempData = tempDataServices.getPlayerData(sender.getPlayerContainer());
if(tempData.isDestiVisible()) { if(tempData.isDestiVisible()) {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.destination.show.disabled")); sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.destination.show.disabled"));
} else { } else {
@ -80,14 +81,14 @@ public class ShowDestiSubCommand implements SubCommand, SubCommand.SubCommandOnI
public void registered() { public void registered() {
gameScheduler.intervalTickEvent("show_portal", () -> { gameScheduler.intervalTickEvent("show_portal", () -> {
for(PlayerContainer player : serverContainer.getPlayers()) { for(PlayerContainer player : serverContainer.getPlayers()) {
var tempData = tempDataServices.getPlayerTempData(player); var tempData = tempDataServices.getPlayerData(player);
if(!tempData.isDestiVisible()) { if(!tempData.isDestiVisible()) {
continue; continue;
} }
for (Destination destination : destinationServices.getDestinations()) { for (Destination destination : destinationServices.getDestinations()) {
var pos = destination.getLoc(); var pos = destination.getLoc();
if(pos.distanceTo(player.getLoc()) < config.getVisibleRange()) { if(Objects.equals(pos.getWorldName(), player.getWorldName()) && pos.distanceTo(player.getLoc()) < config.getVisibleRange()) {
Debug.addMarker(player, pos.toBlockPos(), destination.getArgValues("name")[0], new Color(100, 100, 100, 100), 1300); Debug.addMarker(player, pos.toBlockPos(), destination.getArgValues("name")[0], new Color(100, 100, 100, 100), 1300);
} }
} }

View File

@ -18,6 +18,7 @@ import com.sekwah.advancedportals.core.util.Lang;
import java.awt.*; import java.awt.*;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this. * This will be different from the old show command and I believe it is 1.16+ till the latest version as of writing this.
@ -29,7 +30,7 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn
boolean alternate_show_trigger = true; boolean alternate_show_trigger = true;
@Inject @Inject
PlayerDataServices tempDataServices; PlayerDataServices playerDataServices;
@Inject @Inject
GameScheduler gameScheduler; GameScheduler gameScheduler;
@ -64,7 +65,7 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn
return; return;
} }
var tempData = tempDataServices.getPlayerTempData(sender.getPlayerContainer()); var tempData = playerDataServices.getPlayerData(sender.getPlayerContainer());
if(tempData.isPortalVisible()) { if(tempData.isPortalVisible()) {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.portal.show.disabled")); sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.portal.show.disabled"));
} else { } else {
@ -98,26 +99,26 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn
gameScheduler.intervalTickEvent("show_portal", () -> { gameScheduler.intervalTickEvent("show_portal", () -> {
alternate_show_trigger = !alternate_show_trigger; alternate_show_trigger = !alternate_show_trigger;
for(PlayerContainer player : serverContainer.getPlayers()) { for(PlayerContainer player : serverContainer.getPlayers()) {
var tempData = tempDataServices.getPlayerTempData(player); var tempData = playerDataServices.getPlayerData(player);
if(!tempData.isPortalVisible()) { if(!tempData.isPortalVisible()) {
continue; continue;
} }
if (tempData.getPos1() != null && tempData.getPos2() != null) { if (tempData.getPos1() != null && tempData.getPos2() != null && tempData.getPos1().worldName.equals(player.getWorldName()) && tempData.getPos2().worldName.equals(player.getWorldName())) {
debugVisuals(player, tempData.getPos1(), tempData.getPos2(), SELECTION_COLOR, SHOW_TICKS); debugVisuals(player, tempData.getPos1(), tempData.getPos2(), SELECTION_COLOR, SHOW_TICKS);
} }
if(tempData.getPos1() != null) { if(tempData.getPos1() != null && tempData.getPos1().worldName.equals(player.getWorldName())) {
Debug.addMarker(player, tempData.getPos1(), "Pos1", POS1_COLOR, SHOW_TICKS); Debug.addMarker(player, tempData.getPos1(), "Pos1", POS1_COLOR, SHOW_TICKS);
} }
if(tempData.getPos2() != null) { if(tempData.getPos2() != null && tempData.getPos2().worldName.equals(player.getWorldName())) {
Debug.addMarker(player, tempData.getPos2(), "Pos2", POS2_COLOR, SHOW_TICKS); Debug.addMarker(player, tempData.getPos2(), "Pos2", POS2_COLOR, SHOW_TICKS);
} }
var world = player.getWorld(); var world = player.getWorld();
for (var portal : portalServices.getPortals()) { for (var portal : portalServices.getPortals()) {
if(portal.isLocationInPortal(player.getLoc(), config.getVisibleRange())) { if(Objects.equals(portal.getMinLoc().worldName, player.getWorldName()) && portal.isLocationInPortal(player.getLoc(), config.getVisibleRange())) {
BlockLocation minLoc = portal.getMinLoc(); BlockLocation minLoc = portal.getMinLoc();
BlockLocation maxLoc = portal.getMaxLoc(); BlockLocation maxLoc = portal.getMaxLoc();
int midX = (minLoc.posX + maxLoc.posX) / 2; int midX = (minLoc.posX + maxLoc.posX) / 2;

View File

@ -16,4 +16,6 @@ public interface EntityContainer {
WorldContainer getWorld(); WorldContainer getWorld();
String getName(); String getName();
String getWorldName();
} }

View File

@ -13,13 +13,13 @@ public interface WarpEffect {
interface Sound extends WarpEffect { interface Sound extends WarpEffect {
void onWarpSound(PlayerContainer player, Action action, AdvancedPortal portal); void onWarpSound(PlayerContainer player, Action action);
} }
interface Visual extends WarpEffect { interface Visual extends WarpEffect {
void onWarpVisual(PlayerContainer player, Action action, AdvancedPortal portal); void onWarpVisual(PlayerContainer player, Action action);
} }
} }

View File

@ -8,6 +8,7 @@ import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.serializeddata.DataTag; import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.registry.TagRegistry; import com.sekwah.advancedportals.core.registry.TagRegistry;
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.core.services.PlayerDataServices;
import com.sekwah.advancedportals.core.tags.activation.TriggerBlockTag; import com.sekwah.advancedportals.core.tags.activation.TriggerBlockTag;
import com.sekwah.advancedportals.core.warphandler.ActivationData; import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag; import com.sekwah.advancedportals.core.warphandler.Tag;
@ -33,6 +34,9 @@ public class AdvancedPortal implements TagTarget {
@SerializedName("a") @SerializedName("a")
private HashMap<String, String[]> args = new HashMap<>(); private HashMap<String, String[]> args = new HashMap<>();
@Inject
transient PlayerDataServices playerDataServices;
public AdvancedPortal(BlockLocation minLoc, BlockLocation maxLoc) { public AdvancedPortal(BlockLocation minLoc, BlockLocation maxLoc) {
this.updateBounds(minLoc, maxLoc); this.updateBounds(minLoc, maxLoc);
} }
@ -123,7 +127,11 @@ public class AdvancedPortal implements TagTarget {
activationHandler.postActivated(this, player, data, this.getArgValues(portalTag.NAME)); activationHandler.postActivated(this, player, data, this.getArgValues(portalTag.NAME));
} }
} }
return true; if(data.hasActivated()) {
playerDataServices.getPlayerData(player).setNetherPortalCooldown(1000);
return true;
}
return false;
} }

View File

@ -34,12 +34,14 @@ public class PlayerData {
/** /**
* If the player is in a portal. Stops re-triggering. * If the player is in a portal. Stops re-triggering.
*/ */
private boolean isInPortal = false; private transient boolean isInPortal = false;
/** /**
* The next time System.currentTimeMillis() a player can use a portal. * The next time System.currentTimeMillis() a player can use a portal.
*/ */
private long globalCooldown; private transient long globalCooldown;
private transient long netherPortalCooldown;
private HashMap<String, String> perPortalCooldowns = new HashMap<>(); private HashMap<String, String> perPortalCooldowns = new HashMap<>();
@ -66,7 +68,7 @@ public class PlayerData {
} }
public void setGlobalCooldown(long globalCooldown) { public void setGlobalCooldown(long globalCooldown) {
this.globalCooldown = globalCooldown; this.globalCooldown = System.currentTimeMillis() + globalCooldown;
} }
public String getSelectedPortal() { public String getSelectedPortal() {
@ -100,4 +102,12 @@ public class PlayerData {
public void setInPortal(boolean inPortal) { public void setInPortal(boolean inPortal) {
isInPortal = inPortal; isInPortal = inPortal;
} }
public void setNetherPortalCooldown(long netherPortalCooldown) {
this.netherPortalCooldown = System.currentTimeMillis() + netherPortalCooldown;
}
public boolean isPortalCooldown() {
return System.currentTimeMillis() < netherPortalCooldown;
}
} }

View File

@ -28,7 +28,7 @@ public final class PlayerDataServices {
@Inject @Inject
private ConfigRepository configRepository; private ConfigRepository configRepository;
public PlayerData getPlayerTempData(PlayerContainer player) { public PlayerData getPlayerData(PlayerContainer player) {
return tempDataMap.computeIfAbsent(player.getUUID(), uuid -> { return tempDataMap.computeIfAbsent(player.getUUID(), uuid -> {
var tempData = tempDataRepository.get(player.getUUID().toString()); var tempData = tempDataRepository.get(player.getUUID().toString());
@ -40,17 +40,17 @@ public final class PlayerDataServices {
} }
public void activateCooldown(PlayerContainer player) { public void activateCooldown(PlayerContainer player) {
var tempData = getPlayerTempData(player); var tempData = getPlayerData(player);
tempData.setGlobalCooldown(System.currentTimeMillis() + configRepository.getPortalCooldown()); tempData.setGlobalCooldown(configRepository.getPortalCooldown() * 1000);
} }
public void playerLeave(PlayerContainer player) { public void playerLeave(PlayerContainer player) {
tempDataRepository.save(player.getUUID().toString(), getPlayerTempData(player)); tempDataRepository.save(player.getUUID().toString(), getPlayerData(player));
tempDataMap.remove(player.getUUID()); tempDataMap.remove(player.getUUID());
} }
public void playerSelectorActivate(PlayerContainer player, BlockLocation blockLoc, boolean leftClick) { public void playerSelectorActivate(PlayerContainer player, BlockLocation blockLoc, boolean leftClick) {
var tempData = getPlayerTempData(player); var tempData = getPlayerData(player);
if(leftClick) { if(leftClick) {
tempData.setPos1(blockLoc); tempData.setPos1(blockLoc);
} else { } else {

View File

@ -73,7 +73,7 @@ public class PortalServices {
} }
public void playerMove(PlayerContainer player, PlayerLocation toLoc) { public void playerMove(PlayerContainer player, PlayerLocation toLoc) {
PlayerData tempData = playerDataServices.getPlayerTempData(player); PlayerData tempData = playerDataServices.getPlayerData(player);
if(tempData.getGlobalCooldown() > System.currentTimeMillis()) { if(tempData.getGlobalCooldown() > System.currentTimeMillis()) {
return; return;
@ -117,7 +117,7 @@ public class PortalServices {
} }
public AdvancedPortal createPortal(PlayerContainer player, ArrayList<DataTag> tags) { public AdvancedPortal createPortal(PlayerContainer player, ArrayList<DataTag> tags) {
PlayerData tempData = playerDataServices.getPlayerTempData(player); PlayerData tempData = playerDataServices.getPlayerData(player);
if(tempData.getPos1() == null || tempData.getPos2() == null) { if(tempData.getPos1() == null || tempData.getPos2() == null) {
player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("portal.error.selection.missing")); player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("portal.error.selection.missing"));

View File

@ -57,20 +57,30 @@ public class DestiTag implements Tag.Activation, Tag.AutoComplete, Tag.Split {
} }
@Override @Override
public void postActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { public void postActivated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData) {
} }
@Override @Override
public boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) { public boolean activated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData) {
System.out.println("Teleporting to destination");
Destination destination = destinationServices.getDestination(argData[0]); Destination destination = destinationServices.getDestination(argData[0]);
if (destination != null) { if (destination != null) {
var warpEffect = warpEffectRegistry.getVisualEffect("ender"); var warpEffectVisual = warpEffectRegistry.getVisualEffect("ender");
if (warpEffect != null) { if (warpEffectVisual != null) {
warpEffect.onWarpVisual(player, ac); warpEffectVisual.onWarpVisual(player, WarpEffect.Action.ENTER);
}
var warpEffectSound = warpEffectRegistry.getSoundEffect("ender");
if (warpEffectSound != null) {
warpEffectSound.onWarpSound(player, WarpEffect.Action.ENTER);
} }
player.teleport(destination.getLoc()); player.teleport(destination.getLoc());
if (warpEffectVisual != null) {
warpEffectVisual.onWarpVisual(player, WarpEffect.Action.EXIT);
}
if (warpEffectSound != null) {
warpEffectSound.onWarpSound(player, WarpEffect.Action.EXIT);
}
activationData.setWarpStatus(ActivationData.WarpedStatus.WARPED);
} }
return true; return true;
} }

View File

@ -129,7 +129,6 @@ public class Lang {
String line = getNextLine(scanner); String line = getNextLine(scanner);
HashMap<String, String> newMap = new HashMap<>(); HashMap<String, String> newMap = new HashMap<>();
while (line != null) { while (line != null) {
//System.out.println(line);
if (!line.startsWith("#") && line.indexOf('=') > -1) { if (!line.startsWith("#") && line.indexOf('=') > -1) {
int split = line.indexOf('='); int split = line.indexOf('=');
String key = line.substring(0, split); String key = line.substring(0, split);

View File

@ -23,7 +23,7 @@ public class ActivationData {
public void setWarpStatus(WarpedStatus warped) { public void setWarpStatus(WarpedStatus warped) {
if (this.warpStatus == WarpedStatus.WARPED) { if (this.warpStatus == WarpedStatus.WARPED) {
return; return;
} else if (this.warpStatus == WarpedStatus.NOTACTIVATED) { } else if (this.warpStatus == WarpedStatus.ACTIVATED && warped != WarpedStatus.WARPED) {
return; return;
} }
this.warpStatus = warped; this.warpStatus = warped;
@ -46,6 +46,10 @@ public class ActivationData {
this.warpAllowed = allowed; this.warpAllowed = allowed;
} }
public boolean hasActivated() {
return this.warpStatus != WarpedStatus.NOTACTIVATED;
}
public enum WarpedStatus { public enum WarpedStatus {
/** /**
* Player has moved or something major has happened. (only one of these should activate) * Player has moved or something major has happened. (only one of these should activate)

View File

@ -128,10 +128,10 @@ public interface Tag {
* Any actions to do with player location should be done in activate * Any actions to do with player location should be done in activate
* *
* @param player * @param player
* @param activeData * @param activationData
* @param argData * @param argData
*/ */
void postActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData); void postActivated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData);
/** /**
* Activates if the portal is allowed from preActivating. Should be used to set the intended warp location * Activates if the portal is allowed from preActivating. Should be used to set the intended warp location
@ -140,12 +140,12 @@ public interface Tag {
* triggered here if a desti is listed. * triggered here if a desti is listed.
* *
* @param player * @param player
* @param activeData * @param activationData
* @param argData * @param argData
* *
* @return Action performed (only return false if the tag failed to do anything) * @return Action performed (only return false if the tag failed to do anything)
*/ */
boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData); boolean activated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData);
} }

View File

@ -65,7 +65,7 @@ public class Listeners implements Listener {
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPortalEvent(PlayerPortalEvent event) { public void onPortalEvent(PlayerPortalEvent event) {
if(!this.coreListeners.portalEvent(new SpigotPlayerContainer(event.getPlayer()))) { if(!this.coreListeners.entityPortalEvent(new SpigotPlayerContainer(event.getPlayer()))) {
event.setCancelled(true); event.setCancelled(true);
} }
} }

View File

@ -120,12 +120,13 @@ public class UpdatePortalSubCommand implements SubCommand {
int count = 0; int count = 0;
for(String destiName : destiSet) { for(String destiName : destiSet) {
var destiPos = destiName + ".pos";
var desti = destinationServices.createDesti(new PlayerLocation(config.getString(destiName + ".world"), var desti = destinationServices.createDesti(new PlayerLocation(config.getString(destiName + ".world"),
config.getDouble(destiName + ".x"), config.getDouble(destiPos + ".X"),
config.getDouble(destiName + ".y"), config.getDouble(destiPos + ".Y"),
config.getDouble(destiName + ".z"), config.getDouble(destiPos + ".Z"),
(float) config.getDouble(destiName + ".yaw"), (float) config.getDouble(destiPos + ".yaw"),
(float) config.getDouble(destiName + ".pitch")), List.of(new DataTag("name", destiName))); (float) config.getDouble(destiPos + ".pitch")), List.of(new DataTag("name", destiName)));
if(desti != null) count++; if(desti != null) count++;
} }
return count; return count;

View File

@ -53,7 +53,6 @@ public class SpigotEntityContainer implements EntityContainer {
return this.entity.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ())); return this.entity.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ()));
} }
@Override @Override
public WorldContainer getWorld() { public WorldContainer getWorld() {
return new SpigotWorldContainer(this.entity.getWorld()); return new SpigotWorldContainer(this.entity.getWorld());
@ -63,4 +62,9 @@ public class SpigotEntityContainer implements EntityContainer {
public String getName() { public String getName() {
return this.entity.getName(); return this.entity.getName();
} }
@Override
public String getWorldName() {
return this.entity.getWorld().getName();
}
} }

View File

@ -5,24 +5,32 @@ import com.sekwah.advancedportals.core.effect.WarpEffect;
import com.sekwah.advancedportals.core.portal.AdvancedPortal; import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.serializeddata.WorldLocation; import com.sekwah.advancedportals.core.serializeddata.WorldLocation;
import com.sekwah.advancedportals.spigot.connector.container.SpigotPlayerContainer; import com.sekwah.advancedportals.spigot.connector.container.SpigotPlayerContainer;
import org.bukkit.Effect;
public class EnderWarpEffect implements WarpEffect.Visual, WarpEffect.Sound { public class EnderWarpEffect implements WarpEffect.Visual, WarpEffect.Sound {
@Override @Override
public void onWarpSound(PlayerContainer playerContainer, WarpEffect.Action action, AdvancedPortal portal) { public void onWarpSound(PlayerContainer playerContainer, WarpEffect.Action action) {
if(playerContainer instanceof SpigotPlayerContainer spigotPlayerContainer) { if(playerContainer instanceof SpigotPlayerContainer spigotPlayerContainer) {
var player = spigotPlayerContainer.getPlayer(); var player = spigotPlayerContainer.getPlayer();
player.playSound(player.getLocation(), "entity.enderman.teleport", 1, 1); player.getWorld().playSound(player.getLocation(), "entity.enderman.teleport", 1, 1);
} }
} }
@Override @Override
public void onWarpVisual(PlayerContainer player, WarpEffect.Action action, AdvancedPortal portal) { public void onWarpVisual(PlayerContainer playerContainer, WarpEffect.Action action) {
if (action == WarpEffect.Action.ENTER) { if(playerContainer instanceof SpigotPlayerContainer spigotPlayerContainer) {
player.spawnParticle(new WorldLocation(portal.getPortalLocation()), "ENDER_SIGNAL", 1, 1, 1, 1, 1); var player = spigotPlayerContainer.getPlayer();
} else { var world = player.getWorld();
player.spawnParticle(new WorldLocation(portal.getPortalLocation()), "ENDER_SIGNAL", 1, 1, 1, 1, 1); var loc = player.getLocation().clone();
for(int i = 0; i < 10; i++){
world.playEffect(loc, Effect.ENDER_SIGNAL, 0);
}
loc.add(0D, 1D, 0D);
for(int i = 0; i < 10; i++){
world.playEffect(loc, Effect.ENDER_SIGNAL, 0);
}
} }
} }
} }