#I want to add an ability to my sword to make it so if it hit a mob the mob gets glowing effect

269 messages · Page 1 of 1 (latest)

fickle fox
#

LivingEntity#addStatusEffect

quiet arrow
fickle fox
quiet arrow
#

nothing

#

why do i ned mixin?

#

i am adding smt to my custom item

fickle fox
#

🤦

#

sorry

quiet arrow
#

?

fickle fox
#

in multiple threads at once.

#

nvmind

#

yeah

#

fabric has a post attack event

#

right

quiet arrow
fickle fox
#

so event -> then call LivingEntity#addStatusEffect on the attacked with glowing

#

i can't find the event to give examples

#

you got it?

quiet arrow
fickle fox
#

k

#

lets do this then.

#

have you made a mod?

quiet arrow
#

i made a sword

#

called the glowing stick

#

and it has atk damage and atk speed

fickle fox
#

does it have a custom item class?

quiet arrow
#

kindof

#

it has a tool material class

fickle fox
#
net.minecraft.item.Item
#

has

#
public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker);
#

that you could override

quiet arrow
quiet arrow
fickle fox
#

that work?

#

oh.

#

so you make a class

quiet arrow
fickle fox
#

where do you register the item

quiet arrow
fickle fox
#

is the seccond one better?

quiet arrow
fickle fox
#

ah, just edit next time.

quiet arrow
#

alr

fickle fox
#

k

#

so your GlowingSword class

#

really should be GlowingSwordToolMaterial or GlowingToolMaterial

quiet arrow
#

why?

fickle fox
#

because i represents the material, not the sword itself

#

and you should make a GlowingSwordItem class

quiet arrow
fickle fox
#

that extends sword item

#

hold

#

so

quiet arrow
fickle fox
#

java has something called anonymous classes

quiet arrow
#

it has an error

fickle fox
#

hold.

quiet arrow
#

alr sorry

fickle fox
#

i'm try to make it so you can change as little as possible

quiet arrow
#

alr

fickle fox
#

when you do something like

#
Item item = new Item(/* settings */)
quiet arrow
#

ye

fickle fox
#

you can also do something like

#
Item item = new Item(/* settings */) {
  @Override
  public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
  super.postHit(stack, target, attacker)
}
}
quiet arrow
#

so the boolean checks if ur attacking?

#

and what is the item stack

fickle fox
#

idk what the boolean does.

#

let me check

#

looks like it

#

the ItemStack is the stack of the sword.

#

every item has an instance, that is registered with minecraft

quiet arrow
fickle fox
#

this instance can be used to make itemstacks

#

which is what players think of when they say item

fickle fox
#

disregarding weird moments.

#

the methods in your item class will only be called with an itemstack of your item

#

so, what point are you at?

quiet arrow
#

si put the boolean method with the ovveride under my register for the sword??

fickle fox
#
public static final Item glowsword = registerItem("glowsword",
new SwordItem(INSTANCE,new Item.Settings().rarity(Rarity.UNCOMMON).
attributeModifiers(SwordItem.createAttributeModifiers(INSTANCE,5,-2.4F))) {
  @Override
  public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
  return super.postHit(stack, target, attacker)
}
});
#

should work

#

did it work?

quiet arrow
#

no errors

fickle fox
#

great. ping me if you need me.

quiet arrow
#

him = fabric

fickle fox
#

him = the attackedEntity?

quiet arrow
#

ye

fickle fox
#

do you know of the maven docs?

quiet arrow
fickle fox
#

fabric has a documentation.

quiet arrow
#

ye ik

fickle fox
#

every class has a webpage detailing every feature it has

quiet arrow
#

i cant find post hit there

#

wait i found it

fickle fox
#

and every parameter is hyperlinked. you you can see where to get the perameters

#

can you do ctr + f?

#

i find that helpfull

quiet arrow
#

alr thanks ill look into it in the mean time i need go thx again

fickle fox
#

👍 ping me if you need me.

quiet arrow
#

@fickle fox

#

so i made a class item

#

ands used the example from the docs

#

and now when i right click it spawns lightning

#

i cant figure out how to do it with posthit tho

fickle fox
#

hey, im back

quiet arrow
#

so um

#

am stuck :-:

fickle fox
#

what breaks if you just put it in the postHit Method?

quiet arrow
#

posthit is a boolean

fickle fox
#

yeah so?

quiet arrow
#

i dont think u can return a livinentity from a boolean

#

i need to give the entity i hit an effect

fickle fox
#

yeah.

#

postHit is called with entities

#

the game calls it

quiet arrow
#

how to use it then?

fickle fox
#
package com.example;

import net.fabricmc.fabric.api.event.Event;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import javax.swing.*;
import java.util.Set;


public class GlowStick extends SwordItem {
    public GlowStick(ToolMaterial tl,Settings settings){
        super(tl,settings);

    }
   // eluded due to lacking nitro...
    public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
        if (target.getWorld().isClient || attacker.getWorld().isClient)
          return true;
        BlockPos targetPos = target.getBlockPos();
        LightningEntity lightningBolt = new LightningEntity(EntityType.LIGHTNING_BOLT, target.getWorld());
        lightningBolt.setPosition(targetPos.toCenterPos());
        target.getWorld().spawnEntity(lightningBolt);
        return true;
    }
}
#

like that?

#

@quiet arrow
u busy now?

quiet arrow
fickle fox
#

k

#

look right?

quiet arrow
fickle fox
#

what version are you in

quiet arrow
#

1.21

#

ye docs are outdated for me ;-;

fickle fox
#

look right now?

quiet arrow
fickle fox
#

what?

quiet arrow
fickle fox
#

i edited it.

quiet arrow
#

wait

#

what if we remove the if condition

fickle fox
#

no

quiet arrow
#

why

fickle fox
#

that would end badly

quiet arrow
#

why

#

doesnt posthit respond rtto entity being hit

fickle fox
#

because the code is called on both the client and the server

#

look at the code again

quiet arrow
quiet arrow
#

dw

fickle fox
#

caps was on

quiet arrow
#

btw we dont need to have it spawn lightning

fickle fox
#
package com.example;

import net.fabricmc.fabric.api.event.Event;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import javax.swing.*;
import java.util.Set;


public class GlowStick extends SwordItem {
    public GlowStick(ToolMaterial tl,Settings settings){
        super(tl,settings);

    }
   // eluded due to lacking nitro...
    public boolean postHit(ItemStack stack, LivingEntity target, LivingEntity attacker) {
        if (target.getWorld().isClient || attacker.getWorld().isClient)
          return true;
        BlockPos targetPos = target.getBlockPos();
        LightningEntity lightningBolt = new LightningEntity(EntityType.LIGHTNING_BOLT, target.getWorld());
        lightningBolt.setPosition(targetPos.toCenterPos());
        target.getWorld().spawnEntity(lightningBolt);
        return true;
    }
}
quiet arrow
#

the real reason is to give the target glow effect

#

works

quiet arrow
#

alr

#

imma afk for like 5 mins tho brb

fickle fox
#

k

#
target.addStatusEffect(new StatusEffectInstance(StatusEffects.GLOWING, 10 * 20, 1))
quiet arrow
#

alr am back

quiet arrow
#

how do u know

#

that

#

since i couldnt find it on the wiki

fickle fox
#

not the wiki

#

the docs.

#

the wiki is only usefull for beginers

#

and apparently i can't spell

#

let me show you

quiet arrow
#

alr

fickle fox
#

if you go here

#

and then here

#

you have a searchable list of every class minecraft offers

#

make some sense?

quiet arrow
fickle fox
#

did you get the glowing working?

quiet arrow
#

i didnt test yet but

#

why is there a return true under the if condition

fickle fox
#

did the reason not to do it on the client make sense?

fickle fox
quiet arrow
fickle fox
#

oh

quiet arrow
#

IT WORKS

fickle fox
#

if statements can have one statement under them. typically we put a block statement, such as {} but you can also put other types of statements there, such as a return statement

quiet arrow
#

it just does nothing?

fickle fox
#

yup

#

if client return true else nothing

quiet arrow
#

why tho the other code isnt under the if

clever whale
#

@quiet arrow how much java you know generaly? If you want to mod you should try and learn java first, like how classes, inheritance type etc

fickle fox
fickle fox
#

i don't recomend modding before java.

#

but it's the way i learned

clever whale
#

wdym

quiet arrow
#

like from what i know, ik all java

clever whale
#

coding languages you mean?

fickle fox
clever whale
fickle fox
#

@quiet arrow you consider yourself proficient in java?

quiet arrow
fickle fox
#

or you know all the java you know because u r modding

quiet arrow
fickle fox
#

hmm.

clever whale
# quiet arrow i mean not exacly

there are many tutorials online, even by youtube channels for minecraft modding that explain the basics and more java knowladge you will need

quiet arrow
clever whale
#

for modding yes, but not for java coding

fickle fox
#

not for modding itself, but the java you need to know to get into modding

fickle fox
#

if you look at the great minecraft mods

#

like enderio and extrautilities

#

like 10% is modding

#

the rest is java

quiet arrow
clever whale
#

like you said not exacly

quiet arrow
clever whale
#

if you mean you do not know java for coding than okay, but if you do not know java in some aspects it will be almost impossible to mod

fickle fox
#

we're always here to help, but sometimes, it can be hard if the person needing help is missing foundational concepts in java. learning those foundational concepts saves hours of banging your head against the wall. for us and for you

clever whale
fickle fox
quiet arrow
fickle fox
#

letme google

quiet arrow
fickle fox
#

hmm.

quiet arrow
#

i mean u seem not to know it

fickle fox
#

that is more applied java, not basics of java.

#

i did a unit on it back when i learned 2+ years ago.

#

honestly haven't used it sense.

#

but no

#

@clever whale how can i say basics of programing (Data Structures) (Iteration Concepts) (Memory Managment) (Threads) (Component Models (Server / Client)) (Functions / Methods) (Constructors)

clever whale
#

This is only for interface i believe, anyway this shouldnt be needed for minecraft, more learn the basic syntax, make sure you understand loops, conditionals, clases: inheritance, objects, methods (static, public, private, protected etc) constructors, interfaces: implementation

clever whale
#

classes and interfaces are most important as they are used throughout java (and minecraft) a lot

fickle fox
#

then practice is your freind.

clever whale
#

you should understand what inheritance and override means etc

clever whale
#

oh yeah

#

Generics too for some cases

quiet arrow
#

is there a good vid that explains it well

clever whale
#

GeeksForGeeks generaly has good text tutorials.

fickle fox
#

idk. like i said, I tought myself from minecraft, so I don't know where to find resources. but yt is probably a good place to start

fickle fox
clever whale
#

many sites offer tutorials a few i use when i get stuck with syntax are geeksforgeeks, w3school and baeldung they all explain the uses and works of the syntax and function pretty well