mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-04-05 07:16:10 +00:00
commands on Forge
This commit is contained in:
@@ -8,6 +8,7 @@ import com.dfsek.terra.api.addons.annotations.Author;
|
||||
import com.dfsek.terra.api.addons.annotations.Version;
|
||||
import com.dfsek.terra.api.command.CommandManager;
|
||||
import com.dfsek.terra.api.command.TerraCommandManager;
|
||||
import com.dfsek.terra.api.command.exception.CommandException;
|
||||
import com.dfsek.terra.api.command.exception.MalformedCommandException;
|
||||
import com.dfsek.terra.api.event.EventListener;
|
||||
import com.dfsek.terra.api.event.EventManager;
|
||||
@@ -15,6 +16,7 @@ import com.dfsek.terra.api.event.TerraEventManager;
|
||||
import com.dfsek.terra.api.event.annotations.Global;
|
||||
import com.dfsek.terra.api.event.annotations.Priority;
|
||||
import com.dfsek.terra.api.event.events.config.ConfigPackPreLoadEvent;
|
||||
import com.dfsek.terra.api.platform.CommandSender;
|
||||
import com.dfsek.terra.api.platform.block.BlockData;
|
||||
import com.dfsek.terra.api.platform.handle.ItemHandle;
|
||||
import com.dfsek.terra.api.platform.handle.WorldHandle;
|
||||
@@ -25,6 +27,7 @@ import com.dfsek.terra.api.registry.LockedRegistry;
|
||||
import com.dfsek.terra.api.transform.NotNullValidator;
|
||||
import com.dfsek.terra.api.transform.Transformer;
|
||||
import com.dfsek.terra.api.util.logging.DebugLogger;
|
||||
import com.dfsek.terra.api.util.mutable.MutableInteger;
|
||||
import com.dfsek.terra.commands.CommandUtil;
|
||||
import com.dfsek.terra.config.GenericLoaders;
|
||||
import com.dfsek.terra.config.PluginConfig;
|
||||
@@ -34,26 +37,29 @@ import com.dfsek.terra.config.lang.Language;
|
||||
import com.dfsek.terra.config.pack.ConfigPack;
|
||||
import com.dfsek.terra.config.templates.BiomeTemplate;
|
||||
import com.dfsek.terra.forge.inventory.ForgeItemHandle;
|
||||
import com.dfsek.terra.forge.world.ForgeAdapter;
|
||||
import com.dfsek.terra.forge.world.ForgeBiome;
|
||||
import com.dfsek.terra.forge.world.ForgeTree;
|
||||
import com.dfsek.terra.forge.world.ForgeWorldHandle;
|
||||
import com.dfsek.terra.forge.world.TerraBiomeSource;
|
||||
import com.dfsek.terra.forge.world.features.PopulatorFeature;
|
||||
import com.dfsek.terra.forge.world.generator.ForgeChunkGenerator;
|
||||
import com.dfsek.terra.forge.world.generator.ForgeChunkGeneratorWrapper;
|
||||
import com.dfsek.terra.forge.world.generator.config.TerraLevelType;
|
||||
import com.dfsek.terra.profiler.Profiler;
|
||||
import com.dfsek.terra.profiler.ProfilerImpl;
|
||||
import com.dfsek.terra.registry.exception.DuplicateEntryException;
|
||||
import com.dfsek.terra.registry.master.AddonRegistry;
|
||||
import com.dfsek.terra.registry.master.ConfigRegistry;
|
||||
import com.dfsek.terra.world.TerraWorld;
|
||||
import com.mojang.brigadier.arguments.StringArgumentType;
|
||||
import com.mojang.brigadier.builder.ArgumentBuilder;
|
||||
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
|
||||
import net.minecraft.block.Blocks;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.button.Button;
|
||||
import net.minecraft.command.CommandSource;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.util.registry.WorldSettingsImport;
|
||||
import net.minecraft.util.text.StringTextComponent;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
import net.minecraft.world.biome.BiomeGenerationSettings;
|
||||
@@ -71,13 +77,11 @@ import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.ForgeWorldTypeScreens;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.common.world.ForgeWorldType;
|
||||
import net.minecraftforge.event.RegisterCommandsEvent;
|
||||
import net.minecraftforge.event.RegistryEvent;
|
||||
import net.minecraftforge.eventbus.api.IEventBus;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
@@ -87,10 +91,15 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import static com.mojang.brigadier.builder.LiteralArgumentBuilder.literal;
|
||||
import static com.mojang.brigadier.builder.RequiredArgumentBuilder.argument;
|
||||
|
||||
@Mod("terra")
|
||||
@Mod.EventBusSubscriber(modid = "terra", bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
@@ -103,6 +112,9 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
private final EventManager eventManager = new TerraEventManager(this);
|
||||
private final GenericLoaders genericLoaders = new GenericLoaders(this);
|
||||
private final Profiler profiler = new ProfilerImpl();
|
||||
|
||||
private final CommandManager manager = new TerraCommandManager(this);
|
||||
|
||||
private final com.dfsek.terra.api.util.logging.Logger logger = new com.dfsek.terra.api.util.logging.Logger() {
|
||||
private final org.apache.logging.log4j.Logger logger = LogManager.getLogger();
|
||||
|
||||
@@ -137,6 +149,9 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
public TerraForgePlugin() {
|
||||
if(INSTANCE != null) throw new IllegalStateException("Only one TerraPlugin instance may exist.");
|
||||
INSTANCE = this;
|
||||
MinecraftForge.EVENT_BUS.register(ClientEvents.class);
|
||||
MinecraftForge.EVENT_BUS.register(getClass());
|
||||
MinecraftForge.EVENT_BUS.register(ForgeEvents.class);
|
||||
}
|
||||
|
||||
public static TerraForgePlugin getInstance() {
|
||||
@@ -147,11 +162,40 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
return pack.getTemplate().getID().toLowerCase() + "/" + biomeID.toLowerCase(Locale.ROOT);
|
||||
}
|
||||
|
||||
private static RequiredArgumentBuilder<CommandSource, String> assemble(RequiredArgumentBuilder<CommandSource, String> in, CommandManager manager) {
|
||||
return in.suggests((context, builder) -> {
|
||||
List<String> args = parseCommand(context.getInput());
|
||||
CommandSender sender = ForgeAdapter.adapt(context.getSource());
|
||||
try {
|
||||
manager.tabComplete(args.remove(0), sender, args).forEach(builder::suggest);
|
||||
} catch(CommandException e) {
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
return builder.buildFuture();
|
||||
}).executes(context -> {
|
||||
List<String> args = parseCommand(context.getInput());
|
||||
try {
|
||||
manager.execute(args.remove(0), ForgeAdapter.adapt(context.getSource()), args);
|
||||
} catch(CommandException e) {
|
||||
context.getSource().sendFailure(new StringTextComponent(e.getMessage()));
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
private static List<String> parseCommand(String command) {
|
||||
if(command.startsWith("/terra ")) command = command.substring("/terra ".length());
|
||||
else if(command.startsWith("/te ")) command = command.substring("/te ".length());
|
||||
List<String> c = new ArrayList<>(Arrays.asList(command.split(" ")));
|
||||
if(command.endsWith(" ")) c.add("");
|
||||
return c;
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public static void register(RegistryEvent.Register<Biome> event) {
|
||||
INSTANCE.setup(); // Setup now because we need the biomes, and this event happens after blocks n stuff
|
||||
INSTANCE.registry.forEach(pack -> pack.getBiomeRegistry().forEach((id, biome) -> {
|
||||
Biome minecraftBiome = createBiome(biome);
|
||||
INSTANCE.getConfigRegistry().forEach(pack -> pack.getBiomeRegistry().forEach((id, biome) -> {
|
||||
Biome minecraftBiome = INSTANCE.createBiome(biome);
|
||||
INSTANCE.logger().info("Registering biome " + minecraftBiome.getRegistryName());
|
||||
event.getRegistry().register(minecraftBiome);
|
||||
})); // Register all Terra biomes.
|
||||
@@ -159,7 +203,7 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
|
||||
@SubscribeEvent
|
||||
public static void registerLevels(RegistryEvent.Register<ForgeWorldType> event) {
|
||||
getInstance().logger.info("Registering level types...");
|
||||
INSTANCE.logger().info("Registering level types...");
|
||||
event.getRegistry().register(TerraLevelType.FORGE_WORLD_TYPE);
|
||||
}
|
||||
|
||||
@@ -168,9 +212,9 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
event.getRegistry().register(POPULATOR_FEATURE);
|
||||
}
|
||||
|
||||
private static Biome createBiome(BiomeBuilder biome) {
|
||||
public Biome createBiome(BiomeBuilder biome) {
|
||||
BiomeTemplate template = biome.getTemplate();
|
||||
Map<String, Integer> colors = template.getColors();
|
||||
//Map<String, Integer> colors = template.getColors();
|
||||
|
||||
Biome vanilla = ((ForgeBiome) new ArrayList<>(biome.getVanillaBiomes().getContents()).get(0)).getHandle();
|
||||
|
||||
@@ -231,31 +275,12 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
|
||||
logger.info("Loaded packs.");
|
||||
|
||||
CommandManager manager = new TerraCommandManager(this);
|
||||
|
||||
try {
|
||||
CommandUtil.registerAll(manager);
|
||||
} catch(MalformedCommandException e) {
|
||||
e.printStackTrace(); // TODO do something here even though this should literally never happen
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
|
||||
int max = manager.getMaxArgumentDepth();
|
||||
RequiredArgumentBuilder<ServerCommandSource, String> arg = argument("arg" + (max - 1), StringArgumentType.word());
|
||||
for(int i = 0; i < max; i++) {
|
||||
RequiredArgumentBuilder<ServerCommandSource, String> next = argument("arg" + (max - i - 1), StringArgumentType.word());
|
||||
|
||||
arg = next.then(assemble(arg, manager));
|
||||
}
|
||||
|
||||
dispatcher.register(literal("terra").executes(context -> 1).then(assemble(arg, manager)));
|
||||
dispatcher.register(literal("te").executes(context -> 1).then(assemble(arg, manager)));
|
||||
//dispatcher.register(literal("te").redirect(root));
|
||||
}
|
||||
);
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -345,7 +370,7 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
|
||||
@Override
|
||||
public String platformName() {
|
||||
return "Fabric";
|
||||
return "Forge";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -366,56 +391,27 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
return eventManager;
|
||||
}
|
||||
|
||||
/*
|
||||
private RequiredArgumentBuilder<ServerCommandSource, String> assemble(RequiredArgumentBuilder<ServerCommandSource, String> in, CommandManager manager) {
|
||||
return in.suggests((context, builder) -> {
|
||||
List<String> args = parseCommand(context.getInput());
|
||||
CommandSender sender = FabricAdapter.adapt(context.getSource());
|
||||
try {
|
||||
manager.tabComplete(args.remove(0), sender, args).forEach(builder::suggest);
|
||||
} catch(CommandException e) {
|
||||
sender.sendMessage(e.getMessage());
|
||||
}
|
||||
return builder.buildFuture();
|
||||
}).executes(context -> {
|
||||
List<String> args = parseCommand(context.getInput());
|
||||
try {
|
||||
manager.execute(args.remove(0), FabricAdapter.adapt(context.getSource()), args);
|
||||
} catch(CommandException e) {
|
||||
context.getSource().sendError(new LiteralText(e.getMessage()));
|
||||
}
|
||||
return 1;
|
||||
});
|
||||
}
|
||||
|
||||
private List<String> parseCommand(String command) {
|
||||
if(command.startsWith("/terra ")) command = command.substring("/terra ".length());
|
||||
else if(command.startsWith("/te ")) command = command.substring("/te ".length());
|
||||
List<String> c = new ArrayList<>(Arrays.asList(command.split(" ")));
|
||||
if(command.endsWith(" ")) c.add("");
|
||||
return c;
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
@Override
|
||||
public Profiler getProfiler() {
|
||||
return profiler;
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber(value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
public static final class ClientEvents {
|
||||
@Mod.EventBusSubscriber(modid = "terra", bus = Mod.EventBusSubscriber.Bus.FORGE)
|
||||
public static final class ForgeEvents {
|
||||
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||
@SubscribeEvent
|
||||
public static void register(FMLClientSetupEvent event) {
|
||||
getInstance().logger.info("Client setup...");
|
||||
public static void registerCommands(RegisterCommandsEvent event) {
|
||||
int max = INSTANCE.manager.getMaxArgumentDepth();
|
||||
RequiredArgumentBuilder<CommandSource, String> arg = argument("arg" + (max - 1), StringArgumentType.word());
|
||||
for(int i = 0; i < max; i++) {
|
||||
RequiredArgumentBuilder<CommandSource, String> next = argument("arg" + (max - i - 1), StringArgumentType.word());
|
||||
|
||||
ForgeWorldType world = TerraLevelType.FORGE_WORLD_TYPE;
|
||||
ForgeWorldTypeScreens.registerFactory(world, (returnTo, dimensionGeneratorSettings) -> new Screen(world.getDisplayName()) {
|
||||
@Override
|
||||
protected void init() {
|
||||
addButton(new Button(0, 0, 120, 20, new StringTextComponent("close"), btn -> Minecraft.getInstance().setScreen(returnTo)));
|
||||
}
|
||||
});
|
||||
arg = next.then(assemble(arg, INSTANCE.manager));
|
||||
}
|
||||
|
||||
event.getDispatcher().register(literal("terra").executes(context -> 1).then((ArgumentBuilder) assemble(arg, INSTANCE.manager)));
|
||||
event.getDispatcher().register(literal("te").executes(context -> 1).then((ArgumentBuilder) assemble(arg, INSTANCE.manager)));
|
||||
//dispatcher.register(literal("te").redirect(root));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -468,4 +464,33 @@ public class TerraForgePlugin implements TerraPlugin {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Mod.EventBusSubscriber(value = Dist.CLIENT, bus = Mod.EventBusSubscriber.Bus.MOD)
|
||||
public static final class ClientEvents {
|
||||
@SubscribeEvent
|
||||
public static void register(FMLClientSetupEvent event) {
|
||||
INSTANCE.logger.info("Client setup...");
|
||||
|
||||
ForgeWorldType world = TerraLevelType.FORGE_WORLD_TYPE;
|
||||
ForgeWorldTypeScreens.registerFactory(world, (returnTo, dimensionGeneratorSettings) -> new Screen(world.getDisplayName()) {
|
||||
private final MutableInteger num = new MutableInteger(0);
|
||||
private final List<ConfigPack> packs = new ArrayList<>();
|
||||
private final Button toggle = new Button(0, 25, 120, 20, new StringTextComponent(""), button -> {
|
||||
num.increment();
|
||||
if(num.get() >= packs.size()) num.set(0);
|
||||
button.setMessage(new StringTextComponent("Pack: " + packs.get(num.get()).getTemplate().getID()));
|
||||
});
|
||||
|
||||
@Override
|
||||
protected void init() {
|
||||
packs.clear();
|
||||
INSTANCE.registry.forEach((Consumer<ConfigPack>) packs::add);
|
||||
addButton(new Button(0, 0, 120, 20, new StringTextComponent("Close"), btn -> Minecraft.getInstance().setScreen(returnTo)));
|
||||
toggle.setMessage(new StringTextComponent("Pack: " + packs.get(num.get()).getTemplate().getID()));
|
||||
addButton(toggle);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
package com.dfsek.terra.forge;
|
||||
package com.dfsek.terra.forge.world.generator.config;
|
||||
|
||||
import com.dfsek.terra.config.pack.ConfigPack;
|
||||
import com.dfsek.terra.forge.TerraForgePlugin;
|
||||
import com.dfsek.terra.forge.world.TerraBiomeSource;
|
||||
import com.dfsek.terra.forge.world.generator.ForgeChunkGeneratorWrapper;
|
||||
import jdk.nashorn.internal.runtime.regexp.joni.Config;
|
||||
import net.minecraft.util.registry.Registry;
|
||||
import net.minecraft.world.biome.Biome;
|
||||
import net.minecraft.world.gen.ChunkGenerator;
|
||||
import net.minecraft.world.gen.DimensionSettings;
|
||||
import net.minecraftforge.common.world.ForgeWorldType;
|
||||
import net.minecraftforge.fml.common.registry.GameRegistry;
|
||||
import net.minecraftforge.registries.ForgeRegistries;
|
||||
|
||||
public class TerraLevelType implements ForgeWorldType.IChunkGeneratorFactory {
|
||||
public static final TerraLevelType TERRA_LEVEL_TYPE = new TerraLevelType();
|
||||
public static final ForgeWorldType FORGE_WORLD_TYPE = new ForgeWorldType(TERRA_LEVEL_TYPE).setRegistryName("terra", "world");
|
||||
@Override
|
||||
public ChunkGenerator createChunkGenerator(Registry<Biome> biomeRegistry, Registry<DimensionSettings> dimensionSettingsRegistry, long seed, String generatorSettings) {
|
||||
System.out.println(generatorSettings);
|
||||
dimensionSettingsRegistry.forEach(System.out::println);
|
||||
ConfigPack pack = TerraForgePlugin.getInstance().getConfigRegistry().get("DEFAULT");
|
||||
TerraForgePlugin.getInstance().logger().info("Creating generator for config pack " + pack.getTemplate().getID());
|
||||
return new ForgeChunkGeneratorWrapper(new TerraBiomeSource(biomeRegistry, seed, pack), seed, pack);
|
||||
Reference in New Issue
Block a user