your current location:首页 > news>1.18 Mixin used by modules and simplicity function implementation [Forge] Minecraft Forge Minecraft Game

1.18 Mixin used by modules and simplicity function implementation [Forge] Minecraft Forge Minecraft Game

2024-12-10 17:51:54|Myriagame |source:minecraft skins

hint

Last tutorial: 1.18 Module development of the use and simple functional implementation of the converter.

Mixin use

Jian Shao

Mixin can modify the code of Minecraft without modifying the source file to achieve the effect of achieving custom functions.

use

The following is Mixin's configuration file:

 {{

"Required": true,

"Minversion": "0.8",

"Package": "CN.KSMCBRIGADE.EM.MIXIN",

"CompatibilityLevel": "java_8",, "java_8",

"Refmap": "Em.refmap.json",

"Mixins": [[

],,,

"Client": [[

],,,

"Injectors": {

"DefaultRequire": 1

}

}

Among them, the Mixin item represents Mixin that is injected in both end, and Client means only the mixin that is injected on the client. There is also one that is not displayed here, which is the server item.The end is injected.

First create a class in the directory specified by the Package item. Here is a block class as an example:

 Package cn.ksmcbrigade.em.mixin;

import network.minecraft.World.Level.block.block;

import org.spongepowered.asm.mixin.mixin;

@Mixin (block.class)

public class blockmixin {

}

The Mixin annotation here represents the injected class. Mixin injection has lifted the above writing, and there are the following ways:

@mixin ({block.class}) 
@mixin (targets = {"net.minCraft.World.Level.block.block "}) 

Then inject the ShoulDrenderface function in the Block class:

@inject (method = "shouldRenderface", at = @AT ("Return")

Private Static Void Renderface (Blockstate P_152445_, Blockgetter P_152446_, Blockpos P_152447_, Direction P_152448_, BLOCKPOS P_152449_, CallB ACKINFORETURNABLE CIR) {

}

The inject annotation here is injected. Method is an injected function, AT is the location. In addition to Return (when returning), you can also fill in TAIL (end), head), Invoke_assign (after the execution of the function), InvokeBefore the function is executed), when the injected function is or , head will not be used. If used, it will report an error because the code is located in front of the super function.

If the injected function contains the STATIC modifier, it is necessary to write it as the form of the Private Static Void, and otherwise it is written in the form of Public Void.

Here, try to determine whether the square is a specified block, so it returns True, otherwise False is returned:

Private Static Block [] blocks = New Block [] {blocks.coal_ore, blocks.ir on_ore, blocks.gold_ore, blocks.diamond_ore, blocks.emerald_ore, blocks.d Eepslate_coal_ore, blocks.copper_ore, blocks.deepslate_diamond_ore, blocks.deepslate_lapis_ore, blocks.Lapis_ore};

@Inject (Method = "Shouldrenderface", at = @AT ("Return"), Cancellable = TRUE)

Private Static Void Renderface (Blockstate P_152445_, Blockgetter P_152446_, Blockpos P_152447_, Direction P_152448_, BLOCKPOS P_152449_, CallB ACKINFORETURNABLE CIR) {

If (arrays.stream (blocks) .tolist (). Contains (p_152445_.getBlock ())) {// if)

Cir.setreturnValue (true); // set

}

Else {

Cir.setreturnValue (false); // set

}

}

If you need to cancel or reset the return value, you need to add the words of Cancellable = True to the annotation, otherwise an error will be reported during runtime.

The Blocks array here is only used as a demonstration.

Finally remember to add Mixin to the configuration file:

 {{

"Required": true,

"Minversion": "0.8",

"Package": "CN.KSMCBRIGADE.EM.MIXIN",

"CompatibilityLevel": "java_8", "Refmap": "em.refmap.json",

"Mixins": [[

],,,

"Client": [[

"Blockmixin"

],,,

"Injectors": {

"DefaultRequire": 1

}

}

Simple functional implementation

X-ray

First create a class in the Module directory and inherit the Module class:

 Package cn.ksmcbrigade.em.modules;

Import cn.ksmcBrigade.em.module;

Import java.awt.event.keyevent;

Public Class XRay Extends Module {

Public xray () {

Super ("xray", keyevent.vk_x); // key x

}

}

Then create a Block -type empty variable array and functions used for acquisition, and determine whether the array is empty in the function.

 Public Static ArrayList  blocks = New ArrayList <> ();

public xray () {

Super ("xray", keyevent.vk_x); // key x

}

Public Static ArrayList get () {{) {

If (blocks.size () == 0) {{

Arrays.stream (blocks.class.getDeclaredfields ())

.Filter (f-> f.gettype (). Equals (block.class)) // Filter Block Type

.Filter (f-> modifier.isstatic (f.getmodifiers ())) // Filter Static .Filter (f-> f.getName (). Contains (). ")) // Filter Ore

.Tolist ()

.Foreach (f-> {

Try {

Blocks.add (f.get (null)); // Add to Arrays List

} Catch (iLlegalaccessException E) {{

E.printstacktrace ();

}

});

}

Return blocks;

}

Then judge whether the function enables the function and call the function in Blockmixin:

@inject (method = "shouldRenderface", at = @AT ("RETURN"), CancelLABLE = true) ckstate P_152445_, Blockgetter P_152446_, Blockpos P_152447_, Direction P_152448__152448_, Blockpos P_152449_, CallBackinForeturnable  CIR) {{

If (moduleManager.modulesclass.xray.enabled && xray.get (). Contains (p_152445_.getBlock ()) {) {) {) {) {

Cir.setRTURNVALUE (true);

}

Else if (moduleManager.modulesClass.xray.enabled) {{

Cir.setRTURNVALUE (false);

}

}

 Public Static Class ModulesClass {

Public static module nofall = new not ("not", keyevent.vk_n);

Public static module xyz = new xyz ("xyz", keyevent.vk_b);

Public static module timer = new timer (); // key y

Public module pegasus = new pegasus (); // key I

Public static module xray = new xray (); // key x

}

Boatfly

Jian Shao

This feature allows players to fly on the boat.

accomplish

First create a new class in the Module directory and inherit the Module, and then rewrite the Update function, and determine whether the player contains a mount, whether the mount is BOAT type, whether the space is pressed.0.3 grid.

 Package cn.ksmcbrigade.em.modules;

import cn.ksmcBrigade.em.module; Import net.minecraft.client.minecraft;

import network.minecraft.World.entity.entity;

Import network.minecraft.World.entity.entityType;

Import java.awt.event.keyevent;

Public Class Boatfly Extends Module {

Public boatfly () {

Super ("Boatfly", keyevent.vk_z); // Key Z

}

@Override

Public void update () {

Minecraft mc = minecraft.getInstance ();

If (mc.player == null) {// if player

Return;

}

If (mc.player.getvehicle () == null) {// if very

Return;

}

Entity very = mc.player.getvehicle ();

If (! Vehicle.gettype (). Equals (EntityType.Boat) {// if type

Return;

}

If (! Mc.options.Keyjump.isdown ()) {// if jump

Return;

}

Vehicle.SetDeltamovement (0,0.3,0); // set}

}

Fullbright

First create a new class in the Module directory and inherit the Module class, then save the GAMMA value when enabled, and set it to the preserved Gamma value after being disabled, and resume the Update function. The GAMMA value is constantly set in the Update function.

 Package cn.ksmcbrigade.em.modules;

Import cn.ksmcBrigade.em.module;

import network.minecraft.client.minecraft;

Import java.awt.event.keyevent;

Public Class Fullbright EXTENDS MODULE {

Public double gamma = 0.0d;

Public fullbright () {{)

Super ("Fullbright", keyevent.vk_c);

}

@Override

Public void enabled () {

This.gamma = minecraft.getinstance (). Options.gamma;

}

@Override

Public void disabled () {

Minecraft.getInstance (). Options.gamma = this.gamma;

}

@Override

Public void update () {

Minecraft.getInstance (). Options.gamma = 3000.0d;

}

}

Registration function

Finally, the registration function can

 Public Static Class ModulesClass {

Public Static Module NOFALL = New NOFALL ("NOFALL", keyevent.vk_n); Public Static Module xyz = New Xyz ("xyz", keyevent.vk_b);

Public static module timer = new timer (); // key y

Public module pegasus = new pegasus (); // key I

Public static module xray = new xray (); // key x

Public module boatfly = new boardfly (); // key z

Public static module fullbright = new fullbricht (); // key C

}

Then run the test.

Complete code

ModuleManager.modulesClass

 Public Static Class ModulesClass {

Public static module nofall = new not ("not", keyevent.vk_n);

Public static module xyz = new xyz ("xyz", keyevent.vk_b);

Public static module timer = new timer (); // key y

Public module pegasus = new pegasus (); // key I

Public static module xray = new xray (); // key x

Public module boatfly = new boardfly (); // key z

Public Static Module Fullbright = New Fullbright (); // Key C}

Boatfly.java

 Package cn.ksmcbrigade.em.modules;

Import cn.ksmcBrigade.em.module;

import network.minecraft.client.minecraft;

import network.minecraft.World.entity.entity;

Import network.minecraft.World.entity.entityType;

Import java.awt.event.keyevent;

Public Class Boatfly Extends Module {

Public boatfly () {

Super ("Boatfly", keyevent.vk_z); // Key Z

}

@Override

Public void update () {

Minecraft mc = minecraft.getInstance ();

If (mc.player == null) {

Return;

}

If (mc.player.getvehicle () == null) {

Return;

}

Entity very = mc.player.getvehicle ();

If (! Vehicle.gettype (). Equals (EntityType.Boat)) {

Return;

}

If (! Mc.options.keyjump.isdown ()) {{) {

Return;}

Very.setDeltamovement (0,0.3,0);

}

}

Xray.java

 Package cn.ksmcbrigade.em.modules;

Import cn.ksmcBrigade.em.module;

import network.minecraft.World.Level.block.block;

import network.minecraft.World.Level.block.blocks;

Import java.awt.event.keyevent;

Import java.util.arraylist;

Import java.util.arrays;

Import java.util.objects;

Public Class XRay Extends Module {

Public Static ArrayList blocks = New ArrayList <> ();

Public xray () {

Super ("xray", keyevent.vk_x); // key x

}

Public Static ArrayList get () {{) {

If (blocks.size () == 0) {{

Arrays.stream (blocks.class.getDeclaredfields ())

.Filter (f-> f.gettype (). Equals (block.class))

.Filter (f-> f.getName (). TOLOWERCASE (). Contains ("ol"))

.Tolist ()

.Foreach (f-> {try {

Blocks.add ((block) f.get (null);

} Catch (iLlegalaccessException E) {{

E.printstacktrace ();

}

});

}

Return blocks;

}

}

Fullbright.java

 Package cn.ksmcbrigade.em.modules;

Import cn.ksmcBrigade.em.module;

import network.minecraft.client.minecraft;

Import java.awt.event.keyevent;

Public Class Fullbright EXTENDS MODULE {

Public double gamma = 0.0d;

Public fullbright () {{)

Super ("Fullbright", keyevent.vk_c);}

@Override

Public void enabled () {

This.gamma = minecraft.getinstance (). Options.gamma;

}

@Override

Public void disabled () {

Minecraft.getInstance (). Options.gamma = this.gamma;

}

@Override

Public void update () {

Minecraft.getInstance (). Options.gamma = 3000.0d;

}

}

Blockmixin.java

 Package cn.ksmcbrigade.em.mixin;

Import cn.ksmcbrigade.em.moduleManager;

Import cn.ksmcbrigade.em.modules.xray;

import network.minecraft.core.blockpos;

Import network.minecraft.core.direction;

import network.minecraft.World.Level.blockgetter;

import network.minecraft.World.Level.block.block;

import network.minecraft.World.Level.block.State.blockState;

import org.spongepowered.asm.mixin.mixin;

import org.spongepowered.asm.mixin.injection.at;

Import org.spongepowered.asm.mixin.inject.inject;

Import org.spongepowered.asm.mixin.inject. CallBack.callBackinForeturnable;

@Mixin (block.class)

public Abstract class blockmixin {

@Inject (Method = "Shouldrenderface", at = @AT ("Return"), Cancellable = TRUE)

Private Static Void Renderface (Blockstate P_152445_, Blockgetter P_152446_, Blockpos P_152447_, Direction P_152448_, BLOCKPOS P_152449_, CallBacac kinforeturnable Cir) {if (moduleManager.modulesClass.xray.enabled &&Get (). Contains (p_152445_.getBlock ())) {{

Cir.setRTURNVALUE (true);

}

Else if (moduleManager.modulesClass.xray.enabled) {{

Cir.setRTURNVALUE (false);

}

}

}

modid.mixins.json

 {{

"Required": true,

"Minversion": "0.8",

"Package": "CN.KSMCBRIGADE.EM.MIXIN",

"CompatibilityLevel": "java_8",, "java_8",

"Refmap": "Em.refmap.json",

"Mixins": [[

],,,

"Client": [[

"Blockmixin"

],,,

"Injectors": {

"DefaultRequire": 1

}

}