9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-31 12:46:30 +00:00

Fix gson loading/saving, fix region discover handling. Regions can now be discovered.

This commit is contained in:
SamB440
2020-01-02 15:12:26 +00:00
parent 52a4e348cb
commit 409f5ec1e4
8 changed files with 67 additions and 88 deletions

View File

@@ -7,14 +7,12 @@ import net.islandearth.languagy.language.LanguagyImplementation;
import net.islandearth.languagy.language.LanguagyPluginHook;
import net.islandearth.languagy.language.Translator;
import net.islandearth.rpgregions.api.RPGRegionsAPI;
import net.islandearth.rpgregions.gson.InterfaceAdapter;
import net.islandearth.rpgregions.gson.AbstractAdapter;
import net.islandearth.rpgregions.listener.ConnectionListener;
import net.islandearth.rpgregions.listener.MoveListener;
import net.islandearth.rpgregions.listener.RegionListener;
import net.islandearth.rpgregions.managers.RPGRegionsManagers;
import net.islandearth.rpgregions.rewards.DiscoveryReward;
import net.islandearth.rpgregions.rewards.ExperienceReward;
import net.islandearth.rpgregions.rewards.ItemReward;
import net.islandearth.rpgregions.translation.Translations;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@@ -23,8 +21,6 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, LanguagyPluginHook {
@@ -45,6 +41,7 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
this.createConfig();
this.generateLang();
this.managers = new RPGRegionsManagers(this);
this.registerListeners();
}
@Override
@@ -56,15 +53,10 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
// Save all region configs
managers.getRegionsCache().getConfiguredRegions().forEach((id, region) -> {
File file = new File(this.getDataFolder() + "/regions/" + id + ".json");
if (!file.exists()) {
try {
file.createNewFile();
Gson gson = this.getGson();
gson.toJson(region, new FileWriter(file));
} catch (IOException e) {
e.printStackTrace();
}
try {
region.save(this);
} catch (IOException e) {
e.printStackTrace();
}
});
@@ -127,8 +119,7 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
public Gson getGson() {
return new GsonBuilder()
.registerTypeAdapter(DiscoveryReward.class, new InterfaceAdapter<ItemReward>())
.registerTypeAdapter(DiscoveryReward.class, new InterfaceAdapter<ExperienceReward>())
.registerTypeAdapter(DiscoveryReward.class, new AbstractAdapter<DiscoveryReward>("net.islandearth.rpgregions.rewards."))
.setPrettyPrinting()
.serializeNulls().create();
}

View File

@@ -2,6 +2,7 @@ package net.islandearth.rpgregions.api.integrations;
import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.api.integrations.worldguard.WorldGuardIntegration;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.InvocationTargetException;
@@ -17,11 +18,16 @@ public enum IntegrationType {
}
public Optional<IntegrationManager> get() {
RPGRegions plugin = JavaPlugin.getPlugin(RPGRegions.class);
plugin.getLogger().info("Loading IntegrationManager implementation...");
IntegrationManager generatedClazz = null;
try {
generatedClazz = clazz.getConstructor(RPGRegions.class).newInstance(JavaPlugin.getPlugin(RPGRegions.class));
plugin.getLogger().info("Loaded IntegrationManager implementation " + clazz.getName() + ".");
} catch (InstantiationException | InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
plugin.getLogger().severe("Unable to load StorageManager (" + clazz.getName() + ")! Plugin will disable.");
e.printStackTrace();
Bukkit.getPluginManager().disablePlugin(plugin);
}
return Optional.ofNullable(generatedClazz);

View File

@@ -39,12 +39,7 @@ public class WorldGuardIntegration implements IntegrationManager {
int x = pme.getTo().getBlockX();
int y = pme.getTo().getBlockY();
int z = pme.getTo().getBlockZ();
int oldX = pme.getFrom().getBlockX();
int oldY = pme.getFrom().getBlockY();
int oldZ = pme.getFrom().getBlockZ();
Set<ProtectedRegion> prevRegions = this.getProtectedRegions(new Location(player.getWorld(), oldX, oldY, oldZ));
Set<ProtectedRegion> regions = this.getProtectedRegions(new Location(player.getWorld(), x, y, z));
if (prevRegions.equals(regions)) return;
List<String> stringRegions = new ArrayList<>();
regions.forEach(region -> stringRegions.add(region.getId()));

View File

@@ -0,0 +1,35 @@
package net.islandearth.rpgregions.gson;
import com.google.gson.*;
import java.lang.reflect.Type;
public class AbstractAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T> {
private final String thePackage;
public AbstractAdapter(String thePackage) {
this.thePackage = thePackage;
}
@Override
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject result = new JsonObject();
result.add("type", new JsonPrimitive(src.getClass().getSimpleName()));
result.add("properties", context.serialize(src, src.getClass()));
return result;
}
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String type = jsonObject.get("type").getAsString();
JsonElement element = jsonObject.get("properties");
try {
return context.deserialize(element, Class.forName(thePackage + type));
} catch (ClassNotFoundException cnfe) {
throw new JsonParseException("Unknown element type: " + type, cnfe);
}
}
}

View File

@@ -1,49 +0,0 @@
package net.islandearth.rpgregions.gson;
import com.google.gson.*;
import java.lang.reflect.Type;
public class InterfaceAdapter<T>
implements JsonSerializer<T>, JsonDeserializer<T> {
@Override
public final JsonElement serialize(final T object, final Type interfaceType, final JsonSerializationContext context) {
final JsonObject member = new JsonObject();
member.addProperty("type", object.getClass().getName());
member.add("data", context.serialize(object));
return member;
}
@Override
public final T deserialize(final JsonElement elem, final Type interfaceType, final JsonDeserializationContext context)
throws JsonParseException {
final JsonObject member = (JsonObject) elem;
final JsonElement typeString = get(member, "type");
final JsonElement data = get(member, "data");
final Type actualType = typeForName(typeString);
return context.deserialize(data, actualType);
}
private Type typeForName(final JsonElement typeElem) {
try {
return Class.forName(typeElem.getAsString());
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
private JsonElement get(final JsonObject wrapper, final String memberName) {
final JsonElement elem = wrapper.get(memberName);
if (elem == null) {
throw new JsonParseException(
"no '" + memberName + "' member found in json file.");
}
return elem;
}
}

View File

@@ -30,11 +30,10 @@ public class RegionListener implements Listener {
public void onEnter(RegionsEnterEvent ree) {
Player player = ree.getPlayer();
plugin.getManagers().getStorageManager().getAccount(player.getUniqueId()).thenAccept(account -> {
for (Discovery discovery : account.getDiscoveredRegions()) {
String region = discovery.getRegion();
for (String region : ree.getRegions()) {
boolean has = false;
for (String protectedRegion : ree.getRegions()) {
if (protectedRegion.equals(region)) {
for (Discovery discoveredRegion : account.getDiscoveredRegions()) {
if (discoveredRegion.getRegion().equals(region)) {
has = true;
break;
}

View File

@@ -1,6 +1,5 @@
package net.islandearth.rpgregions.managers;
import com.google.gson.Gson;
import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.api.integrations.IntegrationManager;
import net.islandearth.rpgregions.api.integrations.IntegrationType;
@@ -13,8 +12,8 @@ import net.islandearth.rpgregions.rewards.ExperienceReward;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
@@ -44,23 +43,20 @@ public class RPGRegionsManagers {
rewards.add(new ExperienceReward(10));
ConfiguredRegion configuredRegion = new ConfiguredRegion("docks-1", rewards);
regionsCache.addConfiguredRegion(configuredRegion);
File file1 = new File(plugin.getDataFolder() + "/regions/" + "docks-1" + ".json");
if (!file1.exists()) {
try {
file1.createNewFile();
Gson gson = plugin.getGson();
gson.toJson(configuredRegion, new FileWriter(file1));
} catch (IOException e) {
e.printStackTrace();
}
try {
configuredRegion.save(plugin);
} catch (IOException e) {
e.printStackTrace();
}
for (File file : folder.listFiles()) {
// Exclude non-json files
if (file.getName().endsWith(".json")) {
try {
ConfiguredRegion region = plugin.getGson().fromJson(new FileReader(file), ConfiguredRegion.class);
Reader reader = new FileReader(file);
ConfiguredRegion region = plugin.getGson().fromJson(reader, ConfiguredRegion.class);
regionsCache.addConfiguredRegion(region);
reader.close();
} catch (Exception e) {
plugin.getLogger().severe("Error loading region config " + file.getName() + ":");
e.printStackTrace();

View File

@@ -1,11 +1,13 @@
package net.islandearth.rpgregions.managers.data.region;
import com.google.gson.Gson;
import net.islandearth.rpgregions.RPGRegions;
import net.islandearth.rpgregions.rewards.DiscoveryReward;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
public class ConfiguredRegion {
@@ -35,7 +37,11 @@ public class ConfiguredRegion {
}
public void save(RPGRegions plugin) throws IOException {
File file = new File(plugin.getDataFolder() + "/regions/" + this.id + ".yml");
plugin.getGson().toJson(this, new FileWriter(file));
File file = new File(plugin.getDataFolder() + "/regions/" + this.id + ".json");
Writer writer = new FileWriter(file);
Gson gson = plugin.getGson();
gson.toJson(this, writer);
writer.flush();
writer.close();
}
}