Gli effetti di stato, anche noti come effetti, sono una condizione che interessa un'entità. Possono essere positivi, negativi o neutrali in natura. Il gioco base applica questi effetti in vari modi, come cibi, pozioni ecc.
Il comando /effect può essere usato per applicare effetti su un'entità.
Effetti di Stato Personalizzati
In questo tutorial aggiungeremo un nuovo effetto personalizzato chiamato Tater che ti darà un punto esperienza in ogni tick di gioco.
Estendere StatusEffect
Creiamo una classe per il nostro effetto personalizzato estendendo StatusEffect, che è la classe base per tutti gli effetti.
java
public class TaterEffect extends MobEffect {
protected TaterEffect() {
// category: StatusEffectCategory - describes if the effect is helpful (BENEFICIAL), harmful (HARMFUL) or useless (NEUTRAL)
// color: int - Color is the color assigned to the effect (in RGB)
super(MobEffectCategory.BENEFICIAL, 0xe9b8b3);
}
// Called every tick to check if the effect can be applied or not
@Override
public boolean shouldApplyEffectTickThisTick(int duration, int amplifier) {
// In our case, we just make it return true so that it applies the effect every tick
return true;
}
// Called when the effect is applied.
@Override
public boolean applyEffectTick(ServerLevel world, LivingEntity entity, int amplifier) {
if (entity instanceof Player) {
((Player) entity).giveExperiencePoints(1 << amplifier); // Higher amplifier gives you experience faster
}
return super.applyEffectTick(world, entity, amplifier);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Registrare il tuo Effetto Personalizzato
Come nella registrazione di blocchi e oggetti, usiamo Registry.register per registrare i nostri effetti personalizzati nel registro STATUS_EFFECT. Questo può essere fatto nel nostro initializer.
java
public class ExampleModEffects implements ModInitializer {
public static final Holder<MobEffect> TATER =
Registry.registerForHolder(BuiltInRegistries.MOB_EFFECT, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "tater"), new TaterEffect());
@Override
public void onInitialize() {
// ...
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Traduzioni e Texture
Puoi assegnare un nome al tuo effetto di stato e fornire un'icona che apparirà nello schermo dell'inventario del giocatore.
Texture
L'icona dell'effetto è un PNG 18x18. Posiziona la tua icona personalizzata in:
resources/assets/example-mod/textures/mob_effect/tater.png
Traduzioni
Come ogni altra traduzione, puoi aggiungere una voce con formato ID "effect.example-mod.<effect-identifier>": "Valore" al file di lingua.
json
{
"effect.example-mod.tater": "Tater"
}1
2
3
2
3
Fase di Test
Usa il comando /effect give @p example-mod:tater per dare al giocatore il nostro effetto Tater. Usa /effect clear @p example-mod:tater per rimuovere l'effetto.
INFO
Per creare una pozione che usa questo effetto, per favore vedi la guida Pozioni.





