preparations for Sponge API8

This commit is contained in:
dfsek
2021-02-23 15:57:27 -07:00
parent 98c1fea7fd
commit 9c2b844290
33 changed files with 413 additions and 84 deletions

View File

@@ -7,14 +7,14 @@ import com.dfsek.terra.api.platform.handle.WorldHandle;
import com.dfsek.terra.api.platform.world.World;
import com.dfsek.terra.api.registry.CheckedRegistry;
import com.dfsek.terra.api.registry.LockedRegistry;
import com.dfsek.terra.api.util.DebugLogger;
import com.dfsek.terra.api.util.logging.DebugLogger;
import com.dfsek.terra.api.util.logging.Logger;
import com.dfsek.terra.config.PluginConfig;
import com.dfsek.terra.config.lang.Language;
import com.dfsek.terra.config.pack.ConfigPack;
import com.dfsek.terra.world.TerraWorld;
import java.io.File;
import java.util.logging.Logger;
/**
* Represents a Terra mod/plugin instance.
@@ -24,7 +24,7 @@ public interface TerraPlugin extends LoaderRegistrar {
TerraWorld getWorld(World world);
Logger getLogger();
Logger logger();
PluginConfig getTerraConfig();

View File

@@ -47,15 +47,15 @@ public class TerraEventManager implements EventManager {
} catch(InvocationTargetException e) {
StringWriter writer = new StringWriter();
e.getTargetException().printStackTrace(new PrintWriter(writer));
main.getLogger().warning("Exception occurred during event handling:");
main.getLogger().warning(writer.toString());
main.getLogger().warning("Report this to the maintainers of " + listenerHolder.method.getName());
main.logger().warning("Exception occurred during event handling:");
main.logger().warning(writer.toString());
main.logger().warning("Report this to the maintainers of " + listenerHolder.method.getName());
} catch(Exception e) {
StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));
main.getLogger().warning("Exception occurred during event handling:");
main.getLogger().warning(writer.toString());
main.getLogger().warning("Report this to the maintainers of " + listenerHolder.method.getName());
main.logger().warning("Exception occurred during event handling:");
main.logger().warning(writer.toString());
main.logger().warning("Report this to the maintainers of " + listenerHolder.method.getName());
}
}
);

View File

@@ -8,9 +8,13 @@ import com.dfsek.terra.api.platform.entity.EntityType;
* Interface to be implemented for world manipulation.
*/
public interface WorldHandle {
void setBlockData(Block block, BlockData data, boolean physics);
default void setBlockData(Block block, BlockData data, boolean physics) {
block.setBlockData(data, physics);
}
BlockData getBlockData(Block block);
default BlockData getBlockData(Block block) {
return block.getBlockData();
}
BlockData createBlockData(String data);

View File

@@ -55,7 +55,7 @@ public class EnchantFunction implements LootFunction {
try {
meta.addEnchantment(chosen, FastMath.max(lvl, 1));
} catch(IllegalArgumentException e) {
main.getLogger().warning("Attempted to enchant " + original.getType() + " with " + chosen + " at level " + FastMath.max(lvl, 1) + ", but an unexpected exception occurred! Usually this is caused by a misbehaving enchantment plugin.");
main.logger().warning("Attempted to enchant " + original.getType() + " with " + chosen + " at level " + FastMath.max(lvl, 1) + ", but an unexpected exception occurred! Usually this is caused by a misbehaving enchantment plugin.");
}
}
original.setItemMeta(meta);

View File

@@ -149,7 +149,7 @@ public class StructureScript {
try {
return !block.apply(arguments).getLevel().equals(Block.ReturnLevel.FAIL);
} catch(RuntimeException e) {
main.getLogger().severe("Failed to generate structure at " + arguments.getBuffer().getOrigin() + ": " + e.getMessage());
main.logger().severe("Failed to generate structure at " + arguments.getBuffer().getOrigin() + ": " + e.getMessage());
main.getDebugLogger().stack(e);
return false;
}

View File

@@ -45,7 +45,7 @@ public class LootFunction implements Function<Void> {
LootTable table = registry.get(id);
if(table == null) {
main.getLogger().severe("No such loot table " + id);
main.logger().severe("No such loot table " + id);
return null;
}

View File

@@ -57,7 +57,7 @@ public class StructureFunction implements Function<Boolean> {
String app = id.apply(implementationArguments, variableMap);
StructureScript script = registry.get(app);
if(script == null) {
main.getLogger().severe("No such structure " + app);
main.logger().severe("No such structure " + app);
return null;
}
@@ -66,7 +66,7 @@ public class StructureFunction implements Function<Boolean> {
try {
rotation1 = Rotation.valueOf(rotString);
} catch(IllegalArgumentException e) {
main.getLogger().severe("Invalid rotation " + rotString);
main.logger().severe("Invalid rotation " + rotString);
return null;
}

View File

@@ -20,7 +20,7 @@ public class BufferedLootApplication implements BufferedItem {
public void paste(Location origin) {
BlockState data = origin.getBlock().getState();
if(!(data instanceof Container)) {
main.getLogger().severe("Failed to place loot at " + origin + "; block " + data + " is not container.");
main.logger().severe("Failed to place loot at " + origin + "; block " + data + " is not container.");
return;
}
Container container = (Container) data;

View File

@@ -20,7 +20,7 @@ public class BufferedStateManipulator implements BufferedItem {
state.applyState(data);
state.update(false);
} catch(Exception e) {
main.getLogger().warning("Could not apply BlockState at " + origin + ": " + e.getMessage());
main.logger().warning("Could not apply BlockState at " + origin + ": " + e.getMessage());
main.getDebugLogger().stack(e);
}
}

View File

@@ -1,6 +1,4 @@
package com.dfsek.terra.api.util;
import java.util.logging.Logger;
package com.dfsek.terra.api.util.logging;
public class DebugLogger {
private final Logger logger;

View File

@@ -0,0 +1,24 @@
package com.dfsek.terra.api.util.logging;
public class JavaLogger implements Logger {
private final java.util.logging.Logger logger;
public JavaLogger(java.util.logging.Logger logger) {
this.logger = logger;
}
@Override
public void info(String message) {
logger.info(message);
}
@Override
public void warning(String message) {
logger.warning(message);
}
@Override
public void severe(String message) {
logger.severe(message);
}
}

View File

@@ -0,0 +1,9 @@
package com.dfsek.terra.api.util.logging;
public interface Logger {
void info(String message);
void warning(String message);
void severe(String message);
}

View File

@@ -7,6 +7,7 @@ import com.dfsek.tectonic.exception.ConfigException;
import com.dfsek.tectonic.loading.ConfigLoader;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.util.JarUtil;
import com.dfsek.terra.api.util.logging.Logger;
import java.io.File;
import java.io.FileInputStream;
@@ -14,7 +15,6 @@ import java.io.IOException;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.jar.JarFile;
import java.util.logging.Logger;
@SuppressWarnings("FieldMayBeFinal")
public class PluginConfig implements ConfigTemplate {
@@ -63,7 +63,7 @@ public class PluginConfig implements ConfigTemplate {
private int maxRecursion = 1000;
public void load(TerraPlugin main) {
Logger logger = main.getLogger();
Logger logger = main.logger();
logger.info("Loading config values");
try(FileInputStream file = new FileInputStream(new File(main.getDataFolder(), "config.yml"))) {
ConfigLoader loader = new ConfigLoader();

View File

@@ -2,22 +2,20 @@ package com.dfsek.terra.config.lang;
import com.dfsek.terra.api.TerraPlugin;
import com.dfsek.terra.api.platform.CommandSender;
import com.dfsek.terra.api.util.logging.Logger;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.jar.JarFile;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.dfsek.terra.api.util.JarUtil.copyResourcesToDirectory;
public final class LangUtil {
private static Language language;
private static Logger logger;
public static void load(String langID, TerraPlugin main) {
logger = main.getLogger();
Logger logger = main.logger();
File file = new File(main.getDataFolder(), "lang");
try(JarFile jar = new JarFile(new File(TerraPlugin.class.getProtectionDomain().getCodeSource().getLocation().toURI()))) {
copyResourcesToDirectory(jar, "lang", file.toString());
@@ -41,10 +39,6 @@ public final class LangUtil {
return language;
}
public static void log(String messageID, Level level, String... args) {
language.getMessage(messageID).log(logger, level, args);
}
public static void send(String messageID, CommandSender sender, String... args) {
language.getMessage(messageID).send(sender, args);
}

View File

@@ -34,7 +34,6 @@ import com.dfsek.terra.config.factories.TreeFactory;
import com.dfsek.terra.config.fileloaders.FolderLoader;
import com.dfsek.terra.config.fileloaders.Loader;
import com.dfsek.terra.config.fileloaders.ZIPLoader;
import com.dfsek.terra.config.lang.LangUtil;
import com.dfsek.terra.config.loaders.config.BufferedImageLoader;
import com.dfsek.terra.config.loaders.config.biome.templates.source.BiomePipelineTemplate;
import com.dfsek.terra.config.loaders.config.biome.templates.source.ImageProviderTemplate;
@@ -79,7 +78,6 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
@@ -136,7 +134,7 @@ public class ConfigPack implements LoaderRegistrar {
try {
selfLoader.load(template, new FileInputStream(pack));
main.getLogger().info("Loading config pack \"" + template.getID() + "\"");
main.logger().info("Loading config pack \"" + template.getID() + "\"");
load(l, main);
ConfigPackPostTemplate packPostTemplate = new ConfigPackPostTemplate();
@@ -147,7 +145,7 @@ public class ConfigPack implements LoaderRegistrar {
throw new LoadException("No pack.yml file found in " + folder.getAbsolutePath(), e);
}
} catch(Exception e) {
main.getLogger().severe("Failed to load config pack from folder \"" + folder.getAbsolutePath() + "\"");
main.logger().severe("Failed to load config pack from folder \"" + folder.getAbsolutePath() + "\"");
throw e;
}
}
@@ -178,7 +176,7 @@ public class ConfigPack implements LoaderRegistrar {
if(pack == null) throw new LoadException("No pack.yml file found in " + file.getName());
selfLoader.load(template, file.getInputStream(pack));
main.getLogger().info("Loading config pack \"" + template.getID() + "\"");
main.logger().info("Loading config pack \"" + template.getID() + "\"");
load(l, main);
@@ -191,7 +189,7 @@ public class ConfigPack implements LoaderRegistrar {
throw new LoadException("Unable to load pack.yml from ZIP file", e);
}
} catch(Exception e) {
main.getLogger().severe("Failed to load config pack from ZIP archive \"" + file.getName() + "\"");
main.logger().severe("Failed to load config pack from ZIP archive \"" + file.getName() + "\"");
throw e;
}
}
@@ -236,7 +234,7 @@ public class ConfigPack implements LoaderRegistrar {
.open("biomes", ".yml").then(streams -> buildAll(new BiomeFactory(this), biomeRegistry, abstractConfigLoader.load(streams, () -> new BiomeTemplate(this, main)), main)).close();
main.getEventManager().callEvent(new ConfigPackPostLoadEvent(this));
LangUtil.log("config-pack.loaded", Level.INFO, template.getID(), String.valueOf((System.nanoTime() - start) / 1000000D), template.getAuthor(), template.getVersion());
main.logger().info("Loaded config pack \"" + template.getID() + "\" v" + template.getVersion() + " by " + template.getAuthor() + " in " + (System.nanoTime() - start) / 1000000D + "ms.");
}
public TerraBiome getBiome(String id) {

View File

@@ -59,7 +59,7 @@ public class FloraRegistry extends OpenRegistry<Flora> {
try {
add(id, flora.call());
} catch(Exception e) {
main.getLogger().warning("Failed to load Flora item: " + id + ": " + e.getMessage());
main.logger().warning("Failed to load Flora item: " + id + ": " + e.getMessage());
}
}

View File

@@ -39,7 +39,7 @@ public class TreeRegistry extends OpenRegistry<Tree> {
try {
add(id, new FractalTreeHolder(value));
} catch(Exception e) {
main.getLogger().warning("Unable to load tree " + id + ": " + e.getMessage());
main.logger().warning("Unable to load tree " + id + ": " + e.getMessage());
}
}

View File

@@ -35,7 +35,7 @@ public class AddonRegistry extends OpenRegistry<TerraAddon> {
public boolean add(String identifier, TerraAddon addon) {
if(contains(identifier)) throw new IllegalArgumentException("Addon " + identifier + " is already registered.");
addon.initialize();
main.getLogger().info("Loaded addon " + addon.getName() + " v" + addon.getVersion() + ", by " + addon.getAuthor());
main.logger().info("Loaded addon " + addon.getName() + " v" + addon.getVersion() + ", by " + addon.getAuthor());
return super.add(identifier, addon);
}
@@ -56,7 +56,7 @@ public class AddonRegistry extends OpenRegistry<TerraAddon> {
try {
for(File jar : addonsFolder.listFiles(file -> file.getName().endsWith(".jar"))) {
main.getLogger().info("Loading Addon(s) from: " + jar.getName());
main.logger().info("Loading Addon(s) from: " + jar.getName());
for(Class<? extends TerraAddon> addonClass : AddonClassLoader.fetchAddonClasses(jar)) {
pool.add(new PreLoadAddon(addonClass));
}
@@ -95,9 +95,9 @@ public class AddonRegistry extends OpenRegistry<TerraAddon> {
addChecked(loadedAddon.getName(), loadedAddon);
} catch(DuplicateEntryException e) {
valid = false;
main.getLogger().severe("Duplicate addon ID; addon with ID " + loadedAddon.getName() + " is already loaded.");
main.getLogger().severe("Existing addon class: " + get(loadedAddon.getName()).getClass().getCanonicalName());
main.getLogger().severe("Duplicate addon class: " + addonClass.getCanonicalName());
main.logger().severe("Duplicate addon ID; addon with ID " + loadedAddon.getName() + " is already loaded.");
main.logger().severe("Existing addon class: " + get(loadedAddon.getName()).getClass().getCanonicalName());
main.logger().severe("Duplicate addon class: " + addonClass.getCanonicalName());
}
}
} catch(AddonLoadException | IOException e) {