From 04c573815ac20bcb5cc9bee86777165cd154d842 Mon Sep 17 00:00:00 2001 From: opekope2 Date: Tue, 16 Jan 2024 22:10:37 +0100 Subject: [PATCH] Add wool block support --- .../mixin/IMinecraftClientAccessorMixin.java | 29 +++++ .../VanillaStaffItemHandlers.kt | 18 +++ .../staff_item_handler/WoolHandler.kt | 105 ++++++++++++++++++ .../src/main/resources/avm_staff.mixins.json | 1 + 4 files changed, 153 insertions(+) create mode 100644 StaffMod/src/main/java/opekope2/avm_staff/mixin/IMinecraftClientAccessorMixin.java create mode 100644 StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/WoolHandler.kt diff --git a/StaffMod/src/main/java/opekope2/avm_staff/mixin/IMinecraftClientAccessorMixin.java b/StaffMod/src/main/java/opekope2/avm_staff/mixin/IMinecraftClientAccessorMixin.java new file mode 100644 index 000000000..feecc31f7 --- /dev/null +++ b/StaffMod/src/main/java/opekope2/avm_staff/mixin/IMinecraftClientAccessorMixin.java @@ -0,0 +1,29 @@ +/* + * AvM Staff Mod + * Copyright (c) 2024 opekope2 + * + * This mod is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This mod is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this mod. If not, see . + */ + +package opekope2.avm_staff.mixin; + +import net.minecraft.client.MinecraftClient; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.gen.Accessor; + +@Mixin(MinecraftClient.class) +public interface IMinecraftClientAccessorMixin { + @Accessor + void setItemUseCooldown(int value); +} diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/VanillaStaffItemHandlers.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/VanillaStaffItemHandlers.kt index 898b5ee5e..9462af893 100644 --- a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/VanillaStaffItemHandlers.kt +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/VanillaStaffItemHandlers.kt @@ -18,6 +18,7 @@ package opekope2.avm_staff.internal.staff_item_handler +import net.minecraft.util.Identifier import opekope2.avm_staff.api.initializer.IStaffModInitializationContext @Suppress("unused") @@ -25,4 +26,21 @@ fun register(context: IStaffModInitializationContext) { BoneBlockHandler.registerStaffItemHandler(context) SnowBlockHandler.registerStaffItemHandler(context) + + WoolHandler.registerStaffItemHandler(Identifier("white_wool"), Identifier("white_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("orange_wool"), Identifier("orange_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("magenta_wool"), Identifier("magenta_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("light_blue_wool"), Identifier("light_blue_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("yellow_wool"), Identifier("yellow_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("lime_wool"), Identifier("lime_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("pink_wool"), Identifier("pink_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("gray_wool"), Identifier("gray_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("light_gray_wool"), Identifier("light_gray_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("cyan_wool"), Identifier("cyan_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("purple_wool"), Identifier("purple_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("blue_wool"), Identifier("blue_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("brown_wool"), Identifier("brown_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("green_wool"), Identifier("green_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("red_wool"), Identifier("red_carpet"), context) + WoolHandler.registerStaffItemHandler(Identifier("black_wool"), Identifier("black_carpet"), context) } diff --git a/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/WoolHandler.kt b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/WoolHandler.kt new file mode 100644 index 000000000..3c4a838ad --- /dev/null +++ b/StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/WoolHandler.kt @@ -0,0 +1,105 @@ +/* + * AvM Staff Mod + * Copyright (c) 2024 opekope2 + * + * This mod is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This mod is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this mod. If not, see . + */ + +package opekope2.avm_staff.internal.staff_item_handler + +import net.minecraft.client.MinecraftClient +import net.minecraft.client.network.ClientPlayerEntity +import net.minecraft.entity.LivingEntity +import net.minecraft.entity.player.PlayerEntity +import net.minecraft.item.BlockItem +import net.minecraft.item.ItemPlacementContext +import net.minecraft.item.ItemStack +import net.minecraft.registry.Registries +import net.minecraft.registry.tag.BlockTags +import net.minecraft.sound.SoundCategory +import net.minecraft.util.ActionResult +import net.minecraft.util.Hand +import net.minecraft.util.Identifier +import net.minecraft.util.hit.BlockHitResult +import net.minecraft.util.math.BlockPos +import net.minecraft.util.math.Direction +import net.minecraft.world.World +import net.minecraft.world.event.GameEvent +import opekope2.avm_staff.api.initializer.IStaffModInitializationContext +import opekope2.avm_staff.api.item.StaffItemHandler +import opekope2.avm_staff.mixin.IMinecraftClientAccessorMixin + +class WoolHandler(wool: Identifier, carpet: Identifier) : StaffItemHandler() { + private val woolState = (Registries.ITEM[wool] as BlockItem).block.defaultState + private val carpetState = (Registries.ITEM[carpet] as BlockItem).block.defaultState + + override fun useOnBlock( + staffStack: ItemStack, + world: World, + user: LivingEntity, + target: BlockPos, + side: Direction, + hand: Hand + ): ActionResult { + if (world.isClient && user is ClientPlayerEntity) { + // Allow fast block placement + (MinecraftClient.getInstance() as IMinecraftClientAccessorMixin).setItemUseCooldown(0) + } + + val originalState = world.getBlockState(target) + if (originalState.isIn(BlockTags.WOOL) || originalState.isIn(BlockTags.WOOL_CARPETS)) return ActionResult.FAIL + + val woolPlaceContext = ItemPlacementContext( + world, + user as? PlayerEntity, + hand, + staffStack, + BlockHitResult(target.toCenterPos(), side, target, false) + ) + if (!woolPlaceContext.canPlace()) return ActionResult.FAIL + + val placedState = if (side == Direction.UP) carpetState else woolState + if (!world.isClient) { + world.setBlockState(woolPlaceContext.blockPos, placedState) + } + + val woolSoundGroup = placedState.soundGroup + world.playSound( + user, + woolPlaceContext.blockPos, + woolSoundGroup.placeSound, + SoundCategory.BLOCKS, + (woolSoundGroup.volume + 1.0f) / 2.0f, + woolSoundGroup.pitch * 0.8f + ) + world.emitGameEvent(GameEvent.BLOCK_PLACE, woolPlaceContext.blockPos, GameEvent.Emitter.of(user, placedState)) + + return ActionResult.SUCCESS + } + + companion object { + fun registerStaffItemHandler( + item: Identifier, + wool: Identifier, + carpet: Identifier, + context: IStaffModInitializationContext + ) { + context.registerStaffItemHandler(item, WoolHandler(wool, carpet)) + } + + fun registerStaffItemHandler(wool: Identifier, carpet: Identifier, context: IStaffModInitializationContext) { + registerStaffItemHandler(wool, wool, carpet, context) + } + } +} diff --git a/StaffMod/src/main/resources/avm_staff.mixins.json b/StaffMod/src/main/resources/avm_staff.mixins.json index 73be3fbd7..c1638f508 100644 --- a/StaffMod/src/main/resources/avm_staff.mixins.json +++ b/StaffMod/src/main/resources/avm_staff.mixins.json @@ -6,6 +6,7 @@ "defaultRequire": 1 }, "client": [ + "IMinecraftClientAccessorMixin", "MinecraftClientMixin" ] }