Skip to content

Commit

Permalink
Merge Staff Mod 0.12.0-beta
Browse files Browse the repository at this point in the history
  • Loading branch information
opekope2 authored Feb 16, 2024
2 parents 766dfbd + 1ceed33 commit 89d12bd
Show file tree
Hide file tree
Showing 47 changed files with 935 additions and 350 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@

package opekope2.avm_staff.internal.fabric

import net.fabricmc.api.EnvType
import net.fabricmc.api.ModInitializer
import net.fabricmc.fabric.api.event.player.AttackBlockCallback
import net.fabricmc.fabric.api.event.player.AttackEntityCallback
import net.fabricmc.fabric.api.item.v1.FabricItemSettings
import net.fabricmc.loader.api.FabricLoader
import net.minecraft.entity.Entity
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.item.Item
import net.minecraft.registry.Registries
import net.minecraft.registry.Registry
import net.minecraft.registry.RegistryKeys
import net.minecraft.registry.tag.TagKey
import net.minecraft.util.ActionResult
import net.minecraft.util.Hand
import net.minecraft.util.Identifier
Expand All @@ -46,6 +51,11 @@ object StaffMod : ModInitializer, IStaffMod {
FabricStaffItem(FabricItemSettings().maxCount(1))
)

override val isPhysicalClient: Boolean
get() = FabricLoader.getInstance().environmentType == EnvType.CLIENT

override val staffsTag: TagKey<Item> = TagKey.of(RegistryKeys.ITEM, Identifier(MOD_ID, "staffs"))

override fun onInitialize() {
AttackBlockCallback.EVENT.register(::attackBlock)
AttackEntityCallback.EVENT.register(::handleEntityAttackEvent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin
import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier
import net.fabricmc.fabric.api.itemgroup.v1.ItemGroupEvents
import net.minecraft.client.render.model.BakedModel
import net.minecraft.client.item.ModelPredicateProviderRegistry
import net.minecraft.client.render.model.UnbakedModel
import net.minecraft.client.render.model.json.JsonUnbakedModel
import net.minecraft.item.ItemGroups
import net.minecraft.item.Items
import opekope2.avm_staff.internal.event_handler.ADD_REMOVE_KEYBINDING
import opekope2.avm_staff.internal.fabric.item.model.StaffItemModel
import opekope2.avm_staff.internal.event_handler.handleKeyBindings
import opekope2.avm_staff.internal.fabric.item.model.UnbakedFabricStaffItemModel
import opekope2.avm_staff.internal.registerModelPredicateProviders
import opekope2.avm_staff.util.MOD_ID

@Suppress("unused")
Expand All @@ -50,16 +53,21 @@ object StaffModClient : ClientModInitializer {
KeyBindingHelper.registerKeyBinding(ADD_REMOVE_KEYBINDING)

ClientTickEvents.END_CLIENT_TICK.register(::handleKeyBindings)

registerModelPredicateProviders(ModelPredicateProviderRegistry::register)
}

private fun modelLoadingPlugin(pluginContext: ModelLoadingPlugin.Context) {
pluginContext.modifyModelAfterBake().register(::modifyModelAfterBake)
pluginContext.modifyModelBeforeBake().register(::modifyModelBeforeBake)
}

private fun modifyModelAfterBake(model: BakedModel?, context: ModelModifier.AfterBake.Context): BakedModel? {
val id = context.id()
private fun modifyModelBeforeBake(model: UnbakedModel, context: ModelModifier.BeforeBake.Context): UnbakedModel {
if (context.id().namespace != MOD_ID) return model

return if (model == null || id.namespace != MOD_ID || id.path != "staff") model
else StaffItemModel(model)
return when (context.id().path) {
// TODO hardcoded paths
"staff", "item/staff_in_use" -> UnbakedFabricStaffItemModel(model as JsonUnbakedModel) // FIXME
else -> model
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,29 @@ import net.fabricmc.fabric.api.renderer.v1.model.FabricBakedModel
import net.fabricmc.fabric.api.renderer.v1.render.RenderContext
import net.minecraft.client.render.model.BakedModel
import net.minecraft.item.ItemStack
import net.minecraft.registry.Registries
import net.minecraft.util.Identifier
import net.minecraft.util.math.random.Random
import opekope2.avm_staff.util.handlerOfItemOrFallback
import opekope2.avm_staff.api.item.model.IStaffItemBakedModel
import opekope2.avm_staff.util.isItemInStaff
import opekope2.avm_staff.util.itemInStaff
import java.util.function.Supplier

@Environment(EnvType.CLIENT)
class StaffItemModel(original: BakedModel) : BakedModel by original {
class BakedFabricStaffItemModel(
original: BakedModel,
private val itemModels: Map<Identifier, IStaffItemBakedModel>,
private val missingModel: BakedModel
) : BakedModel by original {
override fun emitItemQuads(stack: ItemStack, randomSupplier: Supplier<Random>, context: RenderContext) {
super.emitItemQuads(stack, randomSupplier, context)

if (!stack.isItemInStaff) return

val itemStack = stack.itemInStaff
val handler = itemStack.handlerOfItemOrFallback
val itemStack = stack.itemInStaff ?: return

val model = handler.itemModelProvider.getModel(stack) as FabricBakedModel
model.emitItemQuads(stack, randomSupplier, context)
val model = itemModels[Registries.ITEM.getId(itemStack.item)]?.getModel(stack) ?: missingModel
(model as FabricBakedModel).emitItemQuads(stack, randomSupplier, context)
}

override fun isVanillaAdapter() = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* 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.fabric.item.model

import net.fabricmc.api.EnvType
import net.fabricmc.api.Environment
import net.minecraft.client.render.model.BakedModel
import net.minecraft.client.render.model.Baker
import net.minecraft.client.render.model.ModelBakeSettings
import net.minecraft.client.render.model.json.JsonUnbakedModel
import net.minecraft.client.texture.MissingSprite
import net.minecraft.client.texture.Sprite
import net.minecraft.client.util.SpriteIdentifier
import net.minecraft.util.Identifier
import opekope2.avm_staff.api.item.model.IStaffItemBakedModel
import opekope2.avm_staff.internal.item.model.UnbakedStaffItemModel
import java.util.function.Function

@Environment(EnvType.CLIENT)
class UnbakedFabricStaffItemModel(private val original: JsonUnbakedModel) : UnbakedStaffItemModel(original) {
override fun bake(
baker: Baker,
textureGetter: Function<SpriteIdentifier, Sprite>,
rotationContainer: ModelBakeSettings,
modelId: Identifier,
itemModels: Map<Identifier, IStaffItemBakedModel>
): BakedModel? {
return BakedFabricStaffItemModel(
original.bake(baker, textureGetter, rotationContainer, modelId) ?: return null,
itemModels,
baker.bake(MissingSprite.getMissingSpriteId(), rotationContainer)!!
)
}
}
5 changes: 5 additions & 0 deletions ForgeMod/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ dependencies {
forge("net.minecraftforge", "forge", project.gradleProperty("forge_version"))
modApi("dev.architectury", "architectury-forge", project.gradleProperty("architectury_api_version"))

compileOnly(
annotationProcessor("io.github.llamalad7", "mixinextras-common", project.gradleProperty("mixin_extras_version"))
)
implementation(include("io.github.llamalad7", "mixinextras-forge", project.gradleProperty("mixin_extras_version")))

common(project(":StaffMod", configuration = "namedElements")) { isTransitive = false }
shadowCommon(project(":StaffMod", configuration = "transformProductionForge")) { isTransitive = false }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.GameMode;
import opekope2.avm_staff.internal.event_handler.StaffAttackHandler;
import opekope2.avm_staff.internal.event_handler.StaffAttackHandlerKt;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand Down Expand Up @@ -56,7 +56,7 @@ private void attackBlock(BlockPos pos, Direction direction, CallbackInfoReturnab
assert client.player != null;
assert client.world != null;

ActionResult result = StaffAttackHandler.attackBlock(client.player, client.world, Hand.MAIN_HAND, pos, direction);
ActionResult result = StaffAttackHandlerKt.attackBlock(client.player, client.world, Hand.MAIN_HAND, pos, direction);

if (result != ActionResult.PASS) {
// Returning true will spawn particles and trigger the animation of the hand -> only for SUCCESS.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* AvM Staff Mod
* Copyright (c) 2016-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.forge;

import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
import net.minecraft.client.render.model.Baker;
import net.minecraft.client.render.model.UnbakedModel;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.util.Identifier;
import opekope2.avm_staff.internal.forge.item.model.UnbakedForgeStaffItemModel;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Coerce;

import static opekope2.avm_staff.util.Constants.MOD_ID;

// Forge geometry loaders are way more difficult, than porting a Fabric mixin
@Mixin(targets = "net/minecraft/client/render/model/ModelLoader$BakerImpl")
public class ModelLoaderBakerImplMixin {
// Change method to directly call Staff Mod instead of an exposed event
// Ignore this error, IntelliJ/mcdev is not looking at the correct jar
@WrapOperation(
method = "bake(Lnet/minecraft/util/Identifier;Lnet/minecraft/client/render/model/ModelBakeSettings;Ljava/util/function/Function;)Lnet/minecraft/client/render/model/BakedModel;",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/client/render/model/ModelLoader$BakerImpl;getOrLoadModel(Lnet/minecraft/util/Identifier;)Lnet/minecraft/client/render/model/UnbakedModel;"
)
)
private UnbakedModel modifyUnbakedModel(@Coerce Baker thiz, Identifier id, Operation<UnbakedModel> original) {
UnbakedModel model = original.call(thiz, id);
if (!MOD_ID.equals(id.getNamespace())) return model;

return switch (id.getPath()) {
// TODO hardcoded paths
case "staff", "item/staff_in_use" -> new UnbakedForgeStaffItemModel((JsonUnbakedModel) model); // FIXME
default -> model;
};
}

// Remove unused mixins: invokeModifyAfterBake
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import opekope2.avm_staff.internal.event_handler.StaffAttackHandler;
import opekope2.avm_staff.internal.event_handler.StaffAttackHandlerKt;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -32,7 +32,7 @@ public class ServerPlayerEntityMixin {
@Inject(method = "attack", at = @At("HEAD"), cancellable = true)
public void onPlayerInteractEntity(Entity target, CallbackInfo info) {
ServerPlayerEntity player = (ServerPlayerEntity) (Object) this;
ActionResult result = StaffAttackHandler.attackEntity(player, player.getEntityWorld(), Hand.MAIN_HAND, target);
ActionResult result = StaffAttackHandlerKt.attackEntity(player, player.getEntityWorld(), Hand.MAIN_HAND, target);

if (result != ActionResult.PASS) {
info.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import opekope2.avm_staff.internal.event_handler.StaffAttackHandler;
import opekope2.avm_staff.internal.event_handler.StaffAttackHandlerKt;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -50,7 +50,7 @@ public abstract class ServerPlayerInteractionManagerMixin {
public void startBlockBreak(BlockPos pos, PlayerActionC2SPacket.Action playerAction, Direction direction, int worldHeight, int i, CallbackInfo info) {
if (playerAction != PlayerActionC2SPacket.Action.START_DESTROY_BLOCK) return;

ActionResult result = StaffAttackHandler.attackBlock(player, world, Hand.MAIN_HAND, pos, direction);
ActionResult result = StaffAttackHandlerKt.attackBlock(player, world, Hand.MAIN_HAND, pos, direction);

if (result == ActionResult.PASS) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
package opekope2.avm_staff.internal.forge

import net.minecraft.item.Item
import net.minecraft.registry.tag.ItemTags
import net.minecraft.registry.tag.TagKey
import net.minecraft.util.Identifier
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.registries.DeferredRegister
Expand All @@ -29,6 +32,7 @@ import opekope2.avm_staff.internal.forge.item.ForgeStaffItem
import opekope2.avm_staff.internal.initializeNetworking
import opekope2.avm_staff.internal.staff_item_handler.registerVanillaStaffItemHandlers
import opekope2.avm_staff.util.MOD_ID
import thedarkcolour.kotlinforforge.forge.DIST
import thedarkcolour.kotlinforforge.forge.MOD_BUS
import thedarkcolour.kotlinforforge.forge.runWhenOn

Expand All @@ -48,6 +52,11 @@ object StaffMod : IStaffMod {
override val staffItem: StaffItem
get() = STAFF_ITEM.get()

override val isPhysicalClient: Boolean
get() = DIST.isClient

override val staffsTag: TagKey<Item> = ItemTags.create(Identifier(MOD_ID, "staffs"))

private fun initialize() {
ITEMS.register(MOD_BUS)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,23 @@
package opekope2.avm_staff.internal.forge

import net.minecraft.client.MinecraftClient
import net.minecraft.client.util.ModelIdentifier
import net.minecraft.client.item.ModelPredicateProviderRegistry
import net.minecraft.item.ItemGroup.StackVisibility.PARENT_AND_SEARCH_TABS
import net.minecraft.item.ItemGroups
import net.minecraft.item.ItemStack
import net.minecraft.item.Items
import net.minecraftforge.api.distmarker.Dist
import net.minecraftforge.api.distmarker.OnlyIn
import net.minecraftforge.client.event.ModelEvent
import net.minecraftforge.client.event.RegisterKeyMappingsEvent
import net.minecraftforge.common.MinecraftForge.EVENT_BUS
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent
import net.minecraftforge.event.TickEvent
import net.minecraftforge.eventbus.api.SubscribeEvent
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent
import opekope2.avm_staff.internal.event_handler.ADD_REMOVE_KEYBINDING
import opekope2.avm_staff.internal.forge.item.model.StaffItemModel
import opekope2.avm_staff.internal.event_handler.handleKeyBindings
import opekope2.avm_staff.internal.platform.forge.getStaffMod
import opekope2.avm_staff.util.MOD_ID
import opekope2.avm_staff.internal.registerModelPredicateProviders
import thedarkcolour.kotlinforforge.forge.MOD_BUS

@OnlyIn(Dist.CLIENT)
Expand All @@ -46,6 +45,13 @@ object StaffModClient {
EVENT_BUS.register(javaClass)
}

@SubscribeEvent
fun initializeClient(event: FMLClientSetupEvent) {
event.enqueueWork {
registerModelPredicateProviders(ModelPredicateProviderRegistry::register)
}
}

@SubscribeEvent
fun addItemsToItemGroups(event: BuildCreativeModeTabContentsEvent) {
if (event.tabKey === ItemGroups.TOOLS) {
Expand Down Expand Up @@ -75,11 +81,4 @@ object StaffModClient {

handleKeyBindings(MinecraftClient.getInstance())
}

@SubscribeEvent
fun modifyModelAfterBake(event: ModelEvent.ModifyBakingResult) {
val id = ModelIdentifier(MOD_ID, "staff", "inventory")

event.models[id] = StaffItemModel(event.models[id]!!)
}
}
Loading

0 comments on commit 89d12bd

Please sign in to comment.