diff --git a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java index 87112b52..a227e693 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/AdvancedPortalsCore.java @@ -16,7 +16,7 @@ import com.sekwah.advancedportals.core.serializeddata.DataStorage; 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; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java b/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java index 4d8af696..9e2e57b5 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/CoreListeners.java @@ -14,6 +14,8 @@ import com.sekwah.advancedportals.core.services.PlayerDataServices; 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 { @@ -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) { @@ -51,7 +64,7 @@ public class CoreListeners { * @param toLoc */ public void playerMove(PlayerContainer player, PlayerLocation toLoc) { - this.portalServices.playerMove(player, toLoc); + this.portalServices.checkPortalActivation(player, toLoc, TriggerType.MOVEMENT); } /** @@ -177,7 +190,7 @@ public class CoreListeners { public void worldChange(PlayerContainer player) { this.playerDataServices.setJoinCooldown(player); - this.playerDataServices.getPlayerData(player).setInPortal(true); + this.setIfInPortal(player); } public boolean preventEntityCombust(EntityContainer entity) { @@ -188,13 +201,35 @@ public class CoreListeners { 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); } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java index 17ba71dd..be4f0a01 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/CreatePortalSubCommand.java @@ -10,8 +10,8 @@ import com.sekwah.advancedportals.core.registry.TagRegistry; 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; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java index c8ee98d9..b2cf23a2 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/commands/subcommands/portal/ShowPortalSubCommand.java @@ -11,7 +11,7 @@ import com.sekwah.advancedportals.core.repository.ConfigRepository; 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; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/PlayerContainer.java b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/PlayerContainer.java index b50510a5..5c438209 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/PlayerContainer.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/PlayerContainer.java @@ -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; /** diff --git a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/ServerContainer.java b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/ServerContainer.java index ad73b423..3ff60a26 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/ServerContainer.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/connector/containers/ServerContainer.java @@ -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; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java b/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java index 0f6cb15a..4f0481d1 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/destination/Destination.java @@ -8,6 +8,8 @@ import com.sekwah.advancedportals.core.serializeddata.DataTag; 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; @@ -65,7 +67,7 @@ public class Destination implements TagTarget { } 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; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/portal/ActivationResult.java b/core/src/main/java/com/sekwah/advancedportals/core/portal/ActivationResult.java new file mode 100644 index 00000000..55a79083 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/portal/ActivationResult.java @@ -0,0 +1,8 @@ +package com.sekwah.advancedportals.core.portal; + +public enum ActivationResult { + SUCCESS, + FAILED_DO_KNOCKBACK, + FAILED_DO_NOTHING + +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java b/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java index c910e90d..eab3dc79 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/portal/AdvancedPortal.java @@ -9,10 +9,12 @@ import com.sekwah.advancedportals.core.serializeddata.BlockLocation; 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.*; /** @@ -107,14 +109,12 @@ public class AdvancedPortal implements TagTarget { * @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); @@ -127,10 +127,10 @@ public class AdvancedPortal implements TagTarget { 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 entry : args.entrySet()) { @@ -143,7 +143,7 @@ public class AdvancedPortal implements TagTarget { if (activationHandler != null && !activationHandler.preActivated( this, player, data, this.getArgValues(portalTag.NAME))) { - return false; + return ActivationResult.FAILED_DO_KNOCKBACK; } } for (DataTag portalTag : portalTags) { @@ -152,7 +152,7 @@ public class AdvancedPortal implements TagTarget { if (activationHandler != null && !activationHandler.activated( this, player, data, this.getArgValues(portalTag.NAME))) { - return false; + return ActivationResult.FAILED_DO_KNOCKBACK; } } for (DataTag portalTag : portalTags) { @@ -164,11 +164,11 @@ public class AdvancedPortal implements TagTarget { } } 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) { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java index 2a40fa3f..28d29e33 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/DestinationRepositoryImpl.java @@ -4,7 +4,7 @@ import com.google.inject.Inject; 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; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java index 8d55a62e..eb030798 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/repository/impl/PortalRepositoryImpl.java @@ -6,7 +6,7 @@ import com.sekwah.advancedportals.core.AdvancedPortalsCore; 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 diff --git a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java index 4f83a1f8..974bc94e 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/serializeddata/PlayerData.java @@ -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 perPortalCooldowns = new HashMap<>(); @@ -97,25 +97,25 @@ public class PlayerData { 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) { @@ -131,4 +131,8 @@ public class PlayerData { public double getPortalCooldownLeft(String portalName) { return perPortalCooldowns.get(portalName) - System.currentTimeMillis(); } + + public void setNotInPortal() { + this.inPortal = null; + } } diff --git a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java index 0fc78df6..e2159f38 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/services/PortalServices.java @@ -2,6 +2,7 @@ package com.sekwah.advancedportals.core.services; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.portal.ActivationResult; import com.sekwah.advancedportals.core.portal.AdvancedPortal; import com.sekwah.advancedportals.core.registry.TagRegistry; import com.sekwah.advancedportals.core.repository.ConfigRepository; @@ -10,10 +11,12 @@ import com.sekwah.advancedportals.core.serializeddata.BlockLocation; import com.sekwah.advancedportals.core.serializeddata.DataTag; import com.sekwah.advancedportals.core.serializeddata.PlayerData; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; -import com.sekwah.advancedportals.core.tags.activation.NameTag; +import com.sekwah.advancedportals.core.tags.NameTag; import com.sekwah.advancedportals.core.util.Lang; import com.sekwah.advancedportals.core.util.PlayerUtils; import com.sekwah.advancedportals.core.warphandler.Tag; +import com.sekwah.advancedportals.core.warphandler.TriggerType; + import java.util.*; import javax.inject.Singleton; @@ -67,42 +70,67 @@ public class PortalServices { } public boolean inPortalRegion(BlockLocation loc, int extraBlocks) { - for (AdvancedPortal portal : portalCache.values()) { - if (portal.isLocationInPortal(loc, extraBlocks)) { - return true; - } - } - return false; + return inPortalRegionGetName(loc, extraBlocks) != null; } - public void playerMove(PlayerContainer player, PlayerLocation toLoc) { + public String inPortalRegionGetName(PlayerLocation loc, int extraBlocks) { + return inPortalRegionGetName(loc.toBlockPos(), extraBlocks); + } + + public String inPortalRegionGetName(BlockLocation loc) { + return inPortalRegionGetName(loc, 0); + } + + public String inPortalRegionGetName(BlockLocation loc, int extraBlocks) { + for (AdvancedPortal portal : portalCache.values()) { + if (portal.isLocationInPortal(loc, extraBlocks)) { + return portal.getName(); + } + } + return null; + } + + public enum PortalActivationResult { + NOT_IN_PORTAL, + PORTAL_TELEPORTED, + PORTAL_ACTIVATED, + PORTAL_DENIED + } + + public PortalActivationResult checkPortalActivation(PlayerContainer player, PlayerLocation toLoc, TriggerType triggerType) { var blockLoc = toLoc.toBlockPos(); var blockEntityTopLoc = blockLoc.addY(player.getHeight()); var world = player.getWorld(); var blockMaterial = world.getBlock(blockLoc); var blockEntityTopMaterial = world.getBlock(blockEntityTopLoc); + var playerData = playerDataServices.getPlayerData(player); - var notInPortal = true; for (AdvancedPortal portal : portalCache.values()) { if ((portal.isLocationInPortal(toLoc) && portal.isTriggerBlock(blockMaterial)) || (portal.isLocationInPortal(blockEntityTopLoc) && portal.isTriggerBlock(blockEntityTopMaterial))) { - notInPortal = false; - if (portal.activate(player, true)) { - return; + var portalName = portal.getName(); + if(Objects.equals(playerData.inPortal(), portalName)) { + return PortalActivationResult.PORTAL_DENIED; + } + switch (portal.activate(player, triggerType)) { + case SUCCESS: + playerData.setInPortal(portal.getName()); + return PortalActivationResult.PORTAL_ACTIVATED; + case FAILED_DO_KNOCKBACK: + playerData.setInPortal(portal.getName()); + var strength = configRepository.getThrowbackStrength(); + PlayerUtils.throwPlayerBack(player, strength); + + return PortalActivationResult.PORTAL_DENIED; } } } - var playerData = playerDataServices.getPlayerData(player); - if (!playerData.isInPortal() && !notInPortal) { - playerData.setInPortal(true); - var strength = configRepository.getThrowbackStrength(); - PlayerUtils.throwPlayerBack(player, strength); - } - if (playerData.isInPortal() && notInPortal) { - playerData.setInPortal(false); + if (playerData.inPortal() != null) { + playerData.setNotInPortal(); } + return PortalActivationResult.NOT_IN_PORTAL; } public List getPortalNames() { diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/CommandTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/CommandTag.java similarity index 99% rename from core/src/main/java/com/sekwah/advancedportals/core/tags/activation/CommandTag.java rename to core/src/main/java/com/sekwah/advancedportals/core/tags/CommandTag.java index 97c8f028..de2a959a 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/CommandTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/CommandTag.java @@ -1,4 +1,4 @@ -package com.sekwah.advancedportals.core.tags.activation; +package com.sekwah.advancedportals.core.tags; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/CooldownTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/CooldownTag.java similarity index 94% rename from core/src/main/java/com/sekwah/advancedportals/core/tags/activation/CooldownTag.java rename to core/src/main/java/com/sekwah/advancedportals/core/tags/CooldownTag.java index c4cf91d1..b515a322 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/CooldownTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/CooldownTag.java @@ -1,19 +1,16 @@ -package com.sekwah.advancedportals.core.tags.activation; +package com.sekwah.advancedportals.core.tags; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; -import com.sekwah.advancedportals.core.destination.Destination; -import com.sekwah.advancedportals.core.effect.WarpEffect; import com.sekwah.advancedportals.core.portal.AdvancedPortal; import com.sekwah.advancedportals.core.registry.TagTarget; import com.sekwah.advancedportals.core.repository.ConfigRepository; import com.sekwah.advancedportals.core.services.PlayerDataServices; import com.sekwah.advancedportals.core.util.InfoLogger; import com.sekwah.advancedportals.core.util.Lang; -import com.sekwah.advancedportals.core.util.PlayerUtils; import com.sekwah.advancedportals.core.warphandler.ActivationData; import com.sekwah.advancedportals.core.warphandler.Tag; -import java.util.List; + import java.util.Random; import javax.annotation.Nullable; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/DestiTag.java similarity index 98% rename from core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java rename to core/src/main/java/com/sekwah/advancedportals/core/tags/DestiTag.java index 65901520..35fdedf0 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/DestiTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/DestiTag.java @@ -1,4 +1,4 @@ -package com.sekwah.advancedportals.core.tags.activation; +package com.sekwah.advancedportals.core.tags; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/NameTag.java similarity index 97% rename from core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java rename to core/src/main/java/com/sekwah/advancedportals/core/tags/NameTag.java index 0b995a0d..4306d49d 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/NameTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/NameTag.java @@ -1,4 +1,4 @@ -package com.sekwah.advancedportals.core.tags.activation; +package com.sekwah.advancedportals.core.tags; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.registry.TagTarget; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/PermissionTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/PermissionTag.java similarity index 97% rename from core/src/main/java/com/sekwah/advancedportals/core/tags/activation/PermissionTag.java rename to core/src/main/java/com/sekwah/advancedportals/core/tags/PermissionTag.java index 910110bf..57251a28 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/PermissionTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/PermissionTag.java @@ -1,4 +1,4 @@ -package com.sekwah.advancedportals.core.tags.activation; +package com.sekwah.advancedportals.core.tags; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.registry.TagTarget; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/PortalEventTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/PortalEventTag.java new file mode 100644 index 00000000..8830fbb2 --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/PortalEventTag.java @@ -0,0 +1,118 @@ +package com.sekwah.advancedportals.core.tags; + +import com.google.inject.Inject; +import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; +import com.sekwah.advancedportals.core.portal.AdvancedPortal; +import com.sekwah.advancedportals.core.registry.TagTarget; +import com.sekwah.advancedportals.core.repository.ConfigRepository; +import com.sekwah.advancedportals.core.services.PlayerDataServices; +import com.sekwah.advancedportals.core.util.InfoLogger; +import com.sekwah.advancedportals.core.util.Lang; +import com.sekwah.advancedportals.core.warphandler.ActivationData; +import com.sekwah.advancedportals.core.warphandler.Tag; + +import javax.annotation.Nullable; +import java.util.Random; + +public class PortalEventTag implements Tag.Activation, Tag.Creation { + @Inject + PlayerDataServices playerDataServices; + + @Inject + ConfigRepository configRepository; + + @Inject + private InfoLogger infoLogger; + + public static String TAG_NAME = "cooldown"; + + private final TagType[] tagTypes = new TagType[] {TagType.PORTAL}; + + @Override + public TagType[] getTagTypes() { + return tagTypes; + } + + @Override + public String getName() { + return TAG_NAME; + } + + @Nullable + @Override + public String[] getAliases() { + return null; + } + + @Override + public String description() { + return Lang.translate("tag.cooldown.description"); + } + + @Override + public boolean preActivated(TagTarget target, PlayerContainer player, + ActivationData activationData, + String[] argData) { + var playerData = playerDataServices.getPlayerData(player); + if (target instanceof AdvancedPortal portal) { + var portalName = portal.getName(); + if (playerData.hasPortalCooldown(portalName)) { + var cooldown = (int) Math.ceil( + playerData.getPortalCooldownLeft(portalName) / 1000D); + player.sendMessage(Lang.translateInsertVariables( + "portal.cooldown.individual", cooldown, + Lang.translate(cooldown == 1 ? "time.second" + : "time.seconds"))); + if (configRepository.playFailSound()) { + player.playSound("block.portal.travel", 0.05f, + new Random().nextFloat() * 0.4F + 0.8F); + } + return false; + } + return true; + } + return false; + } + + @Override + public void postActivated(TagTarget target, PlayerContainer player, + ActivationData activationData, String[] argData) { + if (activationData.hasActivated()) { + if (target instanceof AdvancedPortal portal) { + var playerData = playerDataServices.getPlayerData(player); + try { + playerData.setPortalCooldown( + portal.getName(), Integer.parseInt(argData[0]) * 1000); + } catch (NumberFormatException e) { + infoLogger.warning( + "Cooldown tag failed to set cooldown for portal: " + + portal.getName() + " with value: " + argData[0]); + } + } + } + } + + @Override + public boolean activated(TagTarget target, PlayerContainer player, + ActivationData activationData, String[] argData) { + return true; + } + + @Override + public boolean created(TagTarget target, PlayerContainer player, + String[] argData) { + try { + Integer.parseInt(argData[0]); + } catch (NumberFormatException e) { + player.sendMessage(Lang.translate("messageprefix.negative") + + Lang.translate("tag.cooldown.fail")); + return false; + } + return true; + } + + @Override + public void destroyed(TagTarget target, PlayerContainer player, + String[] argData) { + } +} diff --git a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java b/core/src/main/java/com/sekwah/advancedportals/core/tags/TriggerBlockTag.java similarity index 95% rename from core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java rename to core/src/main/java/com/sekwah/advancedportals/core/tags/TriggerBlockTag.java index d1d58b8d..7fc2442f 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/tags/activation/TriggerBlockTag.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/tags/TriggerBlockTag.java @@ -1,4 +1,4 @@ -package com.sekwah.advancedportals.core.tags.activation; +package com.sekwah.advancedportals.core.tags; import com.google.inject.Inject; import com.sekwah.advancedportals.core.connector.containers.ServerContainer; diff --git a/core/src/main/java/com/sekwah/advancedportals/core/warphandler/ActivationData.java b/core/src/main/java/com/sekwah/advancedportals/core/warphandler/ActivationData.java index 075dea74..2e6a7b93 100644 --- a/core/src/main/java/com/sekwah/advancedportals/core/warphandler/ActivationData.java +++ b/core/src/main/java/com/sekwah/advancedportals/core/warphandler/ActivationData.java @@ -10,18 +10,18 @@ import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; public class ActivationData { private boolean warpAllowed = true; - public final boolean moveActivated; - private WarpedStatus warpStatus = WarpedStatus.NOTACTIVATED; private PlayerLocation wantedLocation; + private TriggerType triggerType; + public WarpedStatus getWarped() { return this.warpStatus; } - public ActivationData(boolean moveActivated) { - this.moveActivated = moveActivated; + public ActivationData(TriggerType triggerType) { + this.triggerType = triggerType; } public void setWarpStatus(WarpedStatus warped) { @@ -33,6 +33,10 @@ public class ActivationData { this.warpStatus = warped; } + public TriggerType getTriggerType() { + return this.triggerType; + } + /** * In case you need to set the status back down a step for whatever reason. * However it is not recommended. diff --git a/core/src/main/java/com/sekwah/advancedportals/core/warphandler/TriggerType.java b/core/src/main/java/com/sekwah/advancedportals/core/warphandler/TriggerType.java new file mode 100644 index 00000000..bc35a9fa --- /dev/null +++ b/core/src/main/java/com/sekwah/advancedportals/core/warphandler/TriggerType.java @@ -0,0 +1,7 @@ +package com.sekwah.advancedportals.core.warphandler; + +public enum TriggerType { + MOVEMENT, + PORTAL, + MANUAL +} diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java index 50d9d822..5c4de33c 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotPlayerContainer.java @@ -6,7 +6,6 @@ import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.connector.containers.ServerContainer; import com.sekwah.advancedportals.core.serializeddata.BlockLocation; import com.sekwah.advancedportals.core.serializeddata.PlayerLocation; -import com.sekwah.advancedportals.core.tags.activation.CommandTag; import com.sekwah.advancedportals.spigot.AdvancedPortalsPlugin; import com.sekwah.advancedportals.spigot.reflection.MinecraftCustomPayload; import java.util.Arrays; @@ -14,11 +13,9 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.permissions.PermissionAttachment; /** * Just a temporary container for whenever advanced portals needs to get data diff --git a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotServerContainer.java b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotServerContainer.java index ea73c220..9c468242 100644 --- a/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotServerContainer.java +++ b/spigot/src/main/java/com/sekwah/advancedportals/spigot/connector/container/SpigotServerContainer.java @@ -3,13 +3,12 @@ package com.sekwah.advancedportals.spigot.connector.container; import com.sekwah.advancedportals.core.connector.containers.PlayerContainer; import com.sekwah.advancedportals.core.connector.containers.ServerContainer; import com.sekwah.advancedportals.core.connector.containers.WorldContainer; -import com.sekwah.advancedportals.core.tags.activation.CommandTag; +import com.sekwah.advancedportals.core.tags.CommandTag; import java.util.Arrays; import java.util.List; import java.util.UUID; import org.bukkit.Material; import org.bukkit.Server; -import org.bukkit.block.Block; import org.bukkit.entity.Player; import org.bukkit.permissions.PermissionAttachment;