EaglerForge/sources/main/java/net/eaglerforge/api/ModAPI.java

276 lines
11 KiB
Java
Raw Normal View History

2024-02-10 23:54:25 -06:00
package net.eaglerforge.api;
2024-03-02 00:27:15 -06:00
import net.eaglerforge.gui.EmptyGui;
2024-02-15 06:00:47 -06:00
import net.eaglerforge.gui.ModGUI;
2024-02-10 23:54:25 -06:00
import net.lax1dude.eaglercraft.v1_8.log4j.LogManager;
import net.lax1dude.eaglercraft.v1_8.log4j.Logger;
2024-02-11 03:43:45 -06:00
import net.minecraft.client.ClientBrandRetriever;
2024-02-10 23:54:25 -06:00
import net.minecraft.client.Minecraft;
2024-03-02 02:57:12 -06:00
import net.minecraft.client.gui.ScaledResolution;
2024-03-06 03:25:44 -06:00
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityTNTPrimed;
2024-02-11 03:43:45 -06:00
import net.minecraft.util.ChatComponentText;
2024-02-20 00:28:56 -06:00
import me.otterdev.UwUAPI;
2024-02-10 23:54:25 -06:00
import org.teavm.jso.JSBody;
import org.teavm.jso.JSObject;
2024-02-20 00:28:56 -06:00
import net.eaglerforge.EaglerForge;
2024-02-23 03:00:17 -06:00
import net.minecraft.enchantment.Enchantment;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.block.material.Material;
2024-02-10 23:54:25 -06:00
2024-02-11 03:43:45 -06:00
import java.util.ArrayList;
2024-02-10 23:54:25 -06:00
import static net.lax1dude.eaglercraft.v1_8.EaglercraftVersion.projectForkVersion;
2024-02-22 07:09:44 -06:00
import static net.minecraft.client.Minecraft.getDebugFPS;
import net.minecraft.client.gui.Gui;
2024-02-10 23:54:25 -06:00
public class ModAPI {
2024-02-22 07:09:44 -06:00
2024-02-10 23:54:25 -06:00
private static Minecraft mc;
2024-03-02 02:57:12 -06:00
private static ScaledResolution sr;
2024-03-02 00:27:15 -06:00
private static Gui gui;
2024-02-11 03:43:45 -06:00
public ArrayList<String> requiredList;
2024-02-10 23:54:25 -06:00
public static final Logger log = LogManager.getLogger();
public static String version = projectForkVersion;
2024-02-11 03:43:45 -06:00
public static boolean clientPacketSendEventsEnabled = true;
2024-02-13 04:19:11 -06:00
@JSBody(params = { "version" }, script = "initAPI(version)")
2024-02-10 23:54:25 -06:00
public static native void initAPI(String version);
2024-02-11 03:43:45 -06:00
@JSBody(params = { "name" }, script = "ModAPI.events.newEvent(name);")
private static native void newEvent(String name);
@JSBody(params = { "name", "data" }, script = "ModAPI.events.callEvent(name, data); return data;")
public static native BaseData callEvent(String name, BaseData data);
@JSBody(params = { "name", "data" }, script = "ModAPI[name]=data;")
public static native void setGlobal(String name, BaseData data);
@JSBody(params = { "name" }, script = "return ModAPI[name] || {};")
public static native BaseData getGlobal(String name);
@JSBody(params = {}, script = "return ModAPI;")
public static native BaseData getModAPI();
@JSBody(params = { "data" }, script = "console.log(data);")
public static native void logJSObj(JSObject data);
public void onGlobalUpdated(String global) {
// logger.info("Global update request: "+global);
BaseData data = getGlobal(global);
// logJSObj(data);
if (data == null) {
return;
}
2024-02-23 03:00:17 -06:00
switch (global) {
case "player":
mc.thePlayer.loadModData(data);
}
2024-02-11 03:43:45 -06:00
}
public void onRequire(String global) {
if (!requiredList.contains(global)) {
log.info("Required global: " + global);
requiredList.add(global);
}
}
2024-02-10 23:54:25 -06:00
public ModAPI(Minecraft mcIn) {
this.mc = mcIn;
2024-02-11 03:43:45 -06:00
requiredList = new ArrayList<String>();
2024-02-10 23:54:25 -06:00
initAPI(version);
2024-02-19 04:15:45 -06:00
newEvent("load");
newEvent("gui");
2024-02-20 00:28:56 -06:00
newEvent("drawhud");
2024-02-22 07:09:44 -06:00
newEvent("key");
2024-02-23 03:00:17 -06:00
newEvent("postmotionupdate");
newEvent("motionupdate");
newEvent("premotionupdate");
2024-02-19 04:15:45 -06:00
2024-02-15 13:15:52 -06:00
newEvent("update");
2024-02-23 03:00:17 -06:00
/*newEvent("packetjoingame");
newEvent("packetspawnobject");
newEvent("packetspawnxporb");
newEvent("packetspawnglobalentity");
newEvent("packetspawnpainting");
newEvent("packetentityvelocity");
newEvent("packetentitymetadata");
newEvent("packetspawnplayer");
newEvent("packetentityteleport");
newEvent("packethelditemchange");
newEvent("packetentity");
newEvent("packetentityheadlook");
newEvent("packetdestroyentities");
newEvent("packetplayerposlook");
newEvent("packetmultiblockchange");
newEvent("packetchunkdata");
newEvent("packetblockchange");
newEvent("packetdisconnect");
newEvent("packetcollectitem");
newEvent("packetchat");
newEvent("packetanimation");
newEvent("packetusebed");
newEvent("packetspawnmob");
newEvent("packettimeupdate");
newEvent("packetspawnposition");
newEvent("packetentityattatch");
newEvent("packetentitystatus");
newEvent("packetupdatehealth");
newEvent("packetsetxp");
newEvent("packetrespawn");
newEvent("packetexplosion");
newEvent("packetopenwindow");
newEvent("packetsetslot");
newEvent("packetconfirmtransaction");
newEvent("packetwindowitems");
newEvent("packetsigneditoropen");
newEvent("packetupdatesign");
newEvent("packetupdatetileentity");
newEvent("packetwindowproperty");
newEvent("packetentityequipment");
newEvent("packetclosewindow");
newEvent("packetblockaction");
newEvent("packetblockbreakanim");
newEvent("packetmapchunkbulk");
newEvent("packetchangegamestate");
newEvent("packetmaps");
newEvent("packeteffect");
newEvent("packetstatistics");
newEvent("packetentityeffect");
newEvent("packetcombatevent");
newEvent("packetserverdifficulty");
newEvent("packetcamera");
newEvent("packetworldborder");
newEvent("packettitle");
newEvent("packetsetcompressionlevel");
newEvent("packetplayerlistheaderfooter");
newEvent("packetremoveentityeffect");
newEvent("packetplayerlistitem");
newEvent("packetkeepalive");
newEvent("packetplayerabilities");
newEvent("packettabcomplete");
newEvent("packetsoundeffect");
newEvent("packetresourcepack");
newEvent("packetupdateentitynbt");
newEvent("packetcustompayload");
newEvent("packetscoreboardobjective");
newEvent("packetupdatescore");
newEvent("packetdisplayscoreboard");
newEvent("packetteams");
newEvent("packetparticles");
newEvent("packetentityproperties");
newEvent("sendpacketanimation");
newEvent("sendpacketentityaction");
newEvent("sendpacketinput");
newEvent("sendpacketclosewindow");
newEvent("sendpacketclickwindow");
newEvent("sendpacketconfirmtransaction");
newEvent("sendpacketkeepalive");
newEvent("sendpacketchatmessage");
newEvent("sendpacketuseentity");
newEvent("sendpacketplayer");
newEvent("sendpacketplayerposition");
newEvent("sendpacketplayerlook");
newEvent("sendpacketplayerposlook");
newEvent("sendpacketplayerdigging");
newEvent("sendpacketplayerblockplacement");
newEvent("sendpackethelditemchange");
newEvent("sendpacketcreativeinventoryaction");
newEvent("sendpacketenchantitem");
newEvent("sendpacketupdatesign");
newEvent("sendpacketplayerabilities");
newEvent("sendpackettabcomplete");
newEvent("sendpacketclientsettings");
newEvent("sendpacketclientstatus");
newEvent("sendpacketcustompayload");
newEvent("sendpacketspectate");
newEvent("sendpacketresourcepackstatus");*/
2024-03-06 06:11:37 -06:00
newEvent("packetchat");
2024-02-15 13:15:52 -06:00
globalsFunctor(this);
globalsRequireFunctor(this);
globalsUpdateFunctor(this);
2024-02-11 03:43:45 -06:00
getModAPI().setCallbackVoidWithDataArg("displayToChat", (BaseData params) -> {
mc.ingameGUI.getChatGUI().printChatMessage(new ChatComponentText(params.getString("msg")));
});
2024-02-16 10:43:17 -06:00
getModAPI().setCallbackStringWithDataArg("uwuify", (BaseData params) -> {
return UwUAPI.uwuify(params.getString("string"));
});
2024-02-11 03:43:45 -06:00
getModAPI().setCallbackVoid("clickMouse", () -> {
mc.clickMouse();
});
getModAPI().setCallbackVoid("rightClickMouse", () -> {
mc.rightClickMouse();
});
2024-03-02 00:27:15 -06:00
getModAPI().setCallbackInt("getFPS", () -> {
return getDebugFPS();
2024-02-22 07:09:44 -06:00
});
2024-02-11 03:43:45 -06:00
getModAPI().set("clientBrand", ClientBrandRetriever.getClientModName());
2024-02-23 03:00:17 -06:00
setGlobal("enchantments", Enchantment.makeModDataStatic());
setGlobal("blocks", Blocks.makeModData());
setGlobal("items", Items.makeModData());
setGlobal("materials", Material.makeModDataStatic());
2024-02-11 03:43:45 -06:00
setGlobal("mcinstance", mc);
2024-02-16 10:43:17 -06:00
setGlobal("platform", PlatformAPI.makeModData());
setGlobal("logger", LoggerAPI.makeModData());
2024-03-02 00:27:15 -06:00
setGlobal("emptygui", EmptyGui.makeModData());
2024-03-02 02:57:12 -06:00
setGlobal("ScaledResolution", ScaledResolution.makeModData());
getModAPI().setCallbackString("currentScreen", () -> {
return mc.currentScreen.toString();
});
2024-02-20 00:28:56 -06:00
getModAPI().setCallbackInt("getdisplayHeight", () -> {
return mc.displayHeight;
});
getModAPI().setCallbackInt("getdisplayWidth", () -> {
return mc.displayWidth;
});
2024-03-02 02:57:12 -06:00
getModAPI().setCallbackInt("getFONT_HEIGHT", () -> {
return mc.fontRendererObj.FONT_HEIGHT;
});
2024-02-20 00:28:56 -06:00
getModAPI().setCallbackVoidWithDataArg("drawStringWithShadow", (BaseData params) -> {
mc.fontRendererObj.drawStringWithShadow(params.getString("msg"), params.getFloat("x"), params.getFloat("y"), params.getInt("color"));
});
getModAPI().setCallbackVoidWithDataArg("drawString", (BaseData params) -> {
mc.fontRendererObj.drawString(params.getString("msg"), params.getFloat("x"), params.getFloat("y"), params.getInt("color"), false);
});
2024-02-22 07:09:44 -06:00
getModAPI().setCallbackVoidWithDataArg("drawRect", (BaseData params) -> {
2024-03-02 00:27:15 -06:00
gui.drawRect(params.getInt("left"), params.getInt("top"), params.getInt("right"), params.getInt("bottom"), params.getInt("color"));
2024-02-22 07:09:44 -06:00
});
2024-02-15 06:00:47 -06:00
ModGUI.loadFont();
2024-02-11 03:43:45 -06:00
}
static void globalsFunctor(ModAPI modAPI) {
GlobalsListener.provideCallback((String name) -> {
modAPI.onGlobalUpdated(name);
});
}
static void globalsRequireFunctor(ModAPI modAPI) {
GlobalsListener.provideRequireCallback((String name) -> {
modAPI.onRequire(name);
});
}
static void globalsUpdateFunctor(ModAPI modAPI) {
GlobalsListener.provideUpdateCallback(() -> {
modAPI.onUpdate();
});
}
2024-02-22 07:09:44 -06:00
2024-02-11 03:43:45 -06:00
public void onUpdate() {
2024-02-23 03:00:17 -06:00
if (requiredList.contains("player") && mc.thePlayer != null) {
ModAPI.setGlobal("player", mc.thePlayer.makeModData());
}
/* if (requiredList.contains("network") && mc.thePlayer != null && mc.thePlayer.sendQueue != null) {
ModAPI.setGlobal("network", mc.thePlayer.sendQueue.makeModData());
}*/
if (requiredList.contains("settings") && mc.gameSettings != null) {
ModAPI.setGlobal("settings", mc.gameSettings.makeModData());
}
2024-02-15 13:15:52 -06:00
ModAPI.callEvent("update", new ModData());
2024-02-10 23:54:25 -06:00
}
}