generated from SmushyTaco/Example-Mod
-
-
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
4 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
StaffMod/src/main/java/opekope2/avm_staff/mixin/IMinecraftClientAccessorMixin.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,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); | ||
} |
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
105 changes: 105 additions & 0 deletions
105
StaffMod/src/main/kotlin/opekope2/avm_staff/internal/staff_item_handler/WoolHandler.kt
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,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) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
"defaultRequire": 1 | ||
}, | ||
"client": [ | ||
"IMinecraftClientAccessorMixin", | ||
"MinecraftClientMixin" | ||
] | ||
} |