mirror of
https://github.com/MineLittlePony/MineLittlePony.git
synced 2025-02-13 08:14:23 +01:00
Implement boiletplate for loading by forge
This commit is contained in:
parent
003a6547bf
commit
8589d2d93a
2 changed files with 134 additions and 0 deletions
62
src/fml/java/com/minelittlepony/client/Config.java
Normal file
62
src/fml/java/com/minelittlepony/client/Config.java
Normal file
|
@ -0,0 +1,62 @@
|
|||
package com.minelittlepony.client;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import com.minelittlepony.client.settings.ClientPonyConfig;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
|
||||
class Config extends ClientPonyConfig {
|
||||
static final Gson gson = new GsonBuilder()
|
||||
.setPrettyPrinting()
|
||||
.excludeFieldsWithoutExposeAnnotation()
|
||||
.create();
|
||||
|
||||
private final File configFile;
|
||||
|
||||
Config(File file) {
|
||||
configFile = file;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
if (configFile.exists()) {
|
||||
configFile.delete();
|
||||
}
|
||||
|
||||
try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(new FileOutputStream(configFile)))) {
|
||||
writer.setIndent(" ");
|
||||
|
||||
gson.toJson(this, Config.class, writer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
static Config of(File file) {
|
||||
Config result = null;
|
||||
|
||||
if (file.exists()) {
|
||||
try (FileInputStream s = new FileInputStream(file)) {
|
||||
result = gson.fromJson(new JsonReader(new InputStreamReader(s)), Config.class);
|
||||
} catch (IOException ignored) {
|
||||
result = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (result == null) {
|
||||
result = new Config(file);
|
||||
}
|
||||
|
||||
result.save();
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.minelittlepony.client;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.renderer.entity.Render;
|
||||
import net.minecraft.client.renderer.tileentity.TileEntityRenderer;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
|
||||
import net.minecraftforge.fml.client.registry.RenderingRegistry;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.fml.common.gameevent.TickEvent.ClientTickEvent;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
|
||||
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
|
||||
|
||||
import com.minelittlepony.client.IModUtilities;
|
||||
import com.minelittlepony.client.MineLPClient;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@Mod("minelittlepony")
|
||||
public class ForgeModMineLittlePony implements IModUtilities {
|
||||
|
||||
private final MineLPClient mlp = new MineLPClient(this);
|
||||
|
||||
public ForgeModMineLittlePony() {
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::init);
|
||||
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::posInit);
|
||||
MinecraftForge.EVENT_BUS.register(this);
|
||||
}
|
||||
|
||||
private void init(final FMLCommonSetupEvent event) {
|
||||
|
||||
// TODO: I don't know what forge did with `event.getModConfigurationDirectory()` but it's not where it used to be.
|
||||
|
||||
File configDirectory = new File(Minecraft.getInstance().getFileResourcePacks().getParentFile(), "config");
|
||||
File configFile = new File(configDirectory, "minelittlepony.json");
|
||||
|
||||
mlp.init(Config.of(configFile));
|
||||
}
|
||||
|
||||
private void posInit(FMLClientSetupEvent event) {
|
||||
mlp.postInit(event.getMinecraftSupplier().get());
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
public void onClientTick(ClientTickEvent event) {
|
||||
mlp.onTick(Minecraft.getInstance(), Minecraft.getInstance().world != null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends TileEntity> void addRenderer(Class<T> type, TileEntityRenderer<T> renderer) {
|
||||
ClientRegistry.bindTileEntitySpecialRenderer(type, renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Entity> void addRenderer(Class<T> type, Render<T> renderer) {
|
||||
RenderingRegistry.registerEntityRenderingHandler(type, rm -> renderer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasFml() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getRenderPartialTicks() {
|
||||
return Minecraft.getInstance().getRenderPartialTicks();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue