feat: add destination and portal services

This commit is contained in:
Sekwah 2023-04-07 02:37:59 +01:00
parent 4f78ef1578
commit 423aa6879d
No known key found for this signature in database
GPG Key ID: 9E0D654FC942286D
11 changed files with 213 additions and 17 deletions

View File

@ -74,7 +74,7 @@ public class AdvancedPortalsCore {
}
private void registerPortalCommand(CommandRegister commandRegister) {
this.portalCommand = new CommandWithSubCommands();
this.portalCommand = new CommandWithSubCommands(this);
// TODO remove once annotations are done
this.portalCommand.registerSubCommand("version", new VersionSubCommand());
@ -91,7 +91,7 @@ public class AdvancedPortalsCore {
}
private void registerDestinationCommand(CommandRegister commandRegister) {
this.destiCommand = new CommandWithSubCommands();
this.destiCommand = new CommandWithSubCommands(this);
// TODO remove once annotations are done
this.destiCommand.registerSubCommand("create", new CreateDestiSubCommand());

View File

@ -1,5 +1,6 @@
package com.sekwah.advancedportals.core.commands;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.connector.containers.CommandSenderContainer;
import com.sekwah.advancedportals.core.registry.SubCommandRegistry;
import com.sekwah.advancedportals.core.util.Lang;
@ -13,12 +14,15 @@ public class CommandWithSubCommands implements CommandTemplate {
private final SubCommandRegistry subCommandRegistry;
private final int subCommandsPerPage = 7;
private final AdvancedPortalsCore pluginCore;
public CommandWithSubCommands() {
public CommandWithSubCommands(AdvancedPortalsCore advancedPortalsCore) {
this.subCommandRegistry = new SubCommandRegistry();
this.pluginCore = advancedPortalsCore;
}
public boolean registerSubCommand(String arg, SubCommand subCommand, String... aliasArgs) {
pluginCore.getModule().getInjector().injectMembers(subCommand);
boolean hasRegistered = false;
for(String additionalArg : aliasArgs) {
hasRegistered = hasRegistered || this.subCommandRegistry.registerSubCommand(additionalArg,subCommand);

View File

@ -6,7 +6,7 @@ import com.sekwah.advancedportals.core.config.Config;
import com.sekwah.advancedportals.core.config.ConfigProvider;
import com.sekwah.advancedportals.core.data.DataStorage;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import com.sekwah.advancedportals.core.repository.ConfigRepositoryImpl;
import com.sekwah.advancedportals.core.repository.impl.ConfigRepositoryImpl;
import com.sekwah.advancedportals.core.util.InfoLogger;
import javax.annotation.Nonnull;

View File

@ -1,4 +1,9 @@
package com.sekwah.advancedportals.core.repository;
public interface IDestinationRepository<T> extends IJsonRepository<T> {
import com.sekwah.advancedportals.core.destination.Destination;
import java.io.IOException;
public interface IDestinationRepository extends IJsonRepository<Destination> {
void addDestination(String name, Destination desti) throws IOException;
}

View File

@ -1,5 +1,6 @@
package com.sekwah.advancedportals.core.repository;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
public interface IJsonRepository<T> {
@ -11,4 +12,8 @@ public interface IJsonRepository<T> {
boolean delete(String name);
boolean update(String name, T t);
T get(String name);
ImmutableMap<String, T> getAll();
}

View File

@ -1,8 +1,9 @@
package com.sekwah.advancedportals.core.repository;
package com.sekwah.advancedportals.core.repository.impl;
import com.google.inject.Singleton;
import com.sekwah.advancedportals.core.config.Config;
import com.sekwah.advancedportals.core.data.DataStorage;
import com.sekwah.advancedportals.core.repository.ConfigRepository;
import java.util.HashMap;

View File

@ -1,7 +1,8 @@
package com.sekwah.advancedportals.core.repository;
package com.sekwah.advancedportals.core.repository.impl;
import com.google.common.collect.ImmutableMap;
import com.sekwah.advancedportals.core.destination.Destination;
import com.sekwah.advancedportals.core.repository.IDestinationRepository;
import javax.inject.Singleton;
import java.io.FileWriter;
@ -12,21 +13,16 @@ import java.util.HashMap;
import java.util.Map;
@Singleton
public class DestinationRepository implements IDestinationRepository<Destination> {
public class DestinationRepositoryImpl implements IDestinationRepository {
private final String fileLocation = "";
private Map<String, Destination> destinationCache = new HashMap<String, Destination>();
/*Is there any reason to load it into the array if it's not been used or connected? Q for Sekwah*/
public void AddDestination(String name, Destination destination) throws IOException {
public void addDestination(String name, Destination destination) throws IOException {
gson.toJson(destination, new FileWriter(fileLocation + name + ".json"));
}
private void test() {
destinationCache.get("");
}
@Override
public boolean save(String name, Destination destination) {
return false;
@ -51,7 +47,11 @@ public class DestinationRepository implements IDestinationRepository<Destination
return false;
}
public ImmutableMap<String, Destination> get(String s) {
public Destination get(String s) {
return null;
}
public ImmutableMap<String, Destination> getAll() {
return null;
}
}

View File

@ -1,12 +1,14 @@
package com.sekwah.advancedportals.core.repository;
package com.sekwah.advancedportals.core.repository.impl;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Singleton;
import com.sekwah.advancedportals.core.data.WorldLocation;
import com.sekwah.advancedportals.core.repository.IPortalRepository;
import java.util.UUID;
@Singleton
public class PortalRepository implements IPortalRepository {
public class PortalRepositoryImpl implements IPortalRepository {
public String getSelectedPortal(UUID uuid) {
return null;
@ -31,4 +33,14 @@ public class PortalRepository implements IPortalRepository {
public boolean update(String name, WorldLocation portalLocation) {
return false;
}
@Override
public WorldLocation get(String name) {
return null;
}
@Override
public ImmutableMap<String, WorldLocation> getAll() {
return null;
}
}

View File

@ -0,0 +1,107 @@
package com.sekwah.advancedportals.core.services;
import com.google.common.collect.ImmutableMap;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.data.DataTag;
import com.sekwah.advancedportals.core.data.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.warphandler.TagHandler;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Handles logic for all destination, this is a transient layer so it should
* not store any information.
*/
public class DestinationServices {
private final IDestinationRepository destinationRepository;
@Inject
private DestinationServices(IDestinationRepository destinationRepository) {
this.destinationRepository = destinationRepository;
}
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;
}
public Boolean delete(String name) {
if (!destinationRepository.containsKey(name)) {
destinationRepository.delete(name);
}
return false;
}
public ImmutableMap<String, Destination> getDestination() {
return destinationRepository.getAll();
}
public ImmutableMap<String, Destination> getDestinations() {
return ImmutableMap.copyOf(destinationRepository.getAll());
}
public Destination createDesti(String name, PlayerContainer player, PlayerLocation playerLocation, ArrayList<DataTag> tags) {
// TODO change to write messages
if(name == null || name.equals("")) {
player.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translate("desti.error.noname"));
return null;
}
else if(this.destinationRepository.containsKey(name)) {
player.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translate("desti.error.takenname"));
return null;
}
Destination desti = new Destination(playerLocation);
for(DataTag portalTag : tags) {
desti.setArg(portalTag);
}
for(DataTag destiTag : tags) {
// TODO sort tag handle registry
/*TagHandler.Creation<Destination> creation = AdvancedPortalsCore.getDestinationTagRegistry().getCreationHandler(destiTag.NAME);
if(creation != null) {
creation.created(desti, player, destiTag.VALUE);
}*/
}
try {
this.destinationRepository.addDestination(name, desti);
} catch (IOException e) {
e.printStackTrace();
}
this.saveDestinations();
return desti;
}
//TODO Change to repository
public void loadDestinations() {
Type type = new TypeToken<HashMap<String, Destination>>() {
}.getType();
//this.destiHashMap = this.portalsCore.getDataStorage().loadJson(type, "destinations.json");
this.saveDestinations();
}
public void saveDestinations() {
/*if (this.destiHashMap == null) {
this.destiHashMap = new HashMap<>();
}
this.portalsCore.getDataStorage().storeJson(this.destiHashMap, "destinations.json");*/
}
}

View File

@ -0,0 +1,52 @@
package com.sekwah.advancedportals.core.services;
import com.google.inject.Inject;
import com.sekwah.advancedportals.core.connector.containers.PlayerContainer;
import com.sekwah.advancedportals.core.data.BlockLocation;
import com.sekwah.advancedportals.core.data.PlayerLocation;
import com.sekwah.advancedportals.core.repository.IPortalRepository;
import java.util.UUID;
public class PortalServices {
private final IPortalRepository portalRepository;
@Inject
public PortalServices(IPortalRepository portalRepository) {
this.portalRepository = portalRepository;
}
public void addSelectedPortal(UUID selectedPlayer, String portal) {
//portalRepository.save(selectedPlayer, portal);
}
public void removeSelectedPortal(UUID uuid) {
//selectedPortal.invalidate(uuid);
}
public void addSelectedPosition(UUID uuid, boolean isPos1, BlockLocation portalLocation) {
//selectedPositions.put(uuid, isPos1, portalLocation);
}
public void removeSelectedPosition(UUID uuid, boolean isPos1) {
//selectedPositions.remove(uuid, isPos1);
}
public void removeAllSelectedHand(UUID uuid) {
//selectedPositions.remove(uuid, true);
//selectedPositions.remove(uuid, false);
}
public void activateCooldown(PlayerContainer player) {
}
public void playerLeave(PlayerContainer player) {
}
public boolean inPortalRegion(PlayerLocation loc) {
return false;
}
}

View File

@ -0,0 +1,10 @@
package com.sekwah.advancedportals.core.services;
public class Response {
public enum Creation {
SUCCESS,
NAME_IN_USE,
}
}