Skip to content

Commit

Permalink
feat: add basic desti create and remove
Browse files Browse the repository at this point in the history
  • Loading branch information
sekwah41 committed Dec 5, 2023
1 parent c3e60fa commit 3c517a9
Show file tree
Hide file tree
Showing 23 changed files with 262 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.inject.Injector;
import com.sekwah.advancedportals.core.commands.CommandWithSubCommands;
import com.sekwah.advancedportals.core.commands.subcommands.desti.CreateDestiSubCommand;
import com.sekwah.advancedportals.core.commands.subcommands.desti.RemoveDestiSubCommand;
import com.sekwah.advancedportals.core.commands.subcommands.portal.*;
import com.sekwah.advancedportals.core.connector.commands.CommandRegister;
import com.sekwah.advancedportals.core.registry.TagRegistry;
Expand Down Expand Up @@ -45,6 +46,7 @@ public AdvancedPortalsCore(File dataStorageLoc, InfoLogger infoLogger) {
this.dataStorage = new DataStorage(dataStorageLoc);
this.infoLogger = infoLogger;
this.module = new AdvancedPortalsModule(this);

// Provide any items that need to be provided.
//this.module.addInstanceBinding(DataCollector.class, this.infoLogger);

Expand Down Expand Up @@ -96,7 +98,7 @@ private void registerPortalCommand(CommandRegister commandRegister) {
this.portalCommand.registerSubCommand("endportalblock", new EndPortalBlockSubCommand());
this.portalCommand.registerSubCommand("endgatewayblock", new EndGatewayBlockSubCommand());
this.portalCommand.registerSubCommand("create", new CreatePortalSubCommand());
this.portalCommand.registerSubCommand("remove", new RemoveSubCommand());
this.portalCommand.registerSubCommand("remove", new RemovePortalSubCommand());
this.portalCommand.registerSubCommand("list", new ListSubCommand());

commandRegister.registerCommand("portal", this.portalCommand);
Expand All @@ -105,6 +107,7 @@ private void registerPortalCommand(CommandRegister commandRegister) {
private void registerDestinationCommand(CommandRegister commandRegister) {
this.destiCommand = new CommandWithSubCommands(this);
this.destiCommand.registerSubCommand("create", new CreateDestiSubCommand());
this.destiCommand.registerSubCommand("remove", new RemoveDestiSubCommand());

commandRegister.registerCommand("destination", this.destiCommand);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,23 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.error.noname"));
return;
}
sender.sendMessage(Lang.centeredTitle(Lang.translate("command.createdesti.prep")));
sender.sendMessage("");
sender.sendMessage(Lang.translate("command.create.tags"));

if(destinationTags.isEmpty()) {
sender.sendMessage(Lang.translate("desti.info.noargs"));
}
else {
this.printTags(sender, destinationTags, Tag.TagType.DESTINATION);
}
sender.sendMessage("");
Destination destination = destinationServices.createDesti(player, player.getLoc(), destinationTags);
if(destination != null) {
sender.sendMessage("");
sender.sendMessage(Lang.translate("command.create.tags"));

ArrayList<DataTag> destiArgs = destination.getArgs();

if(destiArgs.isEmpty()) {
sender.sendMessage(Lang.translate("desti.info.noargs"));
}
else {
for (DataTag tag : destiArgs) {
if(tag.VALUES.length == 1) {
sender.sendMessage(" \u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]);
} else {
sender.sendMessage("\u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]);
}
}
}
sender.sendMessage("");
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.createdesti.complete"));
}
else {
sender.sendMessage("");
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.createdesti.error"));
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.sekwah.advancedportals.core.commands.subcommands.desti;

import com.google.inject.Inject;
import com.sekwah.advancedportals.core.commands.SubCommand;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.destination.Destination;
import com.sekwah.advancedportals.core.permissions.PortalPermissions;
import com.sekwah.advancedportals.core.portal.AdvancedPortal;
import com.sekwah.advancedportals.core.services.DestinationServices;
import com.sekwah.advancedportals.core.util.Lang;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public class RemoveDestiSubCommand implements SubCommand {


@Inject
DestinationServices destinationServices;

@Override
public void onCommand(CommandSenderContainer sender, String[] args) {
if(args.length > 1) {
if(destinationServices.removeDesti(args[1], sender.getPlayerContainer())) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.portal.remove.complete"));
}
else {
sender.sendMessage(Lang.translate("messageprefix.negative")
+ Lang.translate("command.destination.remove.error"));
}
}
else {
sender.sendMessage(Lang.translate("command.portal.remove.noname"));
}
}

@Override
public boolean hasPermission(CommandSenderContainer sender) {
return sender.isOp() || PortalPermissions.CREATE_PORTAL.hasPermission(sender);
}

@Override
public List<String> onTabComplete(CommandSenderContainer sender, String[] args) {
if(args.length > 2) {
return Collections.emptyList();
}
List<String> destiNames = destinationServices.getDestinations();
Collections.sort(destiNames);
return destiNames;
}

@Override
public String getBasicHelpText() {
return Lang.translate("command.create.help");
}

@Override
public String getDetailedHelpText() {
return Lang.translate("command.create.detailedhelp");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,7 @@ public void onCommand(CommandSenderContainer sender, String[] args) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.create.complete"));
sender.sendMessage(Lang.translate("command.create.tags"));
sender.sendMessage("\u00A7a" + "triggerBlock\u00A77:\u00A7e" + Arrays.toString(portal.getTriggerBlocks()));
for (DataTag tag: portal.getArgs()) {
if(tag.VALUES.length == 1) {
sender.sendMessage("\u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]);
} else {
// Output in the format tag.NAME(index): value
for (int i = 0; i < tag.VALUES.length; i++) {
sender.sendMessage("\u00A7a" + tag.NAME + "(" + i + ")\u00A77:\u00A7e" + tag.VALUES[i]);
}
}
}
this.printTags(sender, portal.getArgs(), Tag.TagType.PORTAL);
}
sender.sendMessage(Lang.translate("messageprefix.negative") + Lang.translate("command.create.error"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.sekwah.advancedportals.core.commands.subcommands.portal;

import com.google.inject.Inject;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.commands.SubCommand;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
Expand All @@ -15,7 +14,7 @@
import java.util.List;
import java.util.Map;

public class RemoveSubCommand implements SubCommand {
public class RemovePortalSubCommand implements SubCommand {


@Inject
Expand All @@ -25,25 +24,25 @@ public class RemoveSubCommand implements SubCommand {
public void onCommand(CommandSenderContainer sender, String[] args) {
if(args.length > 1) {
if(portalServices.removePortal(args[1], sender.getPlayerContainer())) {
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.remove.complete"));
sender.sendMessage(Lang.translate("messageprefix.positive") + Lang.translate("command.portal.remove.complete"));
}
else {
sender.sendMessage(Lang.translate("messageprefix.negative")
+ Lang.translate("command.remove.error"));
+ Lang.translate("command.portal.remove.error"));
}
}
else {
PlayerContainer player = sender.getPlayerContainer();
if(player == null) {
sender.sendMessage(Lang.translate("command.remove.noname"));
sender.sendMessage(Lang.translate("command.portal.remove.noname"));
}
else {
if(portalServices.removePlayerSelection(player)) {

}
else {
sender.sendMessage(Lang.translate("messageprefix.negative")
+ Lang.translate("command.remove.error"));
+ Lang.translate("command.portal.remove.error"));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ public List<String> onTabComplete(CommandSenderContainer sender, String[] args)
if(portalTag.NAME.equals(tag.getName())) {
return false;
}
// check the tag aliases
for (String alias : tag.getAliases()) {
if(portalTag.NAME.equals(alias)) {
return false;
var aliases = tag.getAliases();
if(aliases != null) {
for (String alias : aliases) {
if(portalTag.NAME.equals(alias)) {
return false;
}
}
}
}
Expand All @@ -99,4 +101,17 @@ public List<String> onTabComplete(CommandSenderContainer sender, String[] args)

return suggestions;
}

protected void printTags(CommandSenderContainer sender, List<DataTag> dataTags, Tag.TagType tagType) {
for (DataTag tag : dataTags) {
if(tag.VALUES.length == 1) {
sender.sendMessage(" \u00A7a" + tag.NAME + "\u00A77:\u00A7e" + tag.VALUES[0]);
} else {
for (int i = 0; i < tag.VALUES.length; i++) {
sender.sendMessage(" \u00A7a" + tag.NAME + "\u00A77[" + i + "]:\u00A7e" + tag.VALUES[i]);
}
}
}
// TODO add a note saying if tags were ignored due to not being valid for the type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class AdvancedPortalsModule extends AbstractModule {

private Injector injector;
private AdvancedPortalsCore advancedPortalsCore;
private DataStorage dataStorage;

private List<DelayedBinding> delayedBindings = new ArrayList<>();

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.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.commands.SubCommand;
import com.sekwah.advancedportals.core.util.InfoLogger;

Expand Down Expand Up @@ -37,12 +36,12 @@ public class SubCommandRegistry {
public boolean registerSubCommand(String arg, SubCommand subCommand) {

if (subCommand == null) {
this.infoLogger.logWarning("The subcommand '" + arg + "' cannot be null.");
this.infoLogger.warning("The subcommand '" + arg + "' cannot be null.");
return false;
}

if(this.subCommandMap.containsKey(arg)){
this.infoLogger.logWarning("The subcommand '" + arg + "' already exists.");
this.infoLogger.warning("The subcommand '" + arg + "' already exists.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public boolean registerTag(Tag tag) {

// Check literal tags for clashes
if(this.literalTags.contains(tagName)) {
this.portalsCore.getInfoLogger().logWarning("A tag with the name " + tagName + " already exists.");
this.portalsCore.getInfoLogger().warning("A tag with the name " + tagName + " already exists.");
return false;
}

Expand All @@ -74,7 +74,7 @@ public boolean registerTag(Tag tag) {
if(aliases != null) {
for (String alias : aliases) {
if(this.literalTags.contains(alias)) {
this.portalsCore.getInfoLogger().logWarning("A tag with the alias " + alias + " already exists.");
this.portalsCore.getInfoLogger().warning("A tag with the alias " + alias + " already exists.");
return false;
}
}
Expand All @@ -83,7 +83,7 @@ public boolean registerTag(Tag tag) {
}

if (tagName == null) {
this.portalsCore.getInfoLogger().logWarning("A tag cannot be null.");
this.portalsCore.getInfoLogger().warning("A tag cannot be null.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public boolean registerEffect(String name, WarpEffect effect, WarpEffect.Type ty
list = this.visualEffects;
break;
default:
this.portalsCore.getInfoLogger().logWarning(type.toString()
this.portalsCore.getInfoLogger().warning(type.toString()
+ " effect type not recognised");
return false;
}
Expand All @@ -65,15 +65,15 @@ public WarpEffect getEffect(String name, WarpEffect.Type type){
list = this.visualEffects;
break;
default:
this.infoLogger.logWarning(type.toString()
this.infoLogger.warning(type.toString()
+ " effect type not recognised");
return null;
}
if(list.containsKey(name)) {
return list.get(name);
}
else{
this.infoLogger.logWarning("No effect of type:"
this.infoLogger.warning("No effect of type:"
+ type.toString() + " was registered with the name: " + name);
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;

import java.util.List;

public interface IJsonRepository<T> {
Gson gson = new Gson();

boolean save(String name, T t);

boolean containsKey(String name);
Expand All @@ -15,5 +17,5 @@ public interface IJsonRepository<T> {

T get(String name);

ImmutableMap<String, T> getAll();
List<String> listAll();
}
Loading

0 comments on commit 3c517a9

Please sign in to comment.