9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2026-01-06 15:41:35 +00:00

Fix language file generation, add configurable region effects

This commit is contained in:
SamB440
2020-02-10 20:48:36 +00:00
parent 393c21d2e8
commit 9b3fac107a
6 changed files with 66 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="RPGRegions" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="net.islandearth" external.system.module.version="1.0.5" type="JAVA_MODULE" version="4">
<component name="ExternalSystem" externalSystem="GRADLE" externalSystemModuleGroup="net.islandearth" externalSystemModuleVersion="1.0.9" linkedProjectId="RPGRegions" linkedProjectPath="$MODULE_DIR$" rootProjectPath="$MODULE_DIR$" />
<component name="ExternalSystem" externalSystem="GRADLE" externalSystemModuleGroup="net.islandearth" externalSystemModuleVersion="1.1.0" linkedProjectId="RPGRegions" linkedProjectPath="$MODULE_DIR$" rootProjectPath="$MODULE_DIR$" />
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">

View File

@@ -1,2 +1,2 @@
pluginGroup=net.islandearth
pluginVersion=1.0.9
pluginVersion=1.1.0

View File

@@ -12,6 +12,7 @@ import net.islandearth.rpgregions.commands.RPGRegionsCommand;
import net.islandearth.rpgregions.effects.RegionEffect;
import net.islandearth.rpgregions.gson.AbstractAdapter;
import net.islandearth.rpgregions.gson.ItemStackAdapter;
import net.islandearth.rpgregions.gson.PotionEffectAdapter;
import net.islandearth.rpgregions.listener.ConnectionListener;
import net.islandearth.rpgregions.listener.MoveListener;
import net.islandearth.rpgregions.listener.RegionListener;
@@ -26,6 +27,7 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import java.io.IOException;
@@ -140,6 +142,7 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
return new GsonBuilder()
.registerTypeAdapter(DiscoveryReward.class, new AbstractAdapter<DiscoveryReward>(null))
.registerTypeAdapter(RegionEffect.class, new AbstractAdapter<RegionEffect>(null))
.registerTypeHierarchyAdapter(PotionEffect.class, new PotionEffectAdapter())
.registerTypeHierarchyAdapter(ItemStack.class, new ItemStackAdapter())
.setPrettyPrinting()
.serializeNulls().create();

View File

@@ -0,0 +1,58 @@
package net.islandearth.rpgregions.gson;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import java.lang.reflect.Type;
import java.util.Map;
public class PotionEffectAdapter implements JsonSerializer<PotionEffect>, JsonDeserializer<PotionEffect> {
private final Gson gson;
public PotionEffectAdapter() {
this.gson = new GsonBuilder().create();
}
@Override
public PotionEffect deserialize(JsonElement jsonElement, Type type,
JsonDeserializationContext context) throws JsonParseException {
return this.deserialise(gson.fromJson(jsonElement, new TypeToken<Map<String, Object>>(){}.getType()));
}
@Override
public JsonElement serialize(PotionEffect potionEffect, Type type, JsonSerializationContext context) {
// Our own serialisation - use type name instead of id
return gson.toJsonTree(ImmutableMap.<String, Object>builder()
.put("type", potionEffect.getType().getName())
.put("duration", potionEffect.getDuration())
.put("amplifier", potionEffect.getAmplifier())
.put("ambient", potionEffect.isAmbient())
.put("particles", potionEffect.hasParticles())
.put("icon", potionEffect.hasIcon())
.build());
}
/*
Our own deserialisation - use type name instead of id
*/
private PotionEffect deserialise(Map<String, Object> map) {
PotionEffectType type = PotionEffectType.getByName((String) map.get("type"));
double duration = (double) map.get("duration");
double amplifier = (double) map.get("amplifier");
boolean ambient = (boolean) map.get("ambient");
boolean particles = (boolean) map.get("particles");
boolean icon = (boolean) map.get("icon");
return new PotionEffect(type, (int) duration, (int) amplifier, ambient, particles, icon);
}
}

View File

@@ -48,12 +48,11 @@ public class RegionListener implements Listener {
configuredRegion.getEffects().forEach(regionEffect -> {
boolean canEffect = true;
for (ItemStack itemStack : player.getInventory()) {
if (!regionEffect.shouldIgnore(itemStack)) {
if (regionEffect.shouldIgnore(itemStack)) {
canEffect = false;
break;
}
}
if (canEffect) regionEffect.effect(player);
});

View File

@@ -98,7 +98,8 @@ public enum Translations {
for (Language language : Language.values()) {
try {
plugin.saveResource("lang/" + language.getCode(), false);
plugin.saveResource("lang/" + language.getCode() + ".yml", false);
plugin.getLogger().info("Generated " + language.getCode() + ".yml");
} catch (IllegalArgumentException ignored) { }
File file = new File(plugin.getDataFolder() + "/lang/" + language.getCode() + ".yml");