9
0
mirror of https://gitlab.com/SamB440/rpgregions-2.git synced 2025-12-27 18:59:10 +00:00

Add automatic Lands region generation

This commit is contained in:
SamB440
2022-05-28 21:13:40 +01:00
parent d1759ba964
commit 0009c8705e
5 changed files with 133 additions and 2 deletions

View File

@@ -39,8 +39,8 @@ import java.util.stream.Stream;
public class ConfiguredRegion {
private final UUID world;
private final String id;
private UUID world;
private String id;
private String customName;
private final List<DiscoveryReward> rewards;
@NeedsGUI private Sound sound;
@@ -114,6 +114,11 @@ public class ConfiguredRegion {
return id;
}
@Deprecated //todo find better way to do this (templates)
public void setId(String id) {
this.id = id;
}
public String getCustomName() {
return customName;
}
@@ -193,6 +198,11 @@ public class ConfiguredRegion {
return Bukkit.getWorld(world);
}
@Deprecated //todo find better way to do this (templates)
public void setWorld(UUID world) {
this.world = world;
}
public boolean isDiscoverable() {
return discoverable;
}

View File

@@ -8,12 +8,14 @@ import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
@@ -23,11 +25,22 @@ import java.util.Set;
public class LandsIntegration implements IntegrationManager {
private final IRPGRegionsAPI plugin;
private final YamlConfiguration config;
private final me.angeschossen.lands.api.integration.LandsIntegration lands;
public LandsIntegration(IRPGRegionsAPI plugin) {
this.plugin = plugin;
this.config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder() + File.separator + "integrations" + File.separator + "lands.yml"));
this.lands = new me.angeschossen.lands.api.integration.LandsIntegration((Plugin) plugin);
Bukkit.getPluginManager().registerEvents(new LandsListener(this), (Plugin) plugin);
}
public IRPGRegionsAPI getPlugin() {
return plugin;
}
public YamlConfiguration getConfig() {
return config;
}
@Override

View File

@@ -0,0 +1,92 @@
package net.islandearth.rpgregions.api.integrations.lands;
import me.angeschossen.lands.api.events.ChunkDeleteEvent;
import me.angeschossen.lands.api.events.LandCreateEvent;
import me.angeschossen.lands.api.events.LandDeleteEvent;
import me.angeschossen.lands.api.land.Land;
import net.islandearth.rpgregions.api.IRPGRegionsAPI;
import net.islandearth.rpgregions.managers.data.IRPGRegionsCache;
import net.islandearth.rpgregions.managers.data.region.ConfiguredRegion;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.logging.Level;
public class LandsListener implements Listener {
private final LandsIntegration landsIntegration;
private final IRPGRegionsAPI plugin;
private final YamlConfiguration config;
public LandsListener(final LandsIntegration landsIntegration) {
this.landsIntegration = landsIntegration;
this.plugin = landsIntegration.getPlugin();
this.config = landsIntegration.getConfig();
}
@EventHandler
public void onCreate(LandCreateEvent event) {
final Land land = event.getLand();
final int landSize = land.getSize();
final int minSize = config.getInt("min-land-size");
if (landSize < minSize) return;
final ConfigurationSection templates = config.getConfigurationSection("templates");
// Key is like '10', '20' etc.
for (String key : templates.getKeys(false)) {
final int size = Integer.parseInt(key);
if (landSize >= size) {
final String template = templates.getString(key + ".template");
File templateFile = new File(plugin.getDataFolder() + File.separator + "templates" + File.separator + template);
if (!templateFile.exists()) {
plugin.getLogger().log(Level.SEVERE, String.format("Unable to load template '%s' for automatic region generation.", template));
continue;
}
try (Reader reader = new FileReader(templateFile)) {
ConfiguredRegion templateRegion = plugin.getGson().fromJson(reader, ConfiguredRegion.class);
templateRegion.setId(land.getName());
if (land.getSpawn() != null) templateRegion.setWorld(land.getSpawn().getWorld().getUID());
final IRPGRegionsCache regionsCache = plugin.getManagers().getRegionsCache();
if (regionsCache.getConfiguredRegion(land.getName()).isPresent()) {
regionsCache.removeConfiguredRegion(land.getName());
}
regionsCache.addConfiguredRegion(templateRegion);
plugin.debug("Automatically generated region: " + templateRegion.getId());
break;
} catch (Exception e) {
plugin.getLogger().log(Level.SEVERE, "Error loading template config " + templateFile.getName() + ".", e);
}
}
}
}
@EventHandler
public void onDelete(LandDeleteEvent event) {
final Land land = event.getLand();
final IRPGRegionsCache regionsCache = plugin.getManagers().getRegionsCache();
if (regionsCache.getConfiguredRegion(land.getName()).isPresent()) {
regionsCache.removeConfiguredRegion(land.getName());
}
}
@EventHandler
public void onDelete(ChunkDeleteEvent event) {
final Land land = event.getLand();
final int landSize = land.getSize() - 1;
final int minSize = config.getInt("min-land-size");
if (landSize >= minSize) return;
final IRPGRegionsCache regionsCache = plugin.getManagers().getRegionsCache();
if (regionsCache.getConfiguredRegion(land.getName()).isPresent()) {
regionsCache.removeConfiguredRegion(land.getName());
}
}
}

View File

@@ -162,6 +162,7 @@ public final class RPGRegions extends JavaPlugin implements IRPGRegionsAPI {
private void createConfig() {
saveDefaultConfig(); // Moved to config.yml
saveResource("integrations/lands.yml", false);
}
private void registerListeners() {

View File

@@ -0,0 +1,15 @@
# This is the file for editing the Lands integration.
# Should we automatically generate regions based on lands created?
auto-generate: true
# What is the minimum size in chunks of a land to automatically generate a region?
min-land-size: 10
# This is a group of template definitions. Templates are defined in plugins/RPGRegions/templates.
# You can use /rpgre to export a template region.
# The name of the file should be put here. The template will then be used to automatically generate a region.
templates:
10: # At >= 10 land size
template: "land_template_10.json"
20: # At >= 20 land size
template: "land_template_20.json"