Skip to content

Commit

Permalink
done with custom armour effects
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Aug 4, 2024
1 parent d9cfe78 commit 227f9b0
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 7 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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(() -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -1 +1 @@
protected net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer entityModelSet # entityModelSet
protected net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer entityModelSet

0 comments on commit 227f9b0

Please sign in to comment.