Advanced-Portals/core/src/main/java/com/sekwah/advancedportals/services/DestinationServices.java

106 lines
3.6 KiB
Java
Raw Normal View History

package com.sekwah.advancedportals.services;
import com.google.common.collect.ImmutableMap;
2018-02-03 21:31:50 +01:00
import com.google.gson.reflect.TypeToken;
2020-06-29 01:39:27 +02:00
import com.sekwah.advancedportals.core.data.DataTag;
import com.sekwah.advancedportals.core.data.PlayerLocation;
import com.sekwah.advancedportals.core.api.destination.Destination;
import com.sekwah.advancedportals.core.api.warphandler.TagHandler;
2018-01-24 11:05:46 +01:00
import com.sekwah.advancedportals.core.AdvancedPortalsCore;
import com.sekwah.advancedportals.core.util.Lang;
2020-06-29 01:39:27 +02:00
import com.sekwah.advancedportals.core.connector.container.PlayerContainer;
2020-06-29 02:05:14 +02:00
import com.sekwah.advancedportals.repository.DestinationRepository;
2018-01-24 11:05:46 +01:00
import javax.inject.Inject;
import java.io.IOException;
2018-02-03 21:31:50 +01:00
import java.lang.reflect.Type;
import java.util.ArrayList;
2018-01-24 11:05:46 +01:00
import java.util.HashMap;
/**
* Handles logic for all destination, this is a transient layer so it should
* not store any information.
*/
public class DestinationServices {
2020-06-29 02:05:14 +02:00
DestinationRepository destinationRepository;
2018-01-24 11:05:46 +01:00
@Inject
2020-06-29 02:05:14 +02:00
private DestinationServices(DestinationRepository destinationRepository) {
this.destinationRepository = destinationRepository;
}
2020-06-29 01:59:47 +02:00
public Response.Creation create(String name, Destination destination) {
if (!destinationRepository.containsKey(name)) {
destinationRepository.save(name, destination);
2020-06-29 01:59:47 +02:00
return Response.Creation.SUCCESS;
}
2020-06-29 01:59:47 +02:00
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.get("");
}
2018-01-24 11:05:46 +01:00
public ImmutableMap<String, Destination> getDestinations() {
return ImmutableMap.copyOf(destinationRepository.get(""));
2018-01-24 11:05:46 +01:00
}
2018-02-03 21:31:50 +01:00
public Destination createDesti(String name, PlayerContainer player, PlayerLocation playerLocation, ArrayList<DataTag> tags) {
// TODO change to write messages
2018-02-21 18:03:20 +01:00
if(name == null || name.equals("")) {
player.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translate("desti.error.noname"));
return null;
2018-02-21 18:03:20 +01:00
}
else if(this.destinationRepository.containsKey(name)) {
player.sendMessage(Lang.translateColor("messageprefix.positive") + Lang.translate("desti.error.takenname"));
return null;
2018-02-21 18:03:20 +01:00
}
2018-02-03 21:31:50 +01:00
2018-02-21 18:03:20 +01:00
Destination desti = new Destination(playerLocation);
for(DataTag portalTag : tags) {
desti.setArg(portalTag);
}
for(DataTag destiTag : tags) {
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();
2018-02-22 04:15:57 +01:00
return desti;
2018-02-03 21:31:50 +01:00
}
//TODO Change to repository
public void loadDestinations() {
2018-02-03 21:31:50 +01:00
Type type = new TypeToken<HashMap<String, Destination>>() {
}.getType();
//this.destiHashMap = this.portalsCore.getDataStorage().loadJson(type, "destinations.json");
this.saveDestinations();
2018-02-22 04:06:12 +01:00
}
public void saveDestinations() {
/*if (this.destiHashMap == null) {
2018-02-03 21:31:50 +01:00
this.destiHashMap = new HashMap<>();
}
this.portalsCore.getDataStorage().storeJson(this.destiHashMap, "destinations.json");*/
2018-02-03 21:31:50 +01:00
}
}