diff --git a/build.gradle b/build.gradle index 40cd8a2..858fd7e 100644 --- a/build.gradle +++ b/build.gradle @@ -3,7 +3,7 @@ plugins { id 'eclipse' id 'idea' id 'maven-publish' - id 'net.neoforged.gradle.userdev' version '7.0.142' + id 'net.neoforged.gradle.userdev' version '7.0.145' } version = mod_version diff --git a/gradle.properties b/gradle.properties index 7d6d995..75db601 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,8 +12,8 @@ neogradle.subsystems.parchment.mappingsVersion=2024.05.01 # The Minecraft version must agree with the Neo version to get a valid artifact minecraft_version=1.21 minecraft_version_range=[1.21,1.21.1) -neo_version=21.0.20-beta -neo_version_range=[21.0.20-beta,) +neo_version=21.0.150 +neo_version_range=[21.0.150,) loader_version_range=[4,) ## Mod Properties diff --git a/src/main/java/io/github/realyusufismail/tutorialmod/TutorialMod.java b/src/main/java/io/github/realyusufismail/tutorialmod/TutorialMod.java index 3a7a4e8..ff139d0 100644 --- a/src/main/java/io/github/realyusufismail/tutorialmod/TutorialMod.java +++ b/src/main/java/io/github/realyusufismail/tutorialmod/TutorialMod.java @@ -8,6 +8,7 @@ import net.neoforged.fml.ModList; import net.neoforged.fml.common.Mod; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.common.NeoForge; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -27,6 +28,7 @@ public TutorialMod(IEventBus bus) { bus.addListener(DataGenerators::gatherData); bus.addListener(Events::clientSetup); bus.addListener(TutorialModShieldItemRendererProvider::init); + NeoForge.EVENT_BUS.addListener(Events::onPlayerTickEventPre); bus.addListener(FMLClientSetupEvent.class, (fmlClientSetupEvent -> { fmlClientSetupEvent.enqueueWork(() -> { diff --git a/src/main/java/io/github/realyusufismail/tutorialmod/armour/ExampleArmour.java b/src/main/java/io/github/realyusufismail/tutorialmod/armour/ExampleArmour.java index 401aba8..c04ebd9 100644 --- a/src/main/java/io/github/realyusufismail/tutorialmod/armour/ExampleArmour.java +++ b/src/main/java/io/github/realyusufismail/tutorialmod/armour/ExampleArmour.java @@ -1,12 +1,89 @@ package io.github.realyusufismail.tutorialmod.armour; +import io.github.realyusufismail.tutorialmod.TutorialMod; import io.github.realyusufismail.tutorialmod.init.ArmorMaterialInit; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.effect.MobEffectInstance; +import net.minecraft.world.effect.MobEffects; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EquipmentSlot; +import net.minecraft.world.entity.ai.attributes.AttributeInstance; +import net.minecraft.world.entity.ai.attributes.AttributeModifier; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ArmorItem; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.level.Level; +import net.neoforged.neoforge.common.NeoForgeMod; import net.neoforged.neoforge.common.extensions.IItemExtension; +import net.neoforged.neoforge.common.extensions.IPlayerExtension; +import org.jetbrains.annotations.NotNull; + public class ExampleArmour extends ArmorItem implements IItemExtension { - public ExampleArmour(Type pType, int durability) { - super(ArmorMaterialInit.EXAMPLE_ARMOUR, pType, new Item.Properties().durability(pType.getDurability(durability))); + public static final ResourceLocation ENABLE_FLIGHT_RL = ResourceLocation.fromNamespaceAndPath(TutorialMod.MOD_ID, "enabling_flight"); + public static final AttributeModifier ENABLE_FLIGHT = + new AttributeModifier(ENABLE_FLIGHT_RL, 2.0D, AttributeModifier.Operation.ADD_VALUE); + + public ExampleArmour(Type type, int durability) { + super(ArmorMaterialInit.EXAMPLE_ARMOUR, type, new Item.Properties().durability(type.getDurability(durability))); + } + + public static void handleFlight(@NotNull Player player) { + ItemStack boots = player.getItemBySlot(EquipmentSlot.FEET); + ItemStack leggings = player.getItemBySlot(EquipmentSlot.LEGS); + ItemStack chestplate = player.getItemBySlot(EquipmentSlot.CHEST); + ItemStack helmet = player.getItemBySlot(EquipmentSlot.HEAD); + + if (player.isCreative() || player.isSpectator() || !player.isAlive()) { + return; + } + + AttributeInstance attributeInstance = player.getAttribute(NeoForgeMod.CREATIVE_FLIGHT); + + if (attributeInstance == null) { + TutorialMod.logger.error("Attribute instance is null"); + return; + } + + if (attributeInstance.getValue() == 1.0 && !attributeInstance.hasModifier(ENABLE_FLIGHT_RL)) { + return; // Creative flight is enabled, from another source + } + + if (isWearingFullSet(boots, leggings, chestplate, helmet)) { + if (!attributeInstance.hasModifier(ENABLE_FLIGHT_RL)) { + attributeInstance.addTransientModifier(ENABLE_FLIGHT); + } + + if (!player.hasEffect(MobEffects.REGENERATION)) { + // 100 is 00:05 + player.addEffect(new MobEffectInstance(MobEffects.REGENERATION, getMinutesInTicks(2), 0, false, false, true)); + } + } else { + if (attributeInstance.hasModifier(ENABLE_FLIGHT_RL)) { + attributeInstance.removeModifier(ENABLE_FLIGHT_RL); + } + + if (player.hasEffect(MobEffects.REGENERATION)) { + player.removeEffect(MobEffects.REGENERATION); + } + } + } + + private static boolean isWearingFullSet(@NotNull ItemStack boots, ItemStack leggings, ItemStack chestplate, ItemStack helmet) { + return boots.getItem() instanceof ExampleArmour && + leggings.getItem() instanceof ExampleArmour && + chestplate.getItem() instanceof ExampleArmour && + helmet.getItem() instanceof ExampleArmour; + } + + private static int getMinutesInTicks(int value) { + // get one second is 20 ticks + int oneSecond = 20; + // 20 : 1 second + int oneMinute = oneSecond * 60; + // So we multiply the value by one minute + return value * oneMinute; } -} +} \ No newline at end of file diff --git a/src/main/java/io/github/realyusufismail/tutorialmod/events/Events.java b/src/main/java/io/github/realyusufismail/tutorialmod/events/Events.java index f0f439c..9e5800f 100644 --- a/src/main/java/io/github/realyusufismail/tutorialmod/events/Events.java +++ b/src/main/java/io/github/realyusufismail/tutorialmod/events/Events.java @@ -1,9 +1,14 @@ package io.github.realyusufismail.tutorialmod.events; +import io.github.realyusufismail.tutorialmod.armour.ExampleArmour; import io.github.realyusufismail.tutorialmod.init.ItemInit; import net.minecraft.client.renderer.item.ItemProperties; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerPlayer; +import net.minecraft.world.entity.player.Player; +import net.minecraft.world.level.Level; import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent; +import net.neoforged.neoforge.event.tick.PlayerTickEvent; import java.util.Objects; @@ -16,4 +21,18 @@ public static void clientSetup(FMLClientSetupEvent event) { (pItemStack, pClientLevel, pLivingEntity, pPartialTicks) -> pLivingEntity != null && pLivingEntity.isUsingItem() && pLivingEntity.getUseItem() == pItemStack ? 1.0F : 0.0F); }); } + + public static void onPlayerTickEventPre(PlayerTickEvent.Pre event) { + // Handle player tick event here + Player player = event.getEntity(); + Level level = player.level(); + + if (level.isClientSide ) { + return; + } + + if (Objects.requireNonNull(level.getServer(), "Tick count is null").getTickCount() % 40 == 5) { + ExampleArmour.handleFlight(player); + } + } } diff --git a/src/main/resources/META-INF/accesstransformer.cfg b/src/main/resources/META-INF/accesstransformer.cfg index f16e132..f9fc762 100644 --- a/src/main/resources/META-INF/accesstransformer.cfg +++ b/src/main/resources/META-INF/accesstransformer.cfg @@ -1 +1 @@ -protected net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer entityModelSet # entityModelSet \ No newline at end of file +protected net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer entityModelSet \ No newline at end of file