Skip to content

Commit

Permalink
feat: portals now trigger and nether portals are cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent 306f973 commit f9f5a9d
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,16 @@ public boolean preventEntityCombust(EntityContainer entity) {
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) {
return !portalServices.inPortalRegion(entity.getBlockLoc(), 1)
&& (!(entity.getHeight() > 1) || !portalServices.inPortalRegion(entity.getBlockLoc().addY((int) entity.getHeight()), 1));
var pos = entity.getBlockLoc();
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.awt.*;
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.
Expand Down Expand Up @@ -47,7 +48,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
return;
}

var tempData = tempDataServices.getPlayerTempData(sender.getPlayerContainer());
var tempData = tempDataServices.getPlayerData(sender.getPlayerContainer());
if(tempData.isDestiVisible()) {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.destination.show.disabled"));
} else {
Expand Down Expand Up @@ -80,14 +81,14 @@ public String getDetailedHelpText() {
public void registered() {
gameScheduler.intervalTickEvent("show_portal", () -> {
for(PlayerContainer player : serverContainer.getPlayers()) {
var tempData = tempDataServices.getPlayerTempData(player);
var tempData = tempDataServices.getPlayerData(player);
if(!tempData.isDestiVisible()) {
continue;
}

for (Destination destination : destinationServices.getDestinations()) {
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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.awt.*;
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.
Expand All @@ -29,7 +30,7 @@ public class ShowPortalSubCommand implements SubCommand, SubCommand.SubCommandOn
boolean alternate_show_trigger = true;

@Inject
PlayerDataServices tempDataServices;
PlayerDataServices playerDataServices;

@Inject
GameScheduler gameScheduler;
Expand Down Expand Up @@ -64,7 +65,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
return;
}

var tempData = tempDataServices.getPlayerTempData(sender.getPlayerContainer());
var tempData = playerDataServices.getPlayerData(sender.getPlayerContainer());
if(tempData.isPortalVisible()) {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.portal.show.disabled"));
} else {
Expand Down Expand Up @@ -98,26 +99,26 @@ public void registered() {
gameScheduler.intervalTickEvent("show_portal", () -> {
alternate_show_trigger = !alternate_show_trigger;
for(PlayerContainer player : serverContainer.getPlayers()) {
var tempData = tempDataServices.getPlayerTempData(player);
var tempData = playerDataServices.getPlayerData(player);
if(!tempData.isPortalVisible()) {
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);
}

if(tempData.getPos1() != null) {
if(tempData.getPos1() != null && tempData.getPos1().worldName.equals(player.getWorldName())) {
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);
}

var world = player.getWorld();
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 maxLoc = portal.getMaxLoc();
int midX = (minLoc.posX + maxLoc.posX) / 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface EntityContainer {
WorldContainer getWorld();

String getName();

String getWorldName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ enum Action {

interface Sound extends WarpEffect {

void onWarpSound(PlayerContainer player, Action action, AdvancedPortal portal);
void onWarpSound(PlayerContainer player, Action action);

}

interface Visual extends WarpEffect {

void onWarpVisual(PlayerContainer player, Action action, AdvancedPortal portal);
void onWarpVisual(PlayerContainer player, Action action);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.registry.TagRegistry;
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.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;
Expand All @@ -33,6 +34,9 @@ public class AdvancedPortal implements TagTarget {
@SerializedName("a")
private HashMap<String, String[]> args = new HashMap<>();

@Inject
transient PlayerDataServices playerDataServices;

public AdvancedPortal(BlockLocation minLoc, BlockLocation maxLoc) {
this.updateBounds(minLoc, maxLoc);
}
Expand Down Expand Up @@ -123,7 +127,11 @@ public boolean activate(PlayerContainer player) {
activationHandler.postActivated(this, player, data, this.getArgValues(portalTag.NAME));
}
}
return true;
if(data.hasActivated()) {
playerDataServices.getPlayerData(player).setNetherPortalCooldown(1000);
return true;
}
return false;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public class PlayerData {
/**
* 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.
*/
private long globalCooldown;
private transient long globalCooldown;

private transient long netherPortalCooldown;

private HashMap<String, String> perPortalCooldowns = new HashMap<>();

Expand All @@ -66,7 +68,7 @@ public long getGlobalCooldown() {
}

public void setGlobalCooldown(long globalCooldown) {
this.globalCooldown = globalCooldown;
this.globalCooldown = System.currentTimeMillis() + globalCooldown;
}

public String getSelectedPortal() {
Expand Down Expand Up @@ -100,4 +102,12 @@ public boolean isInPortal() {
public void setInPortal(boolean inPortal) {
isInPortal = inPortal;
}

public void setNetherPortalCooldown(long netherPortalCooldown) {
this.netherPortalCooldown = System.currentTimeMillis() + netherPortalCooldown;
}

public boolean isPortalCooldown() {
return System.currentTimeMillis() < netherPortalCooldown;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public final class PlayerDataServices {
@Inject
private ConfigRepository configRepository;

public PlayerData getPlayerTempData(PlayerContainer player) {
public PlayerData getPlayerData(PlayerContainer player) {
return tempDataMap.computeIfAbsent(player.getUUID(), uuid -> {
var tempData = tempDataRepository.get(player.getUUID().toString());

Expand All @@ -40,17 +40,17 @@ public PlayerData getPlayerTempData(PlayerContainer player) {
}

public void activateCooldown(PlayerContainer player) {
var tempData = getPlayerTempData(player);
tempData.setGlobalCooldown(System.currentTimeMillis() + configRepository.getPortalCooldown());
var tempData = getPlayerData(player);
tempData.setGlobalCooldown(configRepository.getPortalCooldown() * 1000);
}

public void playerLeave(PlayerContainer player) {
tempDataRepository.save(player.getUUID().toString(), getPlayerTempData(player));
tempDataRepository.save(player.getUUID().toString(), getPlayerData(player));
tempDataMap.remove(player.getUUID());
}

public void playerSelectorActivate(PlayerContainer player, BlockLocation blockLoc, boolean leftClick) {
var tempData = getPlayerTempData(player);
var tempData = getPlayerData(player);
if(leftClick) {
tempData.setPos1(blockLoc);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public boolean inPortalRegion(BlockLocation loc, int extraBlocks) {
}

public void playerMove(PlayerContainer player, PlayerLocation toLoc) {
PlayerData tempData = playerDataServices.getPlayerTempData(player);
PlayerData tempData = playerDataServices.getPlayerData(player);

if(tempData.getGlobalCooldown() > System.currentTimeMillis()) {
return;
Expand Down Expand Up @@ -117,7 +117,7 @@ public AdvancedPortal createPortal(BlockLocation pos1, BlockLocation pos2, List<
}

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) {
player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("portal.error.selection.missing"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,30 @@ public boolean preActivated(TagTarget target, PlayerContainer player, Activation
}

@Override
public void postActivated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) {
public void postActivated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData) {

}

@Override
public boolean activated(TagTarget target, PlayerContainer player, ActivationData activeData, String[] argData) {
System.out.println("Teleporting to destination");
public boolean activated(TagTarget target, PlayerContainer player, ActivationData activationData, String[] argData) {
Destination destination = destinationServices.getDestination(argData[0]);
if (destination != null) {
var warpEffect = warpEffectRegistry.getVisualEffect("ender");
if (warpEffect != null) {
warpEffect.onWarpVisual(player, ac);
var warpEffectVisual = warpEffectRegistry.getVisualEffect("ender");
if (warpEffectVisual != null) {
warpEffectVisual.onWarpVisual(player, WarpEffect.Action.ENTER);
}
var warpEffectSound = warpEffectRegistry.getSoundEffect("ender");
if (warpEffectSound != null) {
warpEffectSound.onWarpSound(player, WarpEffect.Action.ENTER);
}
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ public static Map<String, String> parseLang(InputStream inputStream) {
String line = getNextLine(scanner);
HashMap<String, String> newMap = new HashMap<>();
while (line != null) {
//System.out.println(line);
if (!line.startsWith("#") && line.indexOf('=') > -1) {
int split = line.indexOf('=');
String key = line.substring(0, split);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public WarpedStatus getWarped() {
public void setWarpStatus(WarpedStatus warped) {
if (this.warpStatus == WarpedStatus.WARPED) {
return;
} else if (this.warpStatus == WarpedStatus.NOTACTIVATED) {
} else if (this.warpStatus == WarpedStatus.ACTIVATED && warped != WarpedStatus.WARPED) {
return;
}
this.warpStatus = warped;
Expand All @@ -46,6 +46,10 @@ public void setAllowed(boolean allowed) {
this.warpAllowed = allowed;
}

public boolean hasActivated() {
return this.warpStatus != WarpedStatus.NOTACTIVATED;
}

public enum WarpedStatus {
/**
* Player has moved or something major has happened. (only one of these should activate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ interface Activation extends Tag {
* Any actions to do with player location should be done in activate
*
* @param player
* @param activeData
* @param activationData
* @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
Expand All @@ -140,12 +140,12 @@ interface Activation extends Tag {
* triggered here if a desti is listed.
*
* @param player
* @param activeData
* @param activationData
* @param argData
*
* @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);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ public int importDestinations() {

int count = 0;
for(String destiName : destiSet) {
var destiPos = destiName + ".pos";
var desti = destinationServices.createDesti(new PlayerLocation(config.getString(destiName + ".world"),
config.getDouble(destiName + ".x"),
config.getDouble(destiName + ".y"),
config.getDouble(destiName + ".z"),
(float) config.getDouble(destiName + ".yaw"),
(float) config.getDouble(destiName + ".pitch")), List.of(new DataTag("name", destiName)));
config.getDouble(destiPos + ".X"),
config.getDouble(destiPos + ".Y"),
config.getDouble(destiPos + ".Z"),
(float) config.getDouble(destiPos + ".yaw"),
(float) config.getDouble(destiPos + ".pitch")), List.of(new DataTag("name", destiName)));
if(desti != null) count++;
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public boolean teleport(PlayerLocation location) {
return this.entity.teleport(new Location(Bukkit.getWorld(location.getWorldName()), location.getPosX(), location.getPosY(), location.getPosZ()));
}


@Override
public WorldContainer getWorld() {
return new SpigotWorldContainer(this.entity.getWorld());
Expand All @@ -63,4 +62,9 @@ public WorldContainer getWorld() {
public String getName() {
return this.entity.getName();
}

@Override
public String getWorldName() {
return this.entity.getWorld().getName();
}
}
Loading

0 comments on commit f9f5a9d

Please sign in to comment.