Skip to content

Commit

Permalink
integration -> addon and fix a few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Noojuno committed Jul 10, 2020
1 parent c1964f2 commit 8669fd3
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 83 deletions.
14 changes: 4 additions & 10 deletions src/main/java/co/runed/multicharacter/MultiCharacterMod.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package co.runed.multicharacter;

import co.runed.multicharacter.api.IMultiCharacterIntegration;
import co.runed.multicharacter.addons.minecraft.MinecraftAddon;
import co.runed.multicharacter.api.MultiCharacterAPI;
import co.runed.multicharacter.character.CharacterManager;
import co.runed.multicharacter.integration.minecraft.MinecraftIntegration;
import co.runed.multicharacter.proxy.CommonProxy;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
Expand Down Expand Up @@ -38,7 +37,9 @@ public void preInit(FMLPreInitializationEvent event)
logger = event.getModLog();

this.characterManager = new CharacterManager();
MultiCharacterAPI.addIntegration(new MinecraftIntegration());
MultiCharacterAPI.addAddon(new MinecraftAddon());

MultiCharacterAPI.addAddonIfModLoaded("moreplayermodels", "co.runed.multicharacter.addons.mpm.MPMAddon");

proxy.preInit();
}
Expand All @@ -53,13 +54,6 @@ public void init(FMLInitializationEvent event)
public void postInit(FMLPostInitializationEvent event)
{
proxy.postInit();

MultiCharacterAPI.addIntegrationIfModLoaded("moreplayermodels", "co.runed.multicharacter.integration.mpm.MPMIntegration");

for (IMultiCharacterIntegration integration : MultiCharacterAPI.getIntegrations())
{
integration.init();
}
}

public static MultiCharacterMod getInstance()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package co.runed.multicharacter.integration.minecraft;
package co.runed.multicharacter.addons.minecraft;

import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package co.runed.multicharacter.integration.minecraft;
package co.runed.multicharacter.addons.minecraft;

import co.runed.multicharacter.MultiCharacterMod;
import co.runed.multicharacter.api.IMultiCharacterIntegration;
import co.runed.multicharacter.api.Addon;
import co.runed.multicharacter.api.MultiCharacterAPI;
import co.runed.multicharacter.character.Character;
import co.runed.multicharacter.character.CharacterManager;
Expand All @@ -22,7 +22,7 @@
import java.io.FileInputStream;
import java.io.IOException;

public class MinecraftIntegration implements IMultiCharacterIntegration
public class MinecraftAddon extends Addon
{
public static final String PLAYER_DATA_NBT_KEY = "PlayerData";

Expand All @@ -32,12 +32,6 @@ public void init()

}

@Override
public void clientInit()
{

}

@Override
public void onPreLoad(EntityPlayer player)
{
Expand Down Expand Up @@ -139,7 +133,7 @@ public void onSave(EntityPlayer player)
{
// WRITE PLAYER.DAT NBT TO CHARACTER NBT
NBTTagCompound nbt = activeCharacter.getNbt();
nbt.setTag(MinecraftIntegration.PLAYER_DATA_NBT_KEY, player.writeToNBT(new NBTTagCompound()));
nbt.setTag(MinecraftAddon.PLAYER_DATA_NBT_KEY, player.writeToNBT(new NBTTagCompound()));

activeCharacter.setNbt(nbt);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,28 @@
package co.runed.multicharacter.integration.mpm;
package co.runed.multicharacter.addons.mpm;

import co.runed.multicharacter.MultiCharacterMod;
import co.runed.multicharacter.api.IMultiCharacterIntegration;
import co.runed.multicharacter.addons.mpm.packets.CPacketUpdateMPM;
import co.runed.multicharacter.addons.mpm.packets.SPacketSaveMPM;
import co.runed.multicharacter.api.Addon;
import co.runed.multicharacter.character.Character;
import co.runed.multicharacter.character.CharacterManager;
import co.runed.multicharacter.network.PacketDispatcher;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.relauncher.Side;
import noppes.mpm.ModelData;

public class MPMIntegration implements IMultiCharacterIntegration
public class MPMAddon extends Addon
{
public static final String NBT_DATA_KEY = "MorePlayerModels";

@Override
public void init()
{
this.setClientAddon(new MPMClientAddon());

PacketDispatcher.registerPacket(CPacketUpdateMPM.Handler.class, CPacketUpdateMPM.class, Side.CLIENT);
PacketDispatcher.registerPacket(SPacketSaveMPM.Handler.class, SPacketSaveMPM.class, Side.SERVER);

MinecraftForge.EVENT_BUS.register(this);
}

@Override
public void clientInit()
{
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package co.runed.multicharacter.addons.mpm;

import co.runed.multicharacter.addons.mpm.packets.SPacketSaveMPM;
import co.runed.multicharacter.api.ClientAddon;
import co.runed.multicharacter.events.client.GuiCloseEvent;
import co.runed.multicharacter.network.PacketDispatcher;
import net.minecraft.client.gui.GuiScreen;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import noppes.mpm.client.gui.GuiMPM;

public class MPMClientAddon extends ClientAddon
{
@Override
public void init()
{
MinecraftForge.EVENT_BUS.register(this);
}

@SubscribeEvent
public void onCloseGui(GuiCloseEvent event)
{
GuiScreen gui = event.getGui();

// TODO: MOVE TO MPM INTEGRATION
if (gui != null && gui.getClass() == GuiMPM.class)
{
PacketDispatcher.sendToServer(new SPacketSaveMPM(((GuiMPM) gui).playerdata));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.runed.multicharacter.integration.mpm;
package co.runed.multicharacter.addons.mpm;

import co.runed.multicharacter.MultiCharacterMod;
import co.runed.multicharacter.addons.mpm.packets.CPacketUpdateMPM;
import co.runed.multicharacter.character.Character;
import co.runed.multicharacter.network.PacketDispatcher;
import co.runed.multicharacter.util.Scheduler;
Expand Down Expand Up @@ -37,7 +38,7 @@ public static ModelData setModelData(EntityPlayer player, ModelData modelData)
if (character != null)
{
NBTTagCompound nbt = character.getNbt();
nbt.setTag(MPMIntegration.NBT_DATA_KEY, data.writeToNBT());
nbt.setTag(MPMAddon.NBT_DATA_KEY, data.writeToNBT());
character.setNbt(nbt);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.runed.multicharacter.integration.mpm;
package co.runed.multicharacter.addons.mpm.packets;

import co.runed.multicharacter.addons.mpm.MPMUtil;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.NBTTagCompound;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package co.runed.multicharacter.integration.mpm;
package co.runed.multicharacter.addons.mpm.packets;

import co.runed.multicharacter.MultiCharacterMod;
import co.runed.multicharacter.addons.mpm.MPMUtil;
import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.ByteBufUtils;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/co/runed/multicharacter/api/Addon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package co.runed.multicharacter.api;

public abstract class Addon implements IAddon
{
IClientAddon clientAddon;

@Override
public IClientAddon getClientAddon()
{
return this.clientAddon;
}

@Override
public void setClientAddon(IClientAddon clientAddon)
{
this.clientAddon = clientAddon;
}
}
5 changes: 5 additions & 0 deletions src/main/java/co/runed/multicharacter/api/ClientAddon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package co.runed.multicharacter.api;

public abstract class ClientAddon implements IClientAddon
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import co.runed.multicharacter.character.Character;
import net.minecraft.entity.player.EntityPlayer;

public interface IMultiCharacterIntegration
public interface IAddon
{
void init();

void clientInit();
IClientAddon getClientAddon();

void setClientAddon(IClientAddon clientAddon);

/**
* Runs before a players characters are loaded
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/co/runed/multicharacter/api/IClientAddon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package co.runed.multicharacter.api;

public interface IClientAddon
{
void init();
}
34 changes: 27 additions & 7 deletions src/main/java/co/runed/multicharacter/api/MultiCharacterAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.common.Loader;
import net.minecraftforge.fml.relauncher.Side;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class MultiCharacterAPI
{
private static final List<IMultiCharacterIntegration> modIntegrations = new ArrayList<>();
private static final List<IAddon> addons = new ArrayList<>();

public static File getCharacterDirectory(EntityPlayer player)
{
Expand All @@ -28,18 +29,37 @@ public static File getCharacterDirectory(EntityPlayer player)
return dir;
}

public static void addIntegration(IMultiCharacterIntegration integration)
public static void initAddons()
{
modIntegrations.add(integration);
for (IAddon addon : addons)
{
addon.init();
}

if (FMLCommonHandler.instance().getEffectiveSide() == Side.CLIENT)
{
for (IAddon addon : addons)
{
if (addon.getClientAddon() != null)
{
addon.getClientAddon().init();
}
}
}
}

public static void addAddon(IAddon addon)
{
addons.add(addon);
}

public static void addIntegrationIfModLoaded(String requiredMod, String className)
public static void addAddonIfModLoaded(String requiredMod, String className)
{
if (Loader.isModLoaded(requiredMod))
{
try
{
addIntegration(Class.forName(className).asSubclass(IMultiCharacterIntegration.class).newInstance());
addAddon(Class.forName(className).asSubclass(IAddon.class).newInstance());
}
catch (InstantiationException | IllegalAccessException | ClassNotFoundException e)
{
Expand All @@ -48,8 +68,8 @@ public static void addIntegrationIfModLoaded(String requiredMod, String classNam
}
}

public static List<IMultiCharacterIntegration> getIntegrations()
public static List<IAddon> getAddons()
{
return modIntegrations;
return addons;
}
}
Loading

0 comments on commit 8669fd3

Please sign in to comment.