9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 03:19:14 +00:00

refactor vanilla loots

This commit is contained in:
XiaoMoMi
2025-04-04 03:57:55 +08:00
parent 0385343b2a
commit 4e945f2af2
6 changed files with 105 additions and 66 deletions

View File

@@ -0,0 +1,30 @@
package net.momirealms.craftengine.core.loot;
import net.momirealms.craftengine.core.util.Key;
import java.util.*;
public abstract class AbstractVanillaLootManager implements VanillaLootManager {
protected final Map<Integer, VanillaLoot> blockLoots = new HashMap<>();
// TODO More entity NBT
protected final Map<Key, VanillaLoot> entityLoots = new HashMap<>();
public AbstractVanillaLootManager() {
}
@Override
public void unload() {
this.blockLoots.clear();
this.entityLoots.clear();
}
@Override
public Optional<VanillaLoot> getBlockLoot(int vanillaBlockState) {
return Optional.ofNullable(this.blockLoots.get(vanillaBlockState));
}
@Override
public Optional<VanillaLoot> getEntityLoot(Key entity) {
return Optional.ofNullable(this.entityLoots.get(entity));
}
}

View File

@@ -1,23 +1,16 @@
package net.momirealms.craftengine.core.loot;
import net.momirealms.craftengine.core.pack.LoadingSequence;
import net.momirealms.craftengine.core.plugin.Reloadable;
import net.momirealms.craftengine.core.plugin.config.ConfigSectionParser;
import net.momirealms.craftengine.core.util.Key;
import java.util.Optional;
public interface VanillaLootManager extends ConfigSectionParser, Reloadable {
String[] CONFIG_SECTION_NAME = new String[] {"vanilla-loots", "vanilla-loot", "loots", "loot"};
public interface VanillaLootManager extends Reloadable {
@Override
default int loadingSequence() {
return LoadingSequence.VANILLA_LOOTS;
}
@Override
default String[] sectionId() {
return CONFIG_SECTION_NAME;
}
ConfigSectionParser parser();
Optional<VanillaLoot> getBlockLoot(int blockState);
Optional<VanillaLoot> getEntityLoot(Key entity);
}

View File

@@ -142,7 +142,7 @@ public abstract class CraftEngine implements Plugin {
@Override
public void enable() {
this.networkManager.enable();
this.templateManager = new TemplateManagerImpl(this);
this.templateManager = new TemplateManagerImpl();
this.itemBrowserManager = new ItemBrowserManagerImpl(this);
this.commandManager.registerDefaultFeatures();
// delay the reload so other plugins can register some parsers
@@ -205,7 +205,7 @@ public abstract class CraftEngine implements Plugin {
this.packManager.registerConfigSectionParser(this.soundManager);
this.packManager.registerConfigSectionParser(this.soundManager.jukeboxSongManager());
// register vanilla loot parser
this.packManager.registerConfigSectionParser(this.vanillaLootManager);
this.packManager.registerConfigSectionParser(this.vanillaLootManager.parser());
}
protected abstract void delayedEnable();

View File

@@ -4,6 +4,7 @@ import net.momirealms.craftengine.core.pack.LoadingSequence;
import net.momirealms.craftengine.core.pack.Pack;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.ConfigSectionParser;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.util.GsonHelper;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.MiscUtils;
@@ -19,13 +20,11 @@ import java.util.regex.Matcher;
import static net.momirealms.craftengine.core.util.MiscUtils.castToMap;
public class TemplateManagerImpl implements TemplateManager {
private final CraftEngine plugin;
private final Map<Key, Object> templates = new HashMap<>();
private final static Set<String> NON_TEMPLATE_KEY = new HashSet<>(Set.of(TEMPLATE, ARGUMENTS, OVERRIDES));
private final TemplateParser templateParser;
public TemplateManagerImpl(CraftEngine plugin) {
this.plugin = plugin;
public TemplateManagerImpl() {
this.templateParser = new TemplateParser();
}
@@ -60,7 +59,10 @@ public class TemplateManagerImpl implements TemplateManager {
@Override
public void addTemplate(Pack pack, Path path, Key id, Object obj) {
if (PreConditions.runIfTrue(this.templates.containsKey(id), () -> this.plugin.logger().warn(path, "Template duplicates: " + id))) return;
if (this.templates.containsKey(id)) {
TranslationManager.instance().log("warning.config.template.duplicated", path.toString(), id.toString());
return;
}
this.templates.put(id, obj);
}