feat: add cooldown tag that is stored

This commit is contained in:
Sekwah 2024-02-05 02:17:44 +00:00
parent 9448fd76bf
commit c9d98b39c3
13 changed files with 189 additions and 27 deletions

View File

@ -15,6 +15,7 @@ import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.services.DestinationServices;
import com.sekwah.advancedportals.core.services.PortalServices;
import com.sekwah.advancedportals.core.services.PlayerDataServices;
import com.sekwah.advancedportals.core.tags.activation.CooldownTag;
import com.sekwah.advancedportals.core.tags.activation.DestiTag;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.tags.activation.TriggerBlockTag;
@ -118,6 +119,7 @@ public class AdvancedPortalsCore {
private void registerTags() {
this.tagRegistry.registerTag(new NameTag());
this.tagRegistry.registerTag(new DestiTag());
this.tagRegistry.registerTag(new CooldownTag());
this.tagRegistry.registerTag(new TriggerBlockTag());
}

View File

@ -32,11 +32,11 @@ public class CoreListeners {
private GameScheduler gameScheduler;
public void playerJoin(PlayerContainer player) {
this.playerDataServices.activateCooldown(player);
this.playerDataServices.setJoinCooldown(player);
}
public void teleportEvent(PlayerContainer player) {
this.playerDataServices.activateCooldown(player);
this.playerDataServices.setJoinCooldown(player);
}
public void playerLeave(PlayerContainer player) {
@ -157,7 +157,7 @@ public class CoreListeners {
}
public void worldChange(PlayerContainer player) {
this.playerDataServices.activateCooldown(player);
this.playerDataServices.setJoinCooldown(player);
}
public boolean preventEntityCombust(EntityContainer entity) {

View File

@ -65,7 +65,7 @@ public class Destination implements TagTarget {
}
public boolean activate(PlayerContainer player) {
ActivationData data = new ActivationData();
ActivationData data = new ActivationData(false);
this.portalActivate(player, data);
this.postActivate(player, data);
return true;

View File

@ -11,6 +11,8 @@ 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.util.Lang;
import com.sekwah.advancedportals.core.util.PlayerUtils;
import com.sekwah.advancedportals.core.warphandler.ActivationData;
import com.sekwah.advancedportals.core.warphandler.Tag;
@ -107,15 +109,19 @@ public class AdvancedPortal implements TagTarget {
*/
public boolean activate(PlayerContainer player, boolean moveActivated) {
var playerData = playerDataServices.getPlayerData(player);
if(playerData.isGlobalCooldown()) {
if(playerData.isInPortal()) return false;
playerData.setInPortal(true);
if(playerData.hasJoinCooldown()) {
var cooldown = (int) Math.ceil(playerData.getJoinCooldownLeft() / 1000D);
player.sendMessage(Lang.translateInsertVariables("portal.cooldown.join", 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);
}
if(moveActivated) throwPlayerBack(player);
return false;
}
ActivationData data = new ActivationData();
ActivationData data = new ActivationData(moveActivated);
DataTag[] portalTags = new DataTag[args.size()];
int i = 0;
for(Map.Entry<String, String[]> entry : args.entrySet()) {
@ -146,18 +152,11 @@ public class AdvancedPortal implements TagTarget {
}
if(data.hasActivated()) {
playerData.setNetherPortalCooldown(1000);
playerData.setGlobalCooldown(configRepository.getPortalCooldown() * 1000);
return true;
}
return false;
}
private void throwPlayerBack(PlayerContainer player) {
var strength = configRepository.getThrowbackStrength();
var playerLoc = player.getLoc().getDirection();
player.setVelocity(playerLoc.setY(0).normalize().multiply(-1).setY(0.5).multiply(strength));
}
public boolean isLocationInPortal(BlockLocation loc) {
return this.isLocationInPortal(loc, 0);
@ -209,4 +208,8 @@ public class AdvancedPortal implements TagTarget {
}
return false;
}
public String getName() {
return getArgValues("name")[0];
}
}

View File

@ -79,7 +79,7 @@ public class ConfigRepositoryImpl implements ConfigRepository {
@Override
public long getPortalCooldown() {
return this.config.portalCooldown;
return this.config.joinCooldown;
}
@Override

View File

@ -39,11 +39,11 @@ public class PlayerData {
/**
* The next time System.currentTimeMillis() a player can use a portal.
*/
private transient long globalCooldown;
private transient long joinCooldown;
private transient long netherPortalCooldown;
private HashMap<String, String> perPortalCooldowns = new HashMap<>();
private HashMap<String, Long> perPortalCooldowns = new HashMap<>();
private String selectedPortal;
@ -63,12 +63,12 @@ public class PlayerData {
this.pos2 = pos2;
}
public long getGlobalCooldown() {
return globalCooldown;
public long getJoinCooldownLeft() {
return joinCooldown - System.currentTimeMillis();
}
public void setGlobalCooldown(long globalCooldown) {
this.globalCooldown = System.currentTimeMillis() + globalCooldown;
public void setJoinCooldown(long joinCooldown) {
this.joinCooldown = System.currentTimeMillis() + joinCooldown;
}
public String getSelectedPortal() {
@ -107,11 +107,23 @@ public class PlayerData {
this.netherPortalCooldown = System.currentTimeMillis() + netherPortalCooldown;
}
public boolean isGlobalCooldown() {
return System.currentTimeMillis() < globalCooldown;
public boolean hasJoinCooldown() {
return System.currentTimeMillis() < joinCooldown;
}
public boolean isNetherPortalCooldown() {
return System.currentTimeMillis() < netherPortalCooldown;
}
public void setPortalCooldown(String portalName, long cooldown) {
perPortalCooldowns.put(portalName, System.currentTimeMillis() + cooldown);
}
public boolean hasPortalCooldown(String portalName) {
return perPortalCooldowns.containsKey(portalName) && System.currentTimeMillis() < perPortalCooldowns.get(portalName);
}
public double getPortalCooldownLeft(String portalName) {
return perPortalCooldowns.get(portalName) - System.currentTimeMillis();
}
}

View File

@ -17,7 +17,7 @@ public class Config {
public boolean stopWaterFlow = true;
public int portalCooldown = 5;
public int joinCooldown = 5;
public String warpParticles = "ENDER";
@ -31,7 +31,7 @@ public class Config {
public int maxTriggerVisualisationSize = 1000;
public double throwbackStrength = 1;
public double throwbackStrength = 0.7;
public boolean playFailSound = true;
}

View File

@ -39,9 +39,9 @@ public final class PlayerDataServices {
});
}
public void activateCooldown(PlayerContainer player) {
public void setJoinCooldown(PlayerContainer player) {
var tempData = getPlayerData(player);
tempData.setGlobalCooldown(configRepository.getPortalCooldown() * 1000);
tempData.setJoinCooldown(configRepository.getPortalCooldown() * 1000);
}
public void playerLeave(PlayerContainer player) {

View File

@ -12,6 +12,7 @@ import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.serializeddata.PlayerData;
import com.sekwah.advancedportals.core.tags.activation.NameTag;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.util.PlayerUtils;
import com.sekwah.advancedportals.core.warphandler.Tag;
import javax.inject.Singleton;
@ -80,16 +81,26 @@ public class PortalServices {
var blockMaterial = world.getBlock(blockLoc);
var blockEntityTopMaterial = world.getBlock(blockEntityTopLoc);
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 playerData = playerDataServices.getPlayerData(player);
if(!notInPortal) {
var strength = configRepository.getThrowbackStrength();
PlayerUtils.throwPlayerBack(player, strength);
}
if(playerData.isInPortal() && notInPortal) {
playerData.setInPortal(false);
}
}
public List<String> getPortalNames() {

View File

@ -0,0 +1,111 @@
package com.sekwah.advancedportals.core.tags.activation;
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 javax.annotation.Nullable;
import java.util.List;
import java.util.Random;
public class CooldownTag implements Tag.Activation, Tag.Creation {
@Inject
transient PlayerDataServices playerDataServices;
@Inject
transient 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) {
}
}

View File

@ -0,0 +1,11 @@
package com.sekwah.advancedportals.core.util;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
public class PlayerUtils {
public static void throwPlayerBack(PlayerContainer player, double strength) {
var playerLoc = player.getLoc().getDirection();
player.setVelocity(playerLoc.setY(0).normalize().multiply(-1).setY(0.5).multiply(strength));
}
}

View File

@ -12,6 +12,8 @@ public class ActivationData {
private boolean warpAllowed = true;
public final boolean moveActivated;
private WarpedStatus warpStatus = WarpedStatus.NOTACTIVATED;
private PlayerLocation wantedLocation;
@ -20,6 +22,10 @@ public class ActivationData {
return this.warpStatus;
}
public ActivationData(boolean moveActivated) {
this.moveActivated = moveActivated;
}
public void setWarpStatus(WarpedStatus warped) {
if (this.warpStatus == WarpedStatus.WARPED) {
return;

View File

@ -144,6 +144,11 @@ error.notplayer=Only players can do that.
portal.selector.poschange=&aYou have selected &epos%1$s &aX&7:&e%2$s &aY&7:&e%3$s &aZ&7:&e%4$s
time.second=second
time.seconds=seconds
portal.cooldown.join=&cThere is &e%1$s &c%2$s join cooldown protection left.
portal.cooldown.individual=&cPlease wait &e%1$s &c%2$s until attempting to enter this portal again.
command.lang.help=Update the translation file
command.version.help=Returns the current version of the plugin
@ -159,4 +164,5 @@ tag.desti.description=Sets the destination of the portal
tag.name.error.nospaces= The name cannot contain spaces.
tag.triggerblock.description=Sets the trigger block/s of the portal. Comma seperated or multi tag.
tag.cooldown.fail= The cooldown must be a number.