-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib events, custom events, custom items demp code
- Loading branch information
1 parent
33d1a71
commit b3752f2
Showing
5 changed files
with
193 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//This mod also requires lib.customitems.js | ||
ModAPI.addEventListener("lib:libcustomitems:loaded", () => { | ||
console.log("Registered my cool custom item."); | ||
LibCustomItems.registerItem({ | ||
tag: "mymod:test_item_1", | ||
base: "magma_cream", | ||
name: "Custom Item", | ||
qty: 32, | ||
recipe: [ | ||
"###", | ||
"# #", | ||
"###" | ||
], | ||
recipeLegend: { | ||
"#": { | ||
"type": "block", | ||
"id": "dirt" | ||
} | ||
}, | ||
onRightClickGround: `/*/user, world, itemstack, blockpos/*/ | ||
itemstack.stackSize -= 1; | ||
if (itemstack.stackSize < 1) { | ||
user.inventory.mainInventory[user.inventory.currentItem] = null; | ||
} | ||
user.setHealth(2); | ||
` | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,85 @@ | ||
// Library to make adding custom items with EaglerForgeInjector much easier. | ||
(function LibItems() { | ||
ModAPI.events.newEvent() | ||
ModAPI.events.newEvent("lib:libcustomitems:loaded"); | ||
function libServerside() { | ||
globalThis.LCI_REGISTRY ||= []; | ||
globalThis.LCI_ACTIONREGISTRY ||= {}; | ||
var useName = ModAPI.util.getMethodFromPackage("net.minecraft.network.NetHandlerPlayServer", "processPlayerBlockPlacement"); | ||
var oldUse = ModAPI.hooks.methods[useName]; | ||
ModAPI.hooks.methods[useName] = function ($this, packet) { | ||
if ($this?.$playerEntity?.$inventory && $this.$playerEntity.$inventory.$getCurrentItem()) { | ||
var item = $this.$playerEntity.$inventory.$getCurrentItem(); | ||
if (item.$stackTagCompound && item.$stackTagCompound.$hasKey(ModAPI.util.str("display"), 10)) { | ||
var displayTag = item.$stackTagCompound.$getCompoundTag(ModAPI.util.str("display")); | ||
if (displayTag.$hasKey(ModAPI.util.str("Lore"), 9)) { | ||
var loreTag = displayTag.$getTag(ModAPI.util.str("Lore")); | ||
if (loreTag.$tagCount() > 0 && globalThis.LCI_REGISTRY.includes(ModAPI.util.ustr(loreTag.$getStringTagAt(0)))) { | ||
var cid = loreTag.$getStringTagAt(0); | ||
var positionTag = Object.keys(packet).filter(x => { return x.startsWith("$position") })[0]; | ||
if (packet[positionTag].$x === -1 && packet[positionTag].$y === -1 && packet[positionTag].$z === -1) { | ||
return 0; | ||
} | ||
globalThis.LCI_ACTIONREGISTRY[cid].call(globalThis, | ||
new Proxy($this.$playerEntity, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf), | ||
new Proxy($this.$serverController.$worldServerForDimension($this.$playerEntity.$dimension), ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf), | ||
new Proxy(item, ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf), | ||
new Proxy(packet[positionTag], ModAPI.util.TeaVM_to_Recursive_BaseData_ProxyConf)); | ||
return 0; | ||
} | ||
} | ||
} | ||
} | ||
return oldUse.apply(this, [$this, packet]); | ||
} | ||
} | ||
function LCI_registerItem(data) { | ||
globalThis.LCI_REGISTRY ||= []; | ||
globalThis.LCI_ACTIONREGISTRY ||= {}; | ||
globalThis.LCI_REGISTRY.push(data.tag); | ||
globalThis.LCI_ACTIONREGISTRY[data.tag] = new Function("user", "world", "itemstack", "blockpos", data.onRightClickGround); | ||
var ObjectClass = ModAPI.reflect.getClassById("java.lang.Object").class; | ||
function ToChar(char) { | ||
return ModAPI.reflect.getClassById("java.lang.Character").staticMethods.valueOf.method(char[0].charCodeAt(0)); | ||
} | ||
ModAPI.dedicatedServer.appendCode(`(function () { | ||
function handler() { | ||
LCI_registerItem(${JSON.stringify(data)}); | ||
ModAPI.removeEventListener("tick", handler); | ||
} | ||
ModAPI.addEventListener("tick", handler); | ||
})()`); | ||
var recipeInternal = []; | ||
Object.keys(data.recipeLegend).forEach((key) => { | ||
recipeInternal.push(ToChar(key)); | ||
var ingredient = null; | ||
var schema = data.recipeLegend[key]; | ||
if (schema.type === "block") { | ||
ingredient = ModAPI.blocks[schema.id].getRef(); | ||
} else { | ||
ingredient = ModAPI.items[schema.id].getRef(); | ||
} | ||
recipeInternal.push(ingredient); | ||
}); | ||
var recipeContents = data.recipe.flatMap(x => { return ModAPI.util.str(x) }); | ||
var recipe = ModAPI.util.makeArray(ObjectClass, recipeContents.concat(recipeInternal)); | ||
|
||
var testItem = ModAPI.reflect.getClassById("net.minecraft.item.ItemStack").constructors[4](ModAPI.items[data.base].getRef(), data.qty); | ||
testItem.$stackTagCompound = ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagCompound").constructors[0](); | ||
testItem.$stackTagCompound.$setTag(ModAPI.util.str("display"), ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagCompound").constructors[0]()); | ||
var displayTag = testItem.$stackTagCompound.$getCompoundTag(ModAPI.util.str("display")); | ||
displayTag.$setString(ModAPI.util.str("Name"), ModAPI.util.str(data.name)); | ||
var lore = ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagList").constructors[0](); | ||
lore.$appendTag(ModAPI.reflect.getClassById("net.minecraft.nbt.NBTTagString").constructors.filter(x => { return x.length === 1 })[0](ModAPI.util.str(data.tag))); | ||
displayTag.$setTag(ModAPI.util.str("Lore"), lore); | ||
|
||
var craftingManager = ModAPI.reflect.getClassById("net.minecraft.item.crafting.CraftingManager").staticMethods.getInstance.method(); | ||
ModAPI.hooks.methods.nmic_CraftingManager_addRecipe(craftingManager, testItem, recipe); | ||
} | ||
ModAPI.dedicatedServer.appendCode(libServerside); | ||
ModAPI.dedicatedServer.appendCode("globalThis.LCI_registerItem = " + LCI_registerItem.toString()); | ||
window.LibCustomItems = {}; | ||
LibCustomItems.registerItem = function register(data) { | ||
LCI_registerItem(data); | ||
} | ||
ModAPI.events.callEvent("lib:libcustomitems:loaded", {}); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters