Skip to content

Commit

Permalink
Fixed a lot of issues with Mjolnir
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Nov 29, 2023
1 parent 38eebb1 commit ac89fc2
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,20 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import io.github.realyusufismail.temporalsmith.client.ClientEvents;
import net.minecraft.client.model.EntityModel;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.Model;
import net.minecraft.client.model.geom.ModelPart;
import net.minecraft.client.model.geom.PartPose;
import net.minecraft.client.model.geom.builders.*;
import net.minecraft.world.entity.Entity;
import net.minecraft.client.renderer.RenderType;
import org.jetbrains.annotations.NotNull;

public class MjolnirModel<T extends Entity> extends EntityModel<T> {
public static final ModelLayerLocation LAYER_LOCATION = ClientEvents.INSTANCE.getMjolnirLayer();
public class MjolnirModel extends Model {
private final ModelPart Themiddlepart;
private final ModelPart Sidepart;
private final ModelPart handle;

public MjolnirModel(ModelPart root) {
super(RenderType::entityCutoutNoCull);
this.Themiddlepart = root.getChild("Themiddlepart");
this.Sidepart = root.getChild("Sidepart");
this.handle = root.getChild("handle");
Expand Down Expand Up @@ -282,15 +280,6 @@ public MjolnirModel(ModelPart root) {
return LayerDefinition.create(meshdefinition, 128, 128);
}

@Override
public void setupAnim(
@NotNull T entity,
float limbSwing,
float limbSwingAmount,
float ageInTicks,
float netHeadYaw,
float headPitch) {}

@Override
public void renderToBuffer(
@NotNull PoseStack poseStack,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ class TemporalSmith {
FORGE_BUS.addListener(ClientEvents::onEntityDeath)
// entity attribute creation event
bus.addListener(ClientEvents::onRegisterEntities)
// player tick event
FORGE_BUS.addListener(ClientEvents::onPlayerTickEvent)
// falling living entity event
FORGE_BUS.addListener(ClientEvents::onLivingFallEvent)
// living hurt event
FORGE_BUS.addListener(ClientEvents::onLivingHurtEvent)
FORGE_BUS.addListener(ClientEvents::onLivingDamageEvent)

KotlinModLoadingContext.get().getKEventBus().addListener(FMLClientSetupEvent::class.java) {
event: FMLClientSetupEvent ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,36 @@ import net.minecraft.client.multiplayer.ClientLevel
import net.minecraft.client.renderer.ItemBlockRenderTypes
import net.minecraft.client.renderer.RenderType
import net.minecraft.client.renderer.item.ItemProperties
import net.minecraft.network.chat.Component
import net.minecraft.resources.ResourceLocation
import net.minecraft.server.level.ServerPlayer
import net.minecraft.sounds.SoundEvents
import net.minecraft.world.effect.MobEffectInstance
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.EntityType
import net.minecraft.world.entity.LivingEntity
import net.minecraft.world.entity.player.Inventory
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.minecraft.world.phys.Vec3
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent
import net.neoforged.neoforge.client.event.EntityRenderersEvent
import net.neoforged.neoforge.client.event.InputEvent
import net.neoforged.neoforge.client.event.RegisterKeyMappingsEvent
import net.neoforged.neoforge.event.TickEvent
import net.neoforged.neoforge.event.entity.EntityAttributeCreationEvent
import net.neoforged.neoforge.event.entity.living.LivingDeathEvent
import net.neoforged.neoforge.event.entity.living.LivingFallEvent
import net.neoforged.neoforge.event.entity.living.LivingHurtEvent

object ClientEvents {
val MjolnirLayer = ModelLayerLocation(TemporalSmith.getModIdAndName("mjolnir"), "mjolnir")
val EnderiteGolemLayer =
ModelLayerLocation(TemporalSmith.getModIdAndName("enderite_golem"), "enderite_golem")

// booleans
private var giveMjolnirToPlayer = false

fun clientSetup(event: FMLClientSetupEvent) {
event.enqueueWork { registerScreens() }

Expand Down Expand Up @@ -173,19 +182,7 @@ object ClientEvents {

fun onKeyInput(event: InputEvent.Key) {
if (KeyBinding.GET_MJOLNIR.consumeClick()) {
val player = Minecraft.getInstance().player
if (player != null) {
val effects = player.activeEffectsMap

if (effects.contains(MobEffectsInit.WORTHY_EFFECT.get())) {
// if they alread have mjolnir, ignore
if (player.inventory.contains(ItemInit.MJOLNIR.get().defaultInstance)) {
return
} else {
player.inventory.add(ItemInit.MJOLNIR.get().defaultInstance)
}
}
}
giveMjolnirToPlayer = true
}
}

Expand All @@ -211,4 +208,68 @@ object ClientEvents {
}
}
}

fun onPlayerTickEvent(event: TickEvent.PlayerTickEvent) {
if (event.player is Player) {

val player = event.player as Player

val inventory = player.inventory
val effects = player.activeEffectsMap

// if player does not have the hammer make sure they can't fly

if (!inventory.contains(ItemInit.MJOLNIR.get().defaultInstance)) {
player.abilities.mayfly = false
player.abilities.flying = false
player.abilities.flyingSpeed = 0.05f
player.abilities.invulnerable = false
} else {
player.abilities.mayfly = true
player.abilities.flyingSpeed = 0.06f
player.abilities.invulnerable = true
}

if (giveMjolnirToPlayer) {

if (effects.contains(MobEffectsInit.WORTHY_EFFECT.get()) && !inventory.contains(ItemInit.MJOLNIR.get().defaultInstance)) {
inventory.add(ItemInit.MJOLNIR.get().defaultInstance)
} else if (!effects.contains(MobEffectsInit.WORTHY_EFFECT.get())) {
player.sendSystemMessage(Component.literal("You are not worthy to wield thors hammer"))
}

giveMjolnirToPlayer = false
}
}
}

fun onLivingFallEvent(event : LivingFallEvent) {
if (event.entity is Player) {
val player = event.entity as Player

if (player.activeEffectsMap.contains(MobEffectsInit.WORTHY_EFFECT.get())) {
event.distance = 0.0f
}
}
}

fun onLivingHurtEvent(event : LivingHurtEvent) {
if (event.entity is Player) {
val player = event.entity as Player

if (player.activeEffectsMap.contains(MobEffectsInit.WORTHY_EFFECT.get())) {
event.amount = 0.0f
}
}
}

fun onLivingDamageEvent(event : LivingHurtEvent) {
if (event.entity is Player) {
val player = event.entity as Player

if (player.activeEffectsMap.contains(MobEffectsInit.WORTHY_EFFECT.get())) {
event.amount = 0.0f
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.mojang.blaze3d.vertex.PoseStack
import com.mojang.math.Axis
import io.github.realyusufismail.temporalsmith.TemporalSmith
import io.github.realyusufismail.temporalsmith.client.ClientEvents
import io.github.realyusufismail.temporalsmith.common.entity.MjolnirEntity
import io.github.realyusufismail.temporalsmith.common.entity.ModTridentEntity
import io.github.realyusufismail.temporalsmith.entities.mjolnir.MjolnirModel
import net.minecraft.client.renderer.MultiBufferSource
Expand All @@ -36,8 +35,8 @@ import net.minecraft.util.Mth
class MjolnirItemRenderer(context: EntityRendererProvider.Context) :
EntityRenderer<ModTridentEntity>(context) {
private val location: ResourceLocation =
TemporalSmith.getModIdAndName("textures/entity/item/mjolnir/all.png")
private var mjolnirModel: MjolnirModel<MjolnirEntity>? = null
TemporalSmith.getModIdAndName("textures/item/mjolnir/all.png")
private var mjolnirModel: MjolnirModel? = null

init {
mjolnirModel = MjolnirModel(context.bakeLayer(ClientEvents.MjolnirLayer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import com.mojang.blaze3d.vertex.PoseStack
import io.github.realyusufismail.temporalsmith.TemporalSmith
import io.github.realyusufismail.temporalsmith.client.ClientEvents
import io.github.realyusufismail.temporalsmith.client.ModBlockEntityWithoutLevelRenderer
import io.github.realyusufismail.temporalsmith.common.entity.MjolnirEntity
import io.github.realyusufismail.temporalsmith.entities.mjolnir.MjolnirModel
import io.github.realyusufismail.temporalsmith.items.hammer.Mjolnir
import net.minecraft.client.Minecraft
Expand All @@ -36,8 +35,8 @@ import net.minecraft.world.item.ItemStack

class MjolnirItemRendererISTER : ModBlockEntityWithoutLevelRenderer() {
private val location: ResourceLocation =
TemporalSmith.getModIdAndName("textures/entity/item/mjolnir/all.png")
private var mjolnirModel: MjolnirModel<MjolnirEntity>? = null
TemporalSmith.getModIdAndName("textures/item/mjolnir/all.png")
private var mjolnirModel: MjolnirModel? = null

override fun onResourceManagerReload(manager: ResourceManager) {
mjolnirModel = MjolnirModel(this.entityModelSet.bakeLayer(ClientEvents.MjolnirLayer))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.Tier
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.state.BlockState
import net.neoforged.neoforge.client.extensions.common.IClientItemExtensions

open class Mjolnir(
Expand All @@ -40,6 +41,7 @@ open class Mjolnir(
pAttackSpeedModifier: Float = 15.0f,
pAttackDamageModifier: Float = -3f
) : ArmourToolsModTridentItem(tier, properties, pAttackSpeedModifier, pAttackDamageModifier) {

override fun onUseTick(
pLevel: Level,
pLivingEntity: LivingEntity,
Expand All @@ -56,20 +58,6 @@ open class Mjolnir(
}
}

override fun inventoryTick(
pStack: ItemStack,
pLevel: Level,
pEntity: Entity,
pSlotId: Int,
pIsSelected: Boolean
) {
if (pEntity is Player) {
pEntity.abilities.mayfly = true
pEntity.abilities.flyingSpeed = 0.06f
pEntity.abilities.invulnerable = true
}
}

override fun getThrownEntity(
world: Level,
thrower: LivingEntity,
Expand Down

0 comments on commit ac89fc2

Please sign in to comment.