Refactor Debug class, set up framework for Paper-specific events.

This commit is contained in:
dfsek
2020-12-06 02:25:15 -07:00
parent 0f8ce8966a
commit d946ea9b15
18 changed files with 100 additions and 127 deletions

View File

@@ -5,7 +5,10 @@ import com.dfsek.terra.command.structure.LocateCommand;
import com.dfsek.terra.config.base.PluginConfig;
import com.dfsek.terra.config.base.WorldConfig;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.TerraChunkGenerator;
import com.dfsek.terra.listeners.EventListener;
import com.dfsek.terra.listeners.SpigotListener;
import com.dfsek.terra.registry.ConfigRegistry;
import com.dfsek.terra.util.PaperUtil;
import org.bstats.bukkit.Metrics;
@@ -39,33 +42,37 @@ public class Terra extends GaeaPlugin {
@SuppressWarnings("deprecation")
@Override
public void onEnable() {
instance = this;
Debug.setMain(this); // Set debug logger.
instance = this; // Singleton B)
saveDefaultConfig();
Metrics metrics = new Metrics(this, 9017);
metrics.addCustomChart(new Metrics.SingleLineChart("worlds", TerraWorld::numWorlds));
Metrics metrics = new Metrics(this, 9017); // Set up bStats.
metrics.addCustomChart(new Metrics.SingleLineChart("worlds", TerraWorld::numWorlds)); // World number chart.
Debug.setMain(this);
PluginConfig.load(this);
PluginConfig.load(this); // Load master config.yml
LangUtil.load(PluginConfig.getLanguage(), this); // Load language.
TerraWorld.invalidate();
ConfigRegistry.loadAll(this);
TerraWorld.invalidate(); // Clear/set up world cache.
ConfigRegistry.loadAll(this); // Load all config packs.
PluginCommand c = Objects.requireNonNull(getCommand("terra"));
TerraCommand command = new TerraCommand(this);
TerraCommand command = new TerraCommand(this); // Set up main Terra command.
c.setExecutor(command);
c.setTabCompleter(command);
LocateCommand locate = new LocateCommand(command, false);
PluginCommand locatePl = Objects.requireNonNull(getCommand("locate"));
locatePl.setExecutor(locate);
locatePl.setExecutor(locate); // Override locate command. Once Paper accepts StructureLocateEvent this will be unneeded on Paper implementations.
locatePl.setTabCompleter(locate);
long save = PluginConfig.getDataSaveInterval();
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, TerraChunkGenerator::saveAll, save, save);
Bukkit.getPluginManager().registerEvents(new EventListener(this), this);
Bukkit.getScheduler().scheduleAsyncRepeatingTask(this, TerraChunkGenerator::saveAll, save, save); // Schedule population data saving
Bukkit.getPluginManager().registerEvents(new EventListener(this), this); // Register master event listener
Bukkit.getPluginManager().registerEvents(new SpigotListener(this), this); // Register Spigot event listener, once Paper accepts StructureLocateEvent PR Spigot and Paper events will be separate.
PaperUtil.checkPaper(this);
}

View File

@@ -18,7 +18,7 @@ public class TerraProfiler extends WorldProfiler {
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "FloraTime")
.addMeasurement(new Measurement(10000000, DataType.PERIOD_MILLISECONDS), "TreeTime")
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "OreTime")
.addMeasurement(new Measurement(3500000, DataType.PERIOD_MILLISECONDS), "CaveTime")
.addMeasurement(new Measurement(5000000, DataType.PERIOD_MILLISECONDS), "CaveTime")
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "StructureTime")
.addMeasurement(new Measurement(1500000, DataType.PERIOD_MILLISECONDS), "ElevationTime");
}

View File

@@ -6,6 +6,7 @@ import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.base.ConfigPackTemplate;
import com.dfsek.terra.config.base.WorldConfig;
import com.dfsek.terra.config.builder.biomegrid.BiomeGridBuilder;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.TerraChunkGenerator;
import org.bukkit.Bukkit;
import org.bukkit.World;

View File

@@ -5,8 +5,8 @@ import com.dfsek.tectonic.annotations.Value;
import com.dfsek.tectonic.config.ConfigTemplate;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.terra.Debug;
import com.dfsek.terra.Terra;
import com.dfsek.terra.debug.Debug;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.util.JarUtil;

View File

@@ -1,7 +1,7 @@
package com.dfsek.terra.config.lang;
import com.dfsek.terra.Debug;
import com.dfsek.terra.Terra;
import com.dfsek.terra.debug.Debug;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.plugin.java.JavaPlugin;

View File

@@ -1,4 +1,4 @@
package com.dfsek.terra;
package com.dfsek.terra.debug;
import com.dfsek.terra.config.base.PluginConfig;
import org.bukkit.plugin.java.JavaPlugin;

View File

@@ -1,38 +0,0 @@
package com.dfsek.terra.event;
import com.dfsek.terra.TerraWorld;
import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
@SuppressWarnings("unused")
public abstract class TerraWorldEvent extends Event {
private static final HandlerList HANDLERS = new HandlerList();
private final TerraWorld world;
private final Location location;
public TerraWorldEvent(TerraWorld tw, Location l) {
this.world = tw;
this.location = l.clone();
}
@NotNull
public static HandlerList getHandlerList() {
return HANDLERS;
}
@Override
@NotNull
public HandlerList getHandlers() {
return HANDLERS;
}
public TerraWorld getWorld() {
return world;
}
public Location getLocation() {
return location;
}
}

View File

@@ -1,34 +0,0 @@
package com.dfsek.terra.event;
import com.dfsek.terra.TerraWorld;
import org.bukkit.Location;
import org.bukkit.event.Cancellable;
import org.polydev.gaea.tree.Tree;
public class TreeGenerateEvent extends TerraWorldEvent implements Cancellable {
private boolean cancelled;
private Tree tree;
public TreeGenerateEvent(TerraWorld tw, Location l, Tree tree) {
super(tw, l);
this.tree = tree;
}
public Tree getTree() {
return tree;
}
public void setTree(Tree tree) {
this.tree = tree;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@@ -1,6 +1,5 @@
package com.dfsek.terra.generation;
import com.dfsek.terra.Debug;
import com.dfsek.terra.Terra;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.TerraWorld;
@@ -9,6 +8,7 @@ import com.dfsek.terra.biome.palette.PaletteHolder;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.templates.BiomeTemplate;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.math.MathUtil;
import com.dfsek.terra.population.CavePopulator;
import com.dfsek.terra.population.FloraPopulator;

View File

@@ -0,0 +1,48 @@
package com.dfsek.terra.listeners;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.items.tree.TerraTree;
import com.dfsek.terra.registry.TreeRegistry;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.world.StructureGrowEvent;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.tree.TreeType;
import org.polydev.gaea.util.FastRandom;
/**
* Listener for events on all implementations.
*/
public class EventListener implements Listener {
private final GaeaPlugin main;
public EventListener(GaeaPlugin main) {
this.main = main;
}
@EventHandler
public void onSaplingGrow(StructureGrowEvent e) {
if(!TerraWorld.isTerraWorld(e.getWorld())) return;
TerraWorld tw = TerraWorld.getWorld(e.getWorld());
ConfigPack c = tw.getConfig();
if(c.getTemplate().isDisableSaplings()) return;
e.setCancelled(true);
Block block = e.getLocation().getBlock();
BlockData data = block.getBlockData();
block.setType(Material.AIR);
TreeRegistry registry = c.getTreeRegistry();
Tree tree = registry.get(TreeType.fromBukkit(e.getSpecies()).toString());
Debug.info("Overriding tree type: " + e.getSpecies());
if(tree instanceof TerraTree) {
if(!((TerraTree) tree).plantBlockCheck(e.getLocation().subtract(0, 1, 0), new FastRandom())) {
block.setBlockData(data);
}
} else if(!tree.plant(e.getLocation().subtract(0, 1, 0), new FastRandom(), main)) block.setBlockData(data);
}
}

View File

@@ -0,0 +1,11 @@
package com.dfsek.terra.listeners;
import org.bukkit.event.Listener;
/**
* Placeholder, will be used once Paper accepts StructureLocateEvent PR.
*/
public class PaperListener implements Listener {
}

View File

@@ -1,14 +1,10 @@
package com.dfsek.terra;
package com.dfsek.terra.listeners;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.async.AsyncStructureFinder;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.items.TerraStructure;
import com.dfsek.terra.generation.items.tree.TerraTree;
import com.dfsek.terra.registry.TreeRegistry;
import com.dfsek.terra.util.StructureTypeEnum;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.EnderSignal;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
@@ -19,16 +15,18 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.VillagerAcquireTradeEvent;
import org.bukkit.event.entity.VillagerCareerChangeEvent;
import org.bukkit.event.world.StructureGrowEvent;
import org.polydev.gaea.GaeaPlugin;
import org.polydev.gaea.tree.Tree;
import org.polydev.gaea.tree.TreeType;
import org.polydev.gaea.util.FastRandom;
public class EventListener implements Listener {
/**
* Listener to load on Spigot servers, contains Villager crash prevention and hacky ender eye redirection.
* <p>
* (This is currently loaded on all servers; once Paper accepts the StructureLocateEvent PR this will only be loaded on servers without
* StructureLocateEvent.
*/
public class SpigotListener implements Listener {
private final GaeaPlugin main;
public EventListener(GaeaPlugin main) {
public SpigotListener(GaeaPlugin main) {
this.main = main;
}
@@ -66,24 +64,4 @@ public class EventListener implements Listener {
e.setCancelled(true);
}
}
@EventHandler
public void onSaplingGrow(StructureGrowEvent e) {
if(!TerraWorld.isTerraWorld(e.getWorld())) return;
TerraWorld tw = TerraWorld.getWorld(e.getWorld());
ConfigPack c = tw.getConfig();
if(c.getTemplate().isDisableSaplings()) return;
e.setCancelled(true);
Block block = e.getLocation().getBlock();
BlockData data = block.getBlockData();
block.setType(Material.AIR);
TreeRegistry registry = c.getTreeRegistry();
Tree tree = registry.get(TreeType.fromBukkit(e.getSpecies()).toString());
Debug.info("Overriding tree type: " + e.getSpecies());
if(tree instanceof TerraTree) {
if(!((TerraTree) tree).plantBlockCheck(e.getLocation().subtract(0, 1, 0), new FastRandom())) {
block.setBlockData(data);
}
} else if(!tree.plant(e.getLocation().subtract(0, 1, 0), new FastRandom(), Terra.getInstance())) block.setBlockData(data);
}
}

View File

@@ -1,11 +1,11 @@
package com.dfsek.terra.population;
import com.dfsek.terra.Debug;
import com.dfsek.terra.TerraProfiler;
import com.dfsek.terra.TerraWorld;
import com.dfsek.terra.biome.UserDefinedBiome;
import com.dfsek.terra.biome.grid.TerraBiomeGrid;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.generation.items.TerraStructure;
import com.dfsek.terra.procgen.math.Vector2;
import com.dfsek.terra.structure.Rotation;

View File

@@ -1,8 +1,8 @@
package com.dfsek.terra.registry;
import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.terra.Debug;
import com.dfsek.terra.config.base.ConfigPack;
import com.dfsek.terra.debug.Debug;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;

View File

@@ -1,6 +1,6 @@
package com.dfsek.terra.structure;
import com.dfsek.terra.Debug;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.procgen.math.Vector2;
import com.dfsek.terra.util.structure.RotationUtil;
import net.jafama.FastMath;

View File

@@ -1,6 +1,6 @@
package com.dfsek.terra.structure;
import com.dfsek.terra.Debug;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.structure.serialize.SerializableBlockData;
import com.dfsek.terra.structure.serialize.block.SerializableBanner;
import com.dfsek.terra.structure.serialize.block.SerializableBlockState;

View File

@@ -1,6 +1,6 @@
package com.dfsek.terra.structure.features;
import com.dfsek.terra.Debug;
import com.dfsek.terra.debug.Debug;
import com.dfsek.terra.structure.Rotation;
import com.dfsek.terra.structure.Structure;
import com.dfsek.terra.structure.StructureInfo;

View File

@@ -1,6 +1,6 @@
package com.dfsek.terra.util;
import com.dfsek.terra.Debug;
import com.dfsek.terra.debug.Debug;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.jetbrains.annotations.NotNull;