From 7cf7f6407d038b103c9ff88cac893b80c00c8e25 Mon Sep 17 00:00:00 2001 From: alpha Date: Sun, 7 Apr 2024 14:33:55 -0500 Subject: [PATCH] Blocks and gui utils modules --- README.md | 2 ++ modules/blocks/build.gradle | 0 .../blocks/extensions/OnExplodedBlock.java | 24 +++++++++++++ .../blocks/mixin/ExplosionMixin.java | 35 +++++++++++++++++++ .../blocks/src/main/resources/fabric.mod.json | 7 ++++ .../resources/porting_lib_blocks.mixins.json | 12 +++++++ modules/gui_utils/build.gradle | 0 .../porting_lib/gui/utils/ModdedButton.java | 10 ++++++ .../src/main/resources/fabric.mod.json | 7 ++++ .../porting_lib_gui_utils.accesswidener | 9 +++++ 10 files changed, 106 insertions(+) create mode 100644 modules/blocks/build.gradle create mode 100644 modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/extensions/OnExplodedBlock.java create mode 100644 modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/mixin/ExplosionMixin.java create mode 100644 modules/blocks/src/main/resources/fabric.mod.json create mode 100644 modules/blocks/src/main/resources/porting_lib_blocks.mixins.json create mode 100644 modules/gui_utils/build.gradle create mode 100644 modules/gui_utils/src/main/java/io/github/fabricators_of_create/porting_lib/gui/utils/ModdedButton.java create mode 100644 modules/gui_utils/src/main/resources/fabric.mod.json create mode 100644 modules/gui_utils/src/main/resources/porting_lib_gui_utils.accesswidener diff --git a/README.md b/README.md index 01f80ca31..9bce17744 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,7 @@ Just choose a version and use its version number. | `accessors` | Accessor mixins aplenty | | `attributes` | Additional Entity Attributes; step height, gravity, swim speed | | `base` | Code that has not yet been split into modules | +| `blocks` | Adds extra block extensions. | | `brewing` | A potion recipe api | | `client_events` | Useful client-side events | | `common` | Miscellaneous utilities for other modules | @@ -34,6 +35,7 @@ Just choose a version and use its version number. | `extensions` | Extensions to vanilla classes for additional functionality | | `fluids` | Api that provides additional fluid attributes for fluids | | `gametest` | Tools to make GameTest creation as easy as possible | +| `gui_utils` | Adds additional utility classes and methods for screens and guis | | `items` | Adds extra item extensions | | `lazy_registration` | A implementation of forge's DeferredRegister system rewritten for fabric | | `level_events` | Provides common level events for mods. | diff --git a/modules/blocks/build.gradle b/modules/blocks/build.gradle new file mode 100644 index 000000000..e69de29bb diff --git a/modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/extensions/OnExplodedBlock.java b/modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/extensions/OnExplodedBlock.java new file mode 100644 index 000000000..14f525f4b --- /dev/null +++ b/modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/extensions/OnExplodedBlock.java @@ -0,0 +1,24 @@ +package io.github.fabricators_of_create.porting_lib.blocks.extensions; + +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Explosion; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; + +public interface OnExplodedBlock { + /** + * Called when the block is destroyed by an explosion. + * Useful for allowing the block to take into account tile entities, + * state, etc. when exploded, before it is removed. + * + * @param level The current level + * @param pos Block position in level + * @param explosion The explosion instance affecting the block + */ + default void onBlockExploded(BlockState state, Level level, BlockPos pos, Explosion explosion) { + level.setBlock(pos, Blocks.AIR.defaultBlockState(), 3); + ((Block) this).wasExploded(level, pos, explosion); + } +} diff --git a/modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/mixin/ExplosionMixin.java b/modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/mixin/ExplosionMixin.java new file mode 100644 index 000000000..79afdabbd --- /dev/null +++ b/modules/blocks/src/main/java/io/github/fabricators_of_create/porting_lib/blocks/mixin/ExplosionMixin.java @@ -0,0 +1,35 @@ +package io.github.fabricators_of_create.porting_lib.blocks.mixin; + +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; + +import com.llamalad7.mixinextras.injector.v2.WrapWithCondition; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; +import com.llamalad7.mixinextras.sugar.Local; + +import io.github.fabricators_of_create.porting_lib.blocks.extensions.OnExplodedBlock; +import net.minecraft.core.BlockPos; +import net.minecraft.world.level.Explosion; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.state.BlockState; + +@Mixin(Explosion.class) +public class ExplosionMixin { + @WrapOperation(method = "finalizeExplosion", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/block/Block;wasExploded(Lnet/minecraft/world/level/Level;Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/Explosion;)V")) + private void onBlockExploded(Block instance, Level level, BlockPos blockPos, Explosion explosion, Operation original, @Local(index = 7) BlockState state) { + if (state.getBlock() instanceof OnExplodedBlock onExplodedBlock) { + onExplodedBlock.onBlockExploded(state, level, blockPos, explosion); + } else { + original.call(instance, level, blockPos, explosion); + } + } + + @WrapWithCondition(method = "finalizeExplosion", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/Level;setBlock(Lnet/minecraft/core/BlockPos;Lnet/minecraft/world/level/block/state/BlockState;I)Z")) + private boolean dontSetBlockOnCustom(Level level, BlockPos pos, BlockState airState, int flag, @Local(index = 7) BlockState state) { + if (state.getBlock() instanceof OnExplodedBlock) + return false; + return true; + } +} diff --git a/modules/blocks/src/main/resources/fabric.mod.json b/modules/blocks/src/main/resources/fabric.mod.json new file mode 100644 index 000000000..f15a9836c --- /dev/null +++ b/modules/blocks/src/main/resources/fabric.mod.json @@ -0,0 +1,7 @@ +{ + "schemaVersion": 1, + "id": "porting_lib_blocks", + "version": "${version}", + "name": "Porting Lib Blocks", + "description": "Adds extra block extensions." +} diff --git a/modules/blocks/src/main/resources/porting_lib_blocks.mixins.json b/modules/blocks/src/main/resources/porting_lib_blocks.mixins.json new file mode 100644 index 000000000..6493622b7 --- /dev/null +++ b/modules/blocks/src/main/resources/porting_lib_blocks.mixins.json @@ -0,0 +1,12 @@ +{ + "required": true, + "minVersion": "0.8", + "package": "io.github.fabricators_of_create.porting_lib.blocks.mixin", + "compatibilityLevel": "JAVA_17", + "injectors": { + "defaultRequire": 1 + }, + "mixins": [ + "ExplosionMixin" + ] +} diff --git a/modules/gui_utils/build.gradle b/modules/gui_utils/build.gradle new file mode 100644 index 000000000..e69de29bb diff --git a/modules/gui_utils/src/main/java/io/github/fabricators_of_create/porting_lib/gui/utils/ModdedButton.java b/modules/gui_utils/src/main/java/io/github/fabricators_of_create/porting_lib/gui/utils/ModdedButton.java new file mode 100644 index 000000000..44581dcdb --- /dev/null +++ b/modules/gui_utils/src/main/java/io/github/fabricators_of_create/porting_lib/gui/utils/ModdedButton.java @@ -0,0 +1,10 @@ +package io.github.fabricators_of_create.porting_lib.gui.utils; + +import net.minecraft.client.gui.components.Button; + +public class ModdedButton extends Button { + protected ModdedButton(Builder builder) { + super(builder.x, builder.y, builder.width, builder.height, builder.message, builder.onPress, builder.createNarration); + setTooltip(builder.tooltip); // Forge: Make use of the Builder tooltip + } +} diff --git a/modules/gui_utils/src/main/resources/fabric.mod.json b/modules/gui_utils/src/main/resources/fabric.mod.json new file mode 100644 index 000000000..8f670fc94 --- /dev/null +++ b/modules/gui_utils/src/main/resources/fabric.mod.json @@ -0,0 +1,7 @@ +{ + "schemaVersion": 1, + "id": "porting_lib_gui_utils", + "version": "${version}", + "name": "Porting Lib Gui Utils", + "description": "Adds additional utility classes and methods for screens and guis." +} diff --git a/modules/gui_utils/src/main/resources/porting_lib_gui_utils.accesswidener b/modules/gui_utils/src/main/resources/porting_lib_gui_utils.accesswidener new file mode 100644 index 000000000..a6505a7ea --- /dev/null +++ b/modules/gui_utils/src/main/resources/porting_lib_gui_utils.accesswidener @@ -0,0 +1,9 @@ +accessWidener v2 named +accessible field net/minecraft/client/gui/components/Button$Builder message Lnet/minecraft/network/chat/Component; +accessible field net/minecraft/client/gui/components/Button$Builder onPress Lnet/minecraft/client/gui/components/Button$OnPress; +accessible field net/minecraft/client/gui/components/Button$Builder tooltip Lnet/minecraft/client/gui/components/Tooltip; +accessible field net/minecraft/client/gui/components/Button$Builder x I +accessible field net/minecraft/client/gui/components/Button$Builder y I +accessible field net/minecraft/client/gui/components/Button$Builder width I +accessible field net/minecraft/client/gui/components/Button$Builder height I +accessible field net/minecraft/client/gui/components/Button$Builder createNarration Lnet/minecraft/client/gui/components/Button$CreateNarration;