Skip to content

Commit

Permalink
Made it where swords set item on fire
Browse files Browse the repository at this point in the history
  • Loading branch information
RealYusufIsmail committed Aug 7, 2024
1 parent 723eff6 commit 2fb9c6a
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class TutorialMod {
public static Logger logger = LoggerFactory.getLogger(TutorialMod.class);

public TutorialMod(IEventBus bus) {
IEventBus neoForgeBus = NeoForge.EVENT_BUS;

ItemInit.ITEMS.register(bus);
BlockInit.BLOCKS.register(bus);
ArmorMaterialInit.ARMOR_MATERIALS.register(bus);
Expand All @@ -28,7 +30,8 @@ public TutorialMod(IEventBus bus) {
bus.addListener(DataGenerators::gatherData);
bus.addListener(Events::clientSetup);
bus.addListener(TutorialModShieldItemRendererProvider::init);
NeoForge.EVENT_BUS.addListener(Events::onPlayerTickEventPre);
neoForgeBus.addListener(Events::onPlayerTickEventPre);
neoForgeBus.addListener(Events::onEntityDamage);

bus.addListener(FMLClientSetupEvent.class, (fmlClientSetupEvent -> {
fmlClientSetupEvent.enqueueWork(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@

import io.github.realyusufismail.tutorialmod.TutorialMod;
import io.github.realyusufismail.tutorialmod.init.ArmorMaterialInit;
import io.github.realyusufismail.tutorialmod.util.TutorialModUtils;
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;


Expand All @@ -31,44 +28,44 @@ public ExampleArmour(Type type, int 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);
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;
}
if (player.isCreative() || player.isSpectator() || !player.isAlive()) {
return;
}

AttributeInstance attributeInstance = player.getAttribute(NeoForgeMod.CREATIVE_FLIGHT);
AttributeInstance attributeInstance = player.getAttribute(NeoForgeMod.CREATIVE_FLIGHT);

if (attributeInstance == null) {
TutorialMod.logger.error("Attribute instance is null");
return;
}
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 (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 (isWearingFullSet(boots, leggings, chestplate, helmet)) {
if (!attributeInstance.hasModifier(ENABLE_FLIGHT_RL)) {
attributeInstance.addTransientModifier(ENABLE_FLIGHT);
}

if (!player.hasEffect(MobEffects.REGENERATION)) {
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.addEffect(new MobEffectInstance(MobEffects.REGENERATION, TutorialModUtils.getTimeInTicks(1, TutorialModUtils.TimeUnit.MINUTES),
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);
}
if (player.hasEffect(MobEffects.REGENERATION)) {
player.removeEffect(MobEffects.REGENERATION);
}
}
}

private static boolean isWearingFullSet(@NotNull ItemStack boots, ItemStack leggings, ItemStack chestplate, ItemStack helmet) {
Expand All @@ -77,13 +74,4 @@ private static boolean isWearingFullSet(@NotNull ItemStack boots, ItemStack legg
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
Expand Up @@ -36,8 +36,8 @@ public void render(ThrownTrident pEntity, float pEntityYaw, float pPartialTick,
ItemRenderer.getFoilBufferDirect(
pBuffer,
tridentModel.renderType(getTextureLocation(pEntity)),
false,
pEntity.isFoil());
false,
pEntity.isFoil());
tridentModel.renderToBuffer(pPoseStack, vertexConsumer, pPackedLight, OverlayTexture.NO_OVERLAY);
pPoseStack.popPose();
super.render(pEntity, pEntityYaw, pPartialTick, pPoseStack, pBuffer, pPackedLight);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.blaze3d.vertex.PoseStack;
import io.github.realyusufismail.tutorialmod.TutorialMod;
import io.github.realyusufismail.tutorialmod.client.render.ModBlockEntityWithoutLevelRenderer;
import io.github.realyusufismail.tutorialmod.init.ItemInit;
import net.minecraft.client.model.TridentModel;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.renderer.MultiBufferSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,18 @@
import io.github.realyusufismail.tutorialmod.TutorialMod;
import io.github.realyusufismail.tutorialmod.init.BlockInit;
import io.github.realyusufismail.tutorialmod.init.ItemInit;
import io.github.realyusufismail.tutorialmod.init.TagsInit;
import net.minecraft.advancements.*;
import net.minecraft.advancements.critereon.InventoryChangeTrigger;
import net.minecraft.advancements.critereon.ItemPredicate;
import net.minecraft.advancements.critereon.MinMaxBounds;
import net.minecraft.core.HolderLookup;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.level.ItemLike;
import net.neoforged.neoforge.common.data.AdvancementProvider;
import net.neoforged.neoforge.common.data.ExistingFileHelper;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

public class ModAdvancementGenerator implements AdvancementProvider.AdvancementGenerator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import net.minecraft.core.HolderLookup;
import net.minecraft.data.PackOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemRandomChanceCondition;
import net.neoforged.neoforge.common.data.GlobalLootModifierProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,22 @@

import io.github.realyusufismail.tutorialmod.armour.ExampleArmour;
import io.github.realyusufismail.tutorialmod.init.ItemInit;
import io.github.realyusufismail.tutorialmod.items.ModSwordItem;
import io.github.realyusufismail.tutorialmod.util.TutorialModUtils;
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.item.ItemStack;
import net.minecraft.world.level.Level;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.common.damagesource.DamageContainer;
import net.neoforged.neoforge.event.entity.living.LivingDamageEvent;
import net.neoforged.neoforge.event.tick.PlayerTickEvent;

import java.util.Objects;

import static io.github.realyusufismail.tutorialmod.util.TutorialModUtils.getTimeInTicks;

public class Events {

public static void clientSetup(FMLClientSetupEvent event) {
Expand All @@ -27,12 +33,28 @@ public static void onPlayerTickEventPre(PlayerTickEvent.Pre event) {
Player player = event.getEntity();
Level level = player.level();

if (level.isClientSide ) {
if (level.isClientSide) {
return;
}

if (Objects.requireNonNull(level.getServer(), "Tick count is null").getTickCount() % 40 == 5) {
ExampleArmour.handleFlight(player);
}
}

/**
* This method is called when a player is damaged by an entity.
*
* @param event The event that is fired when a player is damaged by an entity.
* @see DamageContainer
*/
public static void onEntityDamage(LivingDamageEvent.Post event) {
ItemStack weapon = event.getSource().getWeaponItem();
if (weapon != null) {
if (weapon.getItem() instanceof ModSwordItem) {
event.getEntity()
.igniteForTicks(getTimeInTicks(30, TutorialModUtils.TimeUnit.SECONDS));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.github.realyusufismail.tutorialmod.TutorialMod;
import io.github.realyusufismail.tutorialmod.armour.ExampleArmour;
import io.github.realyusufismail.tutorialmod.items.ModShieldItem;
import io.github.realyusufismail.tutorialmod.items.ModSwordItem;
import net.minecraft.world.item.ArmorItem;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.PickaxeItem;
Expand All @@ -17,14 +18,14 @@ public class ItemInit {

public static final DeferredItem<Item> RAW_EXAMPLE = ITEMS.register("raw_example", () -> new Item(new Item.Properties()));

public static final DeferredItem<Item> SWORD = ITEMS.register("sword", () -> new SwordItem(TierInit.SWORD,
public static final DeferredItem<ModSwordItem> SWORD = ITEMS.register("sword", () -> new ModSwordItem(TierInit.SWORD,
new Item.Properties()
.attributes(SwordItem.createAttributes(
TierInit.SWORD,
// The type-specific attack damage bonus. 3 for swords, 1.5 for shovels, 1 for pickaxes, varying for axes and hoes.
8,
// The type-specific attack speed modifier. The player has a default attack speed of 4, so to get to the desired
// value of 1.6f, we use -2.4f. -2.4f for swords, -3f for shovels, -2.8f for pickaxes, varying for axes and hoes.
// The type-specific attack damage. Its calculated by adding this and the attack damage bonus, set in the tier.
// We have 5 here and an attack damage bonus of 1, so the sword will do 7 damage, as an extra 1 is added by default.
5,
// The type-specific attack speed modifier.
-2.6f
))
.stacksTo(1)));
Expand All @@ -33,10 +34,7 @@ public class ItemInit {
new Item.Properties()
.attributes(PickaxeItem.createAttributes(
TierInit.PICKAXE,
// The type-specific attack damage bonus. 3 for swords, 1.5 for shovels, 1 for pickaxes, varying for axes and hoes.
5,
// The type-specific attack speed modifier. The player has a default attack speed of 4, so to get to the desired
// value of 1.6f, we use -2.4f. -2.4f for swords, -3f for shovels, -2.8f for pickaxes, varying for axes and hoes.
-2.8f
))
.stacksTo(1)));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.github.realyusufismail.tutorialmod.items;

import net.minecraft.world.item.SwordItem;
import net.minecraft.world.item.Tier;

public class ModSwordItem extends SwordItem {
public ModSwordItem(Tier pTier, Properties pProperties) {
super(pTier, pProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.github.realyusufismail.tutorialmod.util;

import org.jetbrains.annotations.NotNull;

public class TutorialModUtils {
private TutorialModUtils() {
throw new IllegalStateException("Utility class");
}

private static final int TICKS_PER_SECOND = 20;
private static final int TICKS_PER_MINUTE = TICKS_PER_SECOND * 60;

public static int getTimeInTicks(int time, @NotNull TimeUnit unit) {
return switch (unit) {
case MINUTES -> time * TICKS_PER_MINUTE;
case SECONDS -> time * TICKS_PER_SECOND;
};
}

public enum TimeUnit {
MINUTES,
SECONDS
}
}

0 comments on commit 2fb9c6a

Please sign in to comment.