Skip to content

Commit

Permalink
feat: add the ability for tags to reject values
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Nov 18, 2024
1 parent 6629151 commit 3558629
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ private void registerDestinationCommand(CommandRegister commandRegister) {
this.destiCommand.registerSubCommand("remove", new RemoveDestiSubCommand());
this.destiCommand.registerSubCommand("list", new ListDestiSubCommand());
this.destiCommand.registerSubCommand("show", new ShowDestiSubCommand());
this.destiCommand.registerSubCommand("reload", new ReloadDestiSubCommand());

commandRegister.registerCommand("destination", this.destiCommand);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void registered() {
return;
}


}
/*sender.sendMessage("Debug");
if(sender.getPlayerContainer() != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.sekwah.advancedportals.core.registry;

public enum ErrorCode {
public enum CommandErrorCode {
INSUFFICIENT_ARGUMENTS(""),
NO_PERMISSION("");
;


ErrorCode(String message) {
CommandErrorCode(String message) {

}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.sekwah.advancedportals.core.registry;

public class CommandException {
private ErrorCode errorCode;
private CommandErrorCode errorCode;
private String message;

public CommandException(ErrorCode errorCode, String message) {
public CommandException(CommandErrorCode errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}

public ErrorCode getErrorCode() {
public CommandErrorCode getErrorCode() {
return errorCode;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sekwah.advancedportals.core.registry;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.warphandler.Tag;

Expand Down Expand Up @@ -53,7 +52,7 @@ public Tag.Creation getCreationHandler(String arg) {
* @param arg
* @return
*/
public Tag.TagStatus getTagStatusHandler(String arg) {
public Tag.TagStatus getStatusHandler(String arg) {
return this.statusTags.get(arg);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,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.util.InfoLogger;

import javax.inject.Singleton;
Expand Down Expand Up @@ -58,6 +59,11 @@ public List<Destination> getAll() {
List<String> allFiles = dataStorage.listAllFiles(fileLocation, false);
for (String fileName : allFiles) {
Destination destination = dataStorage.loadJson(Destination.class, fileLocation + fileName);
// Forces the name tag to be up-to-date on load
String[] name = destination.getArgValues(NameTag.TAG_NAME);
if(name != null && name.length > 0) {
destination.setArgValues(NameTag.TAG_NAME, new String[]{fileName.replace(".json", "")});
}
destinations.add(destination);
}
return destinations;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.sekwah.advancedportals.core.services;

public enum Creation {
SUCCESS,
NAME_IN_USE,
TAG_REJECTED
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import com.google.inject.Inject;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.registry.TagRegistry;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
import com.sekwah.advancedportals.core.serializeddata.PlayerLocation;
import com.sekwah.advancedportals.core.destination.Destination;
import com.sekwah.advancedportals.core.repository.IDestinationRepository;
import com.sekwah.advancedportals.core.util.Lang;
import com.sekwah.advancedportals.core.util.Response;
import com.sekwah.advancedportals.core.warphandler.Tag;

import javax.inject.Singleton;
import java.util.ArrayList;
Expand All @@ -22,22 +23,11 @@ public class DestinationServices {
@Inject
private IDestinationRepository destinationRepository;

private final Map<String, Destination> destinationCache = new HashMap<>();

public Response.Creation create(String name, Destination destination) {
if (!destinationRepository.containsKey(name)) {
destinationRepository.save(name, destination);
return Response.Creation.SUCCESS;
}
return Response.Creation.NAME_IN_USE;
}
@Inject
TagRegistry tagRegistry;

public Boolean delete(String name) {
if (!destinationRepository.containsKey(name)) {
destinationRepository.delete(name);
}
return false;
}
private final Map<String, Destination> destinationCache = new HashMap<>();

public List<String> getDestinationNames() {
return destinationRepository.getAllNames();
Expand All @@ -49,6 +39,7 @@ public List<Destination> getDestinations() {

public void loadDestinations() {
List<String> destinationNames = destinationRepository.getAllNames();
destinationCache.clear();
for (String name : destinationNames) {
Destination destination = destinationRepository.get(name);
destinationCache.put(name, destination);
Expand Down Expand Up @@ -80,17 +71,17 @@ public Destination createDesti(PlayerContainer player, PlayerLocation playerLoca
desti.setArgValues(portalTag);
}
for (DataTag destiTag : tags) {
// TODO sort tag handle registry
/*TagHandler.Creation<Destination> creation = AdvancedPortalsCore.getDestinationTagRegistry().getCreationHandler(destiTag.NAME);
Tag.Creation creation = tagRegistry.getCreationHandler(destiTag.NAME);
if(creation != null) {
creation.created(desti, player, destiTag.VALUE);
}*/
if(!creation.created(desti, player, destiTag.VALUES)) {
return null;
}
}
}
try {
if(this.destinationRepository.save(name, desti)) {
this.destinationCache.put(name, desti);
} else {
player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("desti.error.save"));
return null;
}
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.common.collect.ImmutableList;
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.repository.IDestinationRepository;
import com.sekwah.advancedportals.core.repository.IPortalRepository;
import com.sekwah.advancedportals.core.serializeddata.DataTag;
Expand All @@ -15,6 +16,7 @@
import javax.inject.Singleton;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Singleton
Expand All @@ -29,6 +31,8 @@ public class PortalServices {
@Inject
private PortalTempDataServices portalTempDataServices;

private final Map<String, AdvancedPortal> portalCache = new HashMap<>();

public void loadPortals() {

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class DestiTag implements Tag.Activation, Tag.AutoComplete {

public static String TAG_NAME = "name";
@Inject
DestinationServices destinationServices;

Expand All @@ -23,7 +24,7 @@ public TagType[] getTagTypes() {

@Override
public String getName() {
return "destination";
return TAG_NAME;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
* <p>
* Most tags shouldn't be like this unless they are to be paired with another tag.
*/
public class NameTag implements Tag.AutoComplete {
public class NameTag implements Tag.AutoComplete, Tag.Creation {

public static String TAG_NAME = "name";

private final TagType[] tagTypes = new TagType[]{ TagType.PORTAL, TagType.DESTINATION };

Expand All @@ -26,7 +28,7 @@ public TagType[] getTagTypes() {

@Override
public String getName() {
return "name";
return TAG_NAME;
}

@Override
Expand All @@ -43,4 +45,21 @@ public String description() {
public List<String> autoComplete(String argData) {
return null;
}

@Override
public boolean created(TagTarget target, PlayerContainer player, String[] argData) {
if (argData.length > 0) {
String name = argData[0];
if (name.contains(" ")) {
player.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("tag.name.error.nospaces"));
return false;
}
}
return true;
}

@Override
public void destroyed(TagTarget target, PlayerContainer player, String[] argData) {

}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ interface Creation extends Tag {
*
* @param player if null then created by the server or a plugin
* @param argData
* @return If the tag is valid or allowed creation
*/
void created(TagTarget target, PlayerContainer player, String[] argData);
boolean created(TagTarget target, PlayerContainer player, String[] argData);

/**
* Example if the player does not have access to remove the portal or destination.
Expand Down
1 change: 1 addition & 0 deletions lang/src/main/resources/lang/en_GB.lang
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,4 @@ items.interact.left=Left Click
items.interact.right=Right Click

tag.desti.description=Sets the destination of the portal
tag.name.error.nospaces= The name cannot contain spaces.

0 comments on commit 3558629

Please sign in to comment.