Skip to content

Commit

Permalink
feat: added tag priority sorting and delay tag
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent 7635a13 commit 0044008
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ private void registerTags() {
this.tagRegistry.registerTag(new TriggerBlockTag());
this.tagRegistry.registerTag(new PermissionTag());
this.tagRegistry.registerTag(new CommandTag());
this.tagRegistry.registerTag(new PortalEventTag());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public boolean playerPortalEvent(PlayerContainer player, PlayerLocation toLoc) {
return false;
}

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

if(portalResult != PortalServices.PortalActivationResult.NOT_IN_PORTAL) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class AdvancedPortal implements TagTarget {

private final HashMap<String, String[]> args = new HashMap<>();

private transient List<DataTag> portalTags = new ArrayList<>();

private transient boolean isSorted = false;

@Inject
private transient PlayerDataServices playerDataServices;

Expand Down Expand Up @@ -64,6 +68,7 @@ public String[] getArgValues(String argName) {

@Override
public void setArgValues(String argName, String[] argValues) {
this.isSorted = false;
this.args.put(argName, argValues);
}

Expand All @@ -72,8 +77,26 @@ public void addArg(String argName, String argValues) {
// TODO need to add the ability to add args after creation
}

private void updatePortalTagList() {
portalTags.clear();
int i = 0;
for (Map.Entry<String, String[]> entry : args.entrySet()) {
this.portalTags.add(new DataTag(entry.getKey(), entry.getValue()));
}
// sort the tags by priority
this.portalTags.sort(Comparator.comparingInt(o -> {
var tag = tagRegistry.getTag(o.NAME);
if (tag instanceof Tag.OrderPriority tagPriority) {
return tagPriority.getPriority().ordinal();
} else {
return Tag.Priority.NORMAL.ordinal();
}
}));
}

@Override
public void removeArg(String arg) {
this.isSorted = false;
this.args.remove(arg);
}

Expand Down Expand Up @@ -113,6 +136,11 @@ public void updateBounds(BlockLocation loc1, BlockLocation loc2) {
* @return Whether the portal was successfully activated
*/
public ActivationResult activate(PlayerContainer player, TriggerType triggerType) {
if(!isSorted) {
updatePortalTagList();
isSorted = true;
}

var playerData = playerDataServices.getPlayerData(player);

if (playerData.hasJoinCooldown()) {
Expand All @@ -131,22 +159,23 @@ public ActivationResult activate(PlayerContainer player, TriggerType triggerType
}

ActivationData data = new ActivationData(triggerType);
DataTag[] portalTags = new DataTag[args.size()];
int i = 0;
for (Map.Entry<String, String[]> entry : args.entrySet()) {
portalTags[i++] = new DataTag(entry.getKey(), entry.getValue());
}

for (DataTag portalTag : portalTags) {
for (DataTag portalTag : this.portalTags) {
Tag.Activation activationHandler =
tagRegistry.getActivationHandler(portalTag.NAME);
if (activationHandler != null
&& !activationHandler.preActivated(
this, player, data, this.getArgValues(portalTag.NAME))) {
return ActivationResult.FAILED_DO_KNOCKBACK;
if (activationHandler != null) {
var preActivated = activationHandler.preActivated(
this, player, data, this.getArgValues(portalTag.NAME));

if(!preActivated) {
if(activationHandler instanceof Tag.DenyBehavior denyBehavior && denyBehavior.getDenyBehavior() == Tag.DenyBehavior.Behaviour.SILENT) {
return ActivationResult.FAILED_DO_NOTHING;
}
return ActivationResult.FAILED_DO_KNOCKBACK;
}
}
}
for (DataTag portalTag : portalTags) {
for (DataTag portalTag : this.portalTags) {
Tag.Activation activationHandler =
tagRegistry.getActivationHandler(portalTag.NAME);
if (activationHandler != null
Expand All @@ -155,7 +184,7 @@ public ActivationResult activate(PlayerContainer player, TriggerType triggerType
return ActivationResult.FAILED_DO_KNOCKBACK;
}
}
for (DataTag portalTag : portalTags) {
for (DataTag portalTag : this.portalTags) {
Tag.Activation activationHandler =
tagRegistry.getActivationHandler(portalTag.NAME);
if (activationHandler != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,16 @@ public boolean registerTag(Tag tag) {
return true;
}

public List<Tag> getTags() {
// TODO Make a copy of the list to prevent issues with modification
public Tag getTag(String tagName) {
for (Tag tag : this.tags) {
if (tag.getName().equals(tagName)) {
return tag;
}
}
return null;
}

return this.tags;
public List<Tag> getTags() {
return new ArrayList<>(this.tags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
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 javax.annotation.Nullable;
import javax.inject.Inject;
import java.util.List;
import java.util.Objects;

public class PortalEventTag implements Tag.Activation {
public class PortalEventTag implements Tag.Activation, Tag.AutoComplete, Tag.DenyBehavior, Tag.OrderPriority {
@Inject
PlayerDataServices playerDataServices;

Expand Down Expand Up @@ -40,7 +44,7 @@ public String getName() {
@Nullable
@Override
public String[] getAliases() {
return new String[0];
return aliases;
}

@Override
Expand All @@ -51,21 +55,40 @@ public String description() {
@Override
public boolean preActivated(TagTarget target, PlayerContainer player,
ActivationData activeData, String[] argData) {
if (!player.hasPermission(argData[1])) {
player.sendMessage(Lang.translate("portal.error.nopermission"));
return false;
}
return true;
return !Objects.equals(argData[0], "true") || activeData.getTriggerType() == TriggerType.PORTAL;
}

@Override
public void postActivated(TagTarget target, PlayerContainer player,
ActivationData activationData, String[] argData) {
// Do nothing
}

@Override
public boolean activated(TagTarget target, PlayerContainer player,
ActivationData activationData, String[] argData) {
return true;
}

@Nullable
@Override
public List<String> autoComplete(String argData) {
return List.of("true", "false");
}

@Nullable
@Override
public String splitString() {
return "";
}

@Override
public Behaviour getDenyBehavior() {
return Behaviour.SILENT;
}

@Override
public Priority getPriority() {
return Priority.HIGHEST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public interface Tag {
*/
enum TagType { PORTAL, DESTINATION }

enum Priority { LOWEST, LOW, NORMAL, HIGH, HIGHEST }

/**
* Used to flag where the auto complete should show more or less info.
*
Expand All @@ -43,6 +45,16 @@ enum TagType { PORTAL, DESTINATION }

String description();

interface OrderPriority {
Priority getPriority();
}

interface DenyBehavior {
enum Behaviour { SILENT, KNOCKBACK }

Behaviour getDenyBehavior();
}

interface AutoComplete extends Tag {
/**
* This is used to get the auto complete for the tag. This is called
Expand Down

0 comments on commit 0044008

Please sign in to comment.