Skip to content

Commit

Permalink
feat: enhance the in portal mitigation
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Sep 8, 2024
1 parent 3c968d2 commit ffbc015
Show file tree
Hide file tree
Showing 25 changed files with 276 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import com.sekwah.advancedportals.core.services.DestinationServices;
import com.sekwah.advancedportals.core.services.PlayerDataServices;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.tags.activation.*;
import com.sekwah.advancedportals.core.tags.*;
import com.sekwah.advancedportals.core.util.GameScheduler;
import com.sekwah.advancedportals.core.util.InfoLogger;
import com.sekwah.advancedportals.core.util.Lang;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.util.GameScheduler;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.warphandler.TriggerType;

import java.util.Objects;

public class CoreListeners {
Expand All @@ -31,7 +33,18 @@ public class CoreListeners {

public void playerJoin(PlayerContainer player) {
this.playerDataServices.setJoinCooldown(player);
this.playerDataServices.getPlayerData(player).setInPortal(true);

this.setIfInPortal(player);
}

private void setIfInPortal(PlayerContainer player) {
String inPortal = this.portalServices.inPortalRegionGetName(player.getBlockLoc());

if (inPortal == null) {
inPortal = this.portalServices.inPortalRegionGetName(player.getBlockLoc().addY((int) player.getHeight()));
}

this.playerDataServices.getPlayerData(player).setInPortal(inPortal);
}

public void teleportEvent(PlayerContainer player) {
Expand All @@ -51,7 +64,7 @@ public void tick() {
* @param toLoc
*/
public void playerMove(PlayerContainer player, PlayerLocation toLoc) {
this.portalServices.playerMove(player, toLoc);
this.portalServices.checkPortalActivation(player, toLoc, TriggerType.MOVEMENT);
}

/**
Expand Down Expand Up @@ -177,7 +190,7 @@ public boolean playerInteractWithBlock(PlayerContainer player,

public void worldChange(PlayerContainer player) {
this.playerDataServices.setJoinCooldown(player);
this.playerDataServices.getPlayerData(player).setInPortal(true);
this.setIfInPortal(player);
}

public boolean preventEntityCombust(EntityContainer entity) {
Expand All @@ -188,13 +201,35 @@ public boolean entityPortalEvent(EntityContainer entity) {
var pos = entity.getBlockLoc();
if (entity instanceof PlayerContainer player) {
var playerData = playerDataServices.getPlayerData(player);
if (playerData.isNetherPortalCooldown()) {
if (playerData.getPortalBlockCooldown()) {
return false;
}
}

return !(portalServices.inPortalRegion(pos, 1) || portalServices.inPortalRegion(pos.addY((int) entity.getHeight()), 1));
}

public boolean playerPortalEvent(PlayerContainer player, PlayerLocation toLoc) {

var playerData = playerDataServices.getPlayerData(player);

if (playerData.getPortalBlockCooldown()) {
return false;
}

var portalResult = this.portalServices.checkPortalActivation(player, toLoc, TriggerType.MOVEMENT);

if(portalResult != PortalServices.PortalActivationResult.NOT_IN_PORTAL) {
return false;
}

// Extra checks to prevent the player from being teleported by touching a portal but not having their body fully in the portal
var pos = player.getBlockLoc();

var feetInPortal = portalServices.inPortalRegion(pos, 1);
var headInPortal = portalServices.inPortalRegion(
pos.addY((int) entity.getHeight()), 1);
pos.addY((int) player.getHeight()), 1);

return !(feetInPortal || headInPortal);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.tags.activation.TriggerBlockTag;
import com.sekwah.advancedportals.core.tags.NameTag;
import com.sekwah.advancedportals.core.tags.TriggerBlockTag;
import com.sekwah.advancedportals.core.util.InfoLogger;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.util.TagReader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.services.PlayerDataServices;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.tags.NameTag;
import com.sekwah.advancedportals.core.util.Debug;
import com.sekwah.advancedportals.core.util.GameScheduler;
import com.sekwah.advancedportals.core.util.Lang;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sekwah.advancedportals.core.connector.containers;

import com.sekwah.advancedportals.core.serializeddata.BlockLocation;
import com.sekwah.advancedportals.core.tags.activation.CommandTag;

import java.util.UUID;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.sekwah.advancedportals.core.connector.containers;

import com.sekwah.advancedportals.core.tags.activation.CommandTag;
import com.sekwah.advancedportals.core.tags.CommandTag;
import java.util.List;
import java.util.UUID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;
import com.sekwah.advancedportals.core.warphandler.TriggerType;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -65,7 +67,7 @@ public void removeArg(String arg) {
}

public boolean activate(PlayerContainer player) {
ActivationData data = new ActivationData(false);
ActivationData data = new ActivationData(TriggerType.MANUAL);
this.portalActivate(player, data);
this.postActivate(player, data);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sekwah.advancedportals.core.portal;

public enum ActivationResult {
SUCCESS,
FAILED_DO_KNOCKBACK,
FAILED_DO_NOTHING

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import com.sekwah.advancedportals.core.serializeddata.DataTag;
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.TriggerBlockTag;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;
import com.sekwah.advancedportals.core.warphandler.TriggerType;

import java.util.*;

/**
Expand Down Expand Up @@ -107,14 +109,12 @@ public void updateBounds(BlockLocation loc1, BlockLocation loc2) {
* @param player The player on the server attempting to use an
* advanced
* portal
* @param moveActivated if the portal was activated by a move event (won't
* trigger knockback)
* @return
* @param triggerType The type of trigger that activated the portal
* @return Whether the portal was successfully activated
*/
public boolean activate(PlayerContainer player, boolean moveActivated) {
public ActivationResult activate(PlayerContainer player, TriggerType triggerType) {
var playerData = playerDataServices.getPlayerData(player);
if (playerData.isInPortal())
return false;

if (playerData.hasJoinCooldown()) {
var cooldown =
(int) Math.ceil(playerData.getJoinCooldownLeft() / 1000D);
Expand All @@ -127,10 +127,10 @@ public boolean activate(PlayerContainer player, boolean moveActivated) {
player.playSound("block.portal.travel", 0.05f,
rand.nextFloat() * 0.4F + 0.8F);
}
return false;
return ActivationResult.FAILED_DO_KNOCKBACK;
}

ActivationData data = new ActivationData(moveActivated);
ActivationData data = new ActivationData(triggerType);
DataTag[] portalTags = new DataTag[args.size()];
int i = 0;
for (Map.Entry<String, String[]> entry : args.entrySet()) {
Expand All @@ -143,7 +143,7 @@ public boolean activate(PlayerContainer player, boolean moveActivated) {
if (activationHandler != null
&& !activationHandler.preActivated(
this, player, data, this.getArgValues(portalTag.NAME))) {
return false;
return ActivationResult.FAILED_DO_KNOCKBACK;
}
}
for (DataTag portalTag : portalTags) {
Expand All @@ -152,7 +152,7 @@ public boolean activate(PlayerContainer player, boolean moveActivated) {
if (activationHandler != null
&& !activationHandler.activated(
this, player, data, this.getArgValues(portalTag.NAME))) {
return false;
return ActivationResult.FAILED_DO_KNOCKBACK;
}
}
for (DataTag portalTag : portalTags) {
Expand All @@ -164,11 +164,11 @@ public boolean activate(PlayerContainer player, boolean moveActivated) {
}
}
if (data.hasActivated()) {
playerData.setNetherPortalCooldown(1000);
playerData.setInPortal(true);
return true;
playerData.setPortalBlockCooldown(1000);
playerData.setInPortal(this.getName());
return ActivationResult.SUCCESS;
}
return false;
return ActivationResult.FAILED_DO_KNOCKBACK;
}

public boolean isLocationInPortal(BlockLocation loc) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.sekwah.advancedportals.core.destination.Destination;
import com.sekwah.advancedportals.core.repository.IDestinationRepository;
import com.sekwah.advancedportals.core.serializeddata.DataStorage;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.tags.NameTag;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Singleton;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.repository.IPortalRepository;
import com.sekwah.advancedportals.core.serializeddata.DataStorage;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.tags.NameTag;
import java.util.*;

@Singleton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public class PlayerData {
/**
* If the player is in a portal. Stops re-triggering.
*/
private transient boolean isInPortal = false;
private transient String inPortal = null;

/**
* The next time System.currentTimeMillis() a player can use a portal.
*/
private transient long joinCooldown;

private transient long netherPortalCooldown;
private transient long portalBlockCooldown;

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

Expand Down Expand Up @@ -97,25 +97,25 @@ public void setDestiVisible(boolean destiVisible) {
this.destiVisible = destiVisible;
}

public boolean isInPortal() {
return isInPortal;
public String inPortal() {
return this.inPortal;
}

public void setInPortal(boolean inPortal) {
isInPortal = inPortal;
public void setInPortal(String inPortal) {
this.inPortal = inPortal;
}

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

public boolean hasJoinCooldown() {
return System.currentTimeMillis() < joinCooldown;
}

public boolean isNetherPortalCooldown() {
return System.currentTimeMillis() < netherPortalCooldown;
public boolean getPortalBlockCooldown() {
return System.currentTimeMillis() < portalBlockCooldown;
}

public void setPortalCooldown(String portalName, long cooldown) {
Expand All @@ -131,4 +131,8 @@ public boolean hasPortalCooldown(String portalName) {
public double getPortalCooldownLeft(String portalName) {
return perPortalCooldowns.get(portalName) - System.currentTimeMillis();
}

public void setNotInPortal() {
this.inPortal = null;
}
}
Loading

0 comments on commit ffbc015

Please sign in to comment.