Jump to content

[1.20.1] How do I modify Minecraft classes when my mod is loading


btuh

Recommended Posts

I'm creating a mod that should modify Chicken class and EggItem class. Chicken class should extend my AbstractChicken class and EggItem shouldn't be throwable anymore, and extend the BlockItem class. Is it possible to make?

And yeah, I've already tried mixins

Link to comment
Share on other sites

23 minutes ago, btuh said:

Chicken class should extend my AbstractChicken class

What did you change in your AbstractChicken class? Ideally show your code

24 minutes ago, btuh said:

EggItem shouldn't be throwable anymore

This can be done with PlayerInteractEvent.RightClickItem

25 minutes ago, btuh said:

and extend the BlockItem class

Am I right that you want the EggItem to place a block on right click?
If yes you can use PlayerInteractEvent.RightClickBlock to place a Block at the location the players right clicked an Block

Link to comment
Share on other sites

16 hours ago, Luis_ST said:

What did you change in your AbstractChicken class? Ideally show your code

AbstrcatChicken should be a superclass of the minecraft's Chicken class, my Rooster class, and my Chick class.

Here it is (unfinished yet):

import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.level.Level;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;


public abstract class AbstractChicken extends Animal {
    protected AbstractChicken(EntityType<? extends AbstractChicken> entityType, Level world) {
        super(entityType, world);
    }

    protected SoundEvent getAmbientSound() {
        return SoundEvents.CHICKEN_AMBIENT;
    }

    protected SoundEvent getHurtSound(@NotNull DamageSource damageSource) {
        return SoundEvents.CHICKEN_HURT;
    }

    protected SoundEvent getDeathSound() {
        return SoundEvents.CHICKEN_DEATH;
    }

    @Nullable
    @Override
    public AgeableMob getBreedOffspring(ServerLevel level,
                                        AgeableMob mobBredWith) { // AbstractChicken's subclasses should also return null in this method
        return null;
    }

    @Override
    public abstract boolean isBaby(); // for Chicken and Rooster it should be false, and for Chick it should be true

    @Override
    public abstract boolean canMate(@NotNull Animal other); // Chick will always return false, and Chicken can only mate with Rooster, and vice versa

    @Override
    public abstract void setBaby(boolean newBoolean); // should be overriden by Chick to become a chicken or a rooster

    public abstract boolean isBoy(); // Rooster returns true, Chicken - false, Chick - determines on his NBT
    
    public void setBoy(boolean boy) {
        throw new UnsupportedOperationException(); // Chick shuold override it
    }
}

Also I want to add some NBT to both of these classes (Chicken and EggItem), how do I do that?

Edited by btuh
Link to comment
Share on other sites

On 11/2/2023 at 10:11 AM, btuh said:

AbstrcatChicken should be a superclass of the minecraft's Chicken class, my Rooster class, and my Chick class.

The changes you want to implement in Vanilla Chicken require the use of Mixin, which I do not recommend.

10 hours ago, btuh said:

Okay I think I'll just use the EntityJoinLevelEvent

But before you replace all the vanilla chickens with a custom entity, it may be the best solution you have.

Link to comment
Share on other sites

On 11/3/2023 at 11:39 PM, Luis_ST said:

But before you replace all the vanilla chickens with a custom entity, it may be the best solution you have.

Okay, I have one more question. For example, someone plays a world without this mod. Then, this user loads the world with my mod. And when it happens, all vanilla chickens should be automatically replaced with my chickens. 

Link to comment
Share on other sites

On 11/4/2023 at 11:55 AM, btuh said:

Okay, I have one more question. For example, someone plays a world without this mod. Then, this user loads the world with my mod. And when it happens, all vanilla chickens should be automatically replaced with my chickens.

Please do not replace the vanilla entities, this can and will cause conflicts with other mods.

You should in this case mixin, you can take a look at https://github.com/SpongePowered/Mixin/wiki/Mixins-on-Minecraft-Forge

Link to comment
Share on other sites

Create an interface with the methods you want to add to the Chicken class. Then implement this interface and all its methods in your ChickenMixin class.
If you need to change vanilla methods, use @Inject. If your mixin is properly registered, you can use instanceof to get an instance of your interface from a Chicken object.
Note that it is possible that your IDE will show you an error message that Chicken is not an instance of your interface, you can ignore that.

I admit the documentation for mixin is a bit more complicated as you need a good understanding of java, if you need help I will be happy to give you an example.
 

Link to comment
Share on other sites

20 hours ago, Luis_ST said:

Create an interface with the methods you want to add to the Chicken class. Then implement this interface and all its methods in your ChickenMixin class.
If you need to change vanilla methods, use @Inject. If your mixin is properly registered, you can use instanceof to get an instance of your interface from a Chicken object.
Note that it is possible that your IDE will show you an error message that Chicken is not an instance of your interface, you can ignore that.

I admit the documentation for mixin is a bit more complicated as you need a good understanding of java, if you need help I will be happy to give you an example.
 

I almost forgot that you can use interfaces((

Also can I replace the code that already exists in Chicken class.

this.goalSelector.addGoal(2, new BreedGoal(this, 1.0D));

See, this line of code in Chicken's registerGoals() method makes it possible to be bred with another chicken. But I want it to look like this:

this.goalSelector.addGoal(2, new BreedGoal(this, 1.0D, Rooster.class));

so that chickens can be only bred with roosters.

How do I do this one?

Link to comment
Share on other sites

On 11/8/2023 at 9:14 PM, Luis_ST said:

If you need to change vanilla methods, use @Inject.

The mixin should look like this:

@Mixin(Chicken.class)
public class ChickenMixin {
	
	// ...
	
	@Inject(method = "registerGoals", at = @At("HEAD"), cancellable = true)
	protected void registerGoals(CallbackInfo callback) {
		// Add all goals of the chick here
		// The modify them in the way you want
		callback.cancel(); // Required to prevent vanilla logic from being running
	}
	
	// ...
	
}
Edited by Luis_ST
Link to comment
Share on other sites

1 hour ago, Luis_ST said:

The mixin should look like this:

@Mixin(Chicken.class)
public class ChickenMixin {
	
	// ...
	
	@Inject(method = "registerGoals", at = @At("HEAD"), cancellable = true)
	protected void registerGoals(CallbackInfo callback) {
		// Add all goals of the chick here
		// The modify them in the way you want
		callback.cancel(); // Required to prevent vanilla logic from being running
	}
	
	// ...
	
}

Thx

 

@Inject(method = "setLastHurtByMob", at = @At("TAIL"))
public void setLastHurtByMob(@Nullable LivingEntity hurtByMob) {
    if (hurtByMob != null) {
            level().getEntitiesOfClass(Rooster.class,
                            new AABB(blockPosition().below(20).north(20).west(20),
                                    blockPosition().above(20).south(20).east(20)))
                    .forEach(r -> {
                        if (r.getPersistentAngerTarget() == null
                        	&& r.getTarget() == null) {
                            r.setTarget(hurtByMob);
                            r.setPersistentAngerTarget(hurtByMob.getUUID());
                            r.startPersistentAngerTimer();
                        }
                    });
        }
}

my IDE says "Cannot resolve method 'setLastHurtByMob' in target class". How do I modify this method (like this) if it's not in Chicken class, but in Mob class?

My guesses that you just should remove the @Inject annotation

Edited by btuh
Link to comment
Share on other sites

2 hours ago, btuh said:

my IDE says "Cannot resolve method 'setLastHurtByMob' in target class". How do I modify this method (like this) if it's not in Chicken class, but in Mob class?

The method is missing a Callback parameter, if the method has a returntype you have to use CallbackInfoReturnable else CallbackInfo

Link to comment
Share on other sites

16 hours ago, Luis_ST said:

Please post the updated code (full class) and the error you get

MixinChicken.java

package mymod.mixin;


import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.goal.*;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.entity.animal.Chicken;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import mymod._Entities;
import mymod.entities.chicken.IAbstractChicken;
import mymod.entities.chicken.Rooster;

@Mixin(Chicken.class)
public class MixinChicken extends Animal implements IAbstractChicken {

    @Shadow @Final private static Ingredient FOOD_ITEMS;

    @Inject(method = "isFood", at = @At("HEAD"), cancellable = true)
    public void isFood(ItemStack item, CallbackInfoReturnable<Boolean> cir) {
        cir.setReturnValue(FOOD_ITEMS.test(item));
    }

    protected MixinChicken(EntityType<? extends Animal> p_27557_, Level p_27558_) {
        super(p_27557_, p_27558_);
    }

    @Inject(method = "getBreedOffspring*", at = @At("RETURN"), cancellable = true)
    public void getBreedOffspring(ServerLevel level, AgeableMob mob, CallbackInfoReturnable<AgeableMob> cir) {
        cir.setReturnValue(null);
    }

    @Inject(method = "registerGoals", at = @At("HEAD"), cancellable = true)
    protected void registerGoals(CallbackInfo ci) {
        goalSelector.addGoal(0, new FloatGoal(this));
        goalSelector.addGoal(1, new PanicGoal(this, 1.4));
        goalSelector.addGoal(2, new BreedGoal(this, 1.0D, Rooster.class));
        goalSelector.addGoal(3, new TemptGoal(this, 1.0D, FOOD_ITEMS, false));
        goalSelector.addGoal(5, new WaterAvoidingRandomStrollGoal(this, 1.0D));
        goalSelector.addGoal(6, new LookAtPlayerGoal(this, Player.class, 6.0F));
        goalSelector.addGoal(7, new RandomLookAroundGoal(this));
        ci.cancel();
    }

    @Inject(method = "setLastHurtByMob", at = @At("TAIL")) // IDEA says "Cannot resolve method 'setLastHurtByMob' in target class" here
    public void setLastHurtByMob(@Nullable LivingEntity hurtByMob, CallbackInfo ci) {

        if (hurtByMob != null) {
            level().getEntitiesOfClass(Rooster.class,
                            new AABB(blockPosition().below(20).north(20).west(20),
                                    blockPosition().above(20).south(20).east(20)))
                    .forEach(r -> {
                        if (r.getPersistentAngerTarget() == null
                            && r.getTarget() == null) {
                            r.setTarget(hurtByMob);
                        }
                    });
        }
    }

    @Nullable
    @Override
    public AgeableMob getBreedOffspring(@NotNull ServerLevel p_146743_, @NotNull AgeableMob p_146744_) { // "Name does not match the pattern for added mixin members: ".+[_$].+" " here, ...
        return null;
    }

    @Override
    public boolean isBaby() { // ... same here, ...
        return false;
    }

    @Override
    public void setBaby(boolean newBoolean) { // ... here, ...
        if (newBoolean) {
            Chick me = _Entities.CHICK.get().create(level());
            me.setPos(position());
            me.setBoy(false);
            discard();
        }
    }

    @Override
    public boolean canMate(@Nullable Animal other) { // ... here, ...
         if (this == other) return false;
      	 else if (!(other instanceof Rooster)) return false;
      	 return this.isInLove() && other.isInLove();
    }

    @Override
    public boolean isBoy() { // ... and here
        return false;
    }
}

IAbstractChicken.java

package mymod.entities.chicken;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.AgeableMob;
import net.minecraft.world.entity.animal.Animal;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.Ingredient;

public interface IAbstractChicken {


//   private <T extends Animal> T self() {
//       if (this instanceof AbstractChicken || this instanceof net.minecraft.world.entity.animal.Chicken) {
//           return (T) this;
//       }
//       return null;
//   }

    Ingredient FOOD_ITEMS
            = Ingredient.of(Items.WHEAT_SEEDS,
            Items.MELON_SEEDS, Items.PUMPKIN_SEEDS,
            Items.BEETROOT_SEEDS, Items.TORCHFLOWER_SEEDS, Items.PITCHER_POD);


    default AgeableMob getBreedOffspring(ServerLevel serverWorld, AgeableMob other) {
        return null;
    }

    boolean isBaby();

    void setBaby(boolean newBoolean);

    boolean canMate(Animal other);

    default void setBoy(boolean newBoolean) {
        throw new UnsupportedOperationException();
    }

    boolean isBoy();

    default boolean isFood(ItemStack item) {
        return FOOD_ITEMS.test(item);
    }
}

Anything else?

Link to comment
Share on other sites

7 hours ago, btuh said:
@Inject(method = "setLastHurtByMob", at = @At("TAIL")) // IDEA says "Cannot resolve method 'setLastHurtByMob' in target class" here
public void setLastHurtByMob(@Nullable LivingEntity hurtByMob, CallbackInfo ci) 

First of all your Mixin class should be abstract.

Sorry, I didn't mention that if the method you want to change is not implemented in the target class, you can just override it like you would without mixin.

2 hours ago, btuh said:

Maybe I need to mixin in LivingEntity class and inject it there

You should use as few mixins as possible

Link to comment
Share on other sites

16 minutes ago, Luis_ST said:

First of all your Mixin class should be abstract.

Sorry, I didn't mention that if the method you want to change is not implemented in the target class, you can just override it like you would without mixin.

You should use as few mixins as possible

Ok

Is it important to annotate methods and fields with @Unique if they don't exist in target class? And if I want to add some extra NBT to the vanilla chicken, should I just override defineSynchedData(), readAdditionalSaveData() and addAdditionalSaveData() as they do that in custom entities?

Link to comment
Share on other sites

2 hours ago, btuh said:

Is it important to annotate methods and fields with @Unique if they don't exist in target class?

I personally don't use it in my mods, you can look at the generated class files in run/.mixin.out to see if there is a difference.

 

2 hours ago, btuh said:

And if I want to add some extra NBT to the vanilla chicken, should I just override defineSynchedData(), readAdditionalSaveData() and addAdditionalSaveData() as they do that in custom entities?

If they are not overriden in the vanilla chicken then yes

Link to comment
Share on other sites

19 hours ago, Luis_ST said:

... If they are not overriden in the vanilla chicken then yes

And one more question: if I define an EntityDataAccessor, should it look like this:

SynchedEntityData.defineId(Chicken.class, /*...*/)

or like this:

SynchedEntityData.defineId(MixinChicken.class, /*...*/)

?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.