Skip to content

Commit

Permalink
Add wool block support
Browse files Browse the repository at this point in the history
  • Loading branch information
opekope2 committed Jan 16, 2024
1 parent 0be7adc commit 04c5738
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,29 @@

package opekope2.avm_staff.internal.staff_item_handler

import net.minecraft.util.Identifier
import opekope2.avm_staff.api.initializer.IStaffModInitializationContext

@Suppress("unused")
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)
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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)
}
}
}
1 change: 1 addition & 0 deletions StaffMod/src/main/resources/avm_staff.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"defaultRequire": 1
},
"client": [
"IMinecraftClientAccessorMixin",
"MinecraftClientMixin"
]
}

0 comments on commit 04c5738

Please sign in to comment.