your current location:首页 > news>Usage that reflects in Kubejs [kjs] Kubejs Minecraft Game

Usage that reflects in Kubejs [kjs] Kubejs Minecraft Game

2024-12-10 17:58:57|Myriagame |source:minecraft skins

This tutorial is set by the author to use the CC By-NC-ND protocol.

Basic grammar

Reflection is an important feature of Kubejs. It allows you to access some safe Java classes to use the function of more than Kubejs.

Use the following statement to obtain a class

 const shield = java.loadClass ("net.minecraft.World.Item.shielditem"); 

This class gets an original item class, corresponding to the category of shields in the game.

Some classes cannot be reflected and obtained, such as

 // Reflexes get java.io.file. File reads and write generally requires this class

const file = java.loadClass ('java.io.file')

// The output result of the log

[12:59:55] [Error]! Economic.js#1: Failed to load java class 'java.io.file': class is not allowed by class file!

Class Filter determines which classes can be reflected, and the classes that are not allowed often have the ability to make the integrated package become virus software.However, most categories related to game content can be used normally.

Class can directly call its static method

 const $ keymappingregization = java.loadClass (

"Dev.architectable.regization.client.Keymappings.Keymappingregization" "

);

$ KeymappingRegistry.register (global.testkey)

You can also use new to get an object

 const prop = java.loadClass ("net.mineCraft.World.Item.item $ Properties");

const defandroperties = new prop ();

It can be found that the class obtained by reflection is almost the same as the class in Java. When used, just imitate the Java method.More accurate, it is the development of imitating Forge or Neoforge module development and using the development of the Architectury API.

When using Probejs

Each class must be loaded at least once to be recognized by Probejs and generates the required files required for automatic completion.

The class with automatic completion can be reflected

If not, run the following instructions according to the location of the script after reflection, refresh the script

/kubejs relaxed client_scripts

/kubejs recoad server_scripts

/kubejs regoad startup_scripts

Run this command and let Probejs generate the file required for this class

/Probejs dump 

Example: Register almost the same items

The registration incident provided by Kubejs cannot register such as shields, bows, tridents and other items.Using reflection can register almost the same items, and can also customize some attributes.

 // The original shield class

const shield = java.loadclass ("net.minecraft.World.Item.shieldItem"); // Item attribute category

const prop = java.loadclass ("net.minecraft.World.Item.Item $ Properties");

const defandroperties = new prop ();

Startupevents.regization ("item", (event) => {{{{

// Set the attributes of the item, the durability is 1024, and the scarcity is EPIC (for example, the scarcity of the enchanting gold apple is EPIC, and its name is purple)

Defproperties.defaultDurability (1024);

Defproperties.rarity ("EPIC");

Event.createcustom (name, () => {

Let item = new shield (defproperties);

Return item;

});

});

The point is this callback function.We can roughly understand that each time the game needs this item, the backing function must return an instance (object) of an item class.