forked from neoforged/MDK
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
276 additions
and
205 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
37 changes: 37 additions & 0 deletions
37
src/main/java/com/example/examplemod/mixin/ExampleServerMixin.java
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,37 @@ | ||
package com.example.examplemod.mixin; | ||
|
||
import com.mojang.logging.LogUtils; | ||
import net.minecraft.commands.CommandSource; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.ServerInfo; | ||
import net.minecraft.server.TickTask; | ||
import net.minecraft.util.thread.ReentrantBlockableEventLoop; | ||
import net.minecraft.world.level.chunk.storage.ChunkIOErrorReporter; | ||
import org.slf4j.Logger; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
// Example Mixin to demonstrate that Mixins should be written in Java. | ||
// It is _technically_ possible to write them in Kotlin but only if you understand | ||
// how the Kotlin compiler works internally and what bytecode it produces and are fully | ||
// aware of that at all times. The general advice is: JUST USE JAVA FOR MIXINS | ||
|
||
// Marked as abstract so all the extends and implements clauses don't need to be "followed". | ||
// Those clauses are added to get easy access to things without needing to @Shadow them. | ||
@Mixin(MinecraftServer.class) | ||
public abstract class ExampleServerMixin extends ReentrantBlockableEventLoop<TickTask> implements ServerInfo, ChunkIOErrorReporter, CommandSource, AutoCloseable { | ||
|
||
private final Logger LOGGER = LogUtils.getLogger(); | ||
|
||
// Constructor of a Mixin gets ignored | ||
public ExampleServerMixin(String pName) { | ||
super(pName); | ||
} | ||
|
||
@Inject(method = "runServer", at = @At("HEAD")) | ||
public void examplemod$runServer(CallbackInfo ci) { | ||
LOGGER.info("Example Mixin ran from server startup"); | ||
} | ||
} |
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,60 @@ | ||
package com.example.examplemod | ||
|
||
import net.minecraft.core.registries.BuiltInRegistries | ||
import net.minecraft.resources.ResourceLocation | ||
import net.minecraft.world.item.Item | ||
import net.neoforged.bus.api.SubscribeEvent | ||
import net.neoforged.fml.common.EventBusSubscriber | ||
import net.neoforged.fml.event.config.ModConfigEvent | ||
import net.neoforged.neoforge.common.ModConfigSpec | ||
import java.util.stream.Collectors | ||
|
||
// An example config class. This is not required, but it's a good idea to have one to keep your config organized. | ||
// Demonstrates how to use Neo's config APIs | ||
@EventBusSubscriber(modid = ExampleMod.MODID, bus = EventBusSubscriber.Bus.MOD) | ||
object Config { | ||
private val BUILDER: ModConfigSpec.Builder = ModConfigSpec.Builder() | ||
|
||
private val LOG_DIRT_BLOCK: ModConfigSpec.BooleanValue = | ||
BUILDER.comment("Whether to log the dirt block on common setup").define("logDirtBlock", true) | ||
|
||
private val MAGIC_NUMBER: ModConfigSpec.IntValue = | ||
BUILDER.comment("A magic number").defineInRange("magicNumber", 42, 0, Int.MAX_VALUE) | ||
|
||
val MAGIC_NUMBER_INTRODUCTION: ModConfigSpec.ConfigValue<String> = | ||
BUILDER.comment("What you want the introduction message to be for the magic number") | ||
.define("magicNumberIntroduction", "The magic number is... ") | ||
|
||
// a list of strings that are treated as resource locations for items | ||
private val ITEM_STRINGS: ModConfigSpec.ConfigValue<List<String>> = | ||
BUILDER.comment("A list of items to log on common setup.").defineListAllowEmpty( | ||
"items", | ||
listOf("minecraft:iron_ingot"), | ||
::validateItemName | ||
) | ||
|
||
val SPEC: ModConfigSpec = BUILDER.build() | ||
|
||
var logDirtBlock: Boolean = false | ||
var magicNumber: Int = 0 | ||
lateinit var magicNumberIntroduction: String | ||
lateinit var items: Set<Item> | ||
|
||
private fun validateItemName(obj: Any): Boolean { | ||
return obj is String && BuiltInRegistries.ITEM.containsKey(ResourceLocation.parse(obj)) | ||
} | ||
|
||
@SubscribeEvent | ||
fun onLoad(event: ModConfigEvent) { | ||
logDirtBlock = LOG_DIRT_BLOCK.get() | ||
magicNumber = MAGIC_NUMBER.get() | ||
magicNumberIntroduction = MAGIC_NUMBER_INTRODUCTION.get() | ||
|
||
// convert the list of strings into a set of items | ||
items = ITEM_STRINGS.get().stream().map { itemName: String? -> | ||
BuiltInRegistries.ITEM[ResourceLocation.parse( | ||
itemName | ||
)] | ||
}.collect(Collectors.toSet()) | ||
} | ||
} |
Oops, something went wrong.