Jump to content

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


Recommended Posts

Posted

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

Posted
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

Posted (edited)
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
Posted
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.

Posted
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. 

Posted
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

Posted

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.
 

Posted
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?

Posted (edited)
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
Posted (edited)
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
Posted
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

Posted
12 hours ago, Luis_ST said:

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

It didn't help

Posted
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?

Posted
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

Posted
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?

Posted
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

Posted
18 hours ago, Luis_ST said:

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

If they are overriden I should use @Inject, right?

Posted
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, /*...*/)

?

Posted
5 hours ago, btuh said:

If they are overriden I should use @Inject, right?

Exactly.

The EntityDataAccessor should look like this:

SynchedEntityData.defineId(Chicken.class, /*...*/)
Posted
On 11/13/2023 at 11:14 PM, Luis_ST said:

Exactly.

The EntityDataAccessor should look like this:

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

Thank you so much for all help with mixin classes!

Topic closed.

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi,  I'm using Forge 47.3.0 for Minecraft 1.20.1 I apologise if this is obvious I am very new to modding for Minecraft. I sucessfully made a mod that launched without errors or crashes (without it doing anything) but in order to add the features I need, I need to add "Custom Portal API [Forge]" as a dependency. However no matter the way I've tried to acheive this, it crashes. I am pretty sure it's not the way I'm putting it in the repositories, the dependencies or the way I'm refrencing it, as I've a hundred diffrent combinations and multiple Maven methods. And on all those diffrent variations I still get this crash: pastebin.com/UhumzZCZ Any tips would be invaluable as I've been loosing my mind over this!
    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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