Jump to content

[1.8] [SOLVED!] Changing vanilla mob attributes (zombie, skeleton etc.)


lorizz

Recommended Posts

Hello, I'm making a huge mod for minecraft and I'm currently stuck at changing vanilla mob attributes, I did something like when a mob spawns, it register it to my custom entity and change the attributes from there, like this:

 

When entity spawns:

@SubscribeEvent
public void onLivingSpawn(LivingSpawnEvent e) {
	if (e.entityLiving instanceof EntityMob) {
		EntityMob entity = (EntityMob) e.entityLiving;
		String entityName = entity.getDisplayName().getFormattedText().toLowerCase().replace("§r", "");
		MepCustomEntityLiving customEntity = MepEntityEnumRegistry
		 .getEntityFromName(entityName);
		entity.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(customEntity.getMaxHealth());
		// entity.getEntityAttribute(SharedMonsterAttributes.attackDamage)
		// .setBaseValue(customEntity.getStat("strength"));
		System.out.println(customEntity.getName().toUpperCase() + " changed successfully");
	}
}

 

MepCustomEntityLiving:

package com.thelorizz.mep.customclass;

import com.thelorizz.mep.MepMain;
import com.thelorizz.mep.enumeration.MepEntityFunctions;

public class MepCustomEntityLiving {

public MepEntityFunctions entityFunctions;
public double expGiven;
public double strength;
public double defense;
public double maxHealth;
public int tier;
public String name;

public MepCustomEntityLiving(int par1, String par2) {
	this.tier = par1;
	this.name = par2;
	this.expGiven = this.setByTier(tier, "expGiven");
	this.strength = this.setByTier(tier, "strength");
	this.defense = this.setByTier(tier, "defense");
	this.maxHealth = this.setByTier(tier, "maxHealth");
}

public double getStat(String par1) {
	switch (par1) {
	case "expGiven":
		return this.expGiven;
	case "strength":
		return this.strength;
	case "defense":
		return this.defense;
	default:
		return 0;
	}
}

public String getName() {
	return this.name;
}

public double getMaxHealth() {
	return this.maxHealth;
}

public int getTier() {
	return this.tier;
}

public static double random(int mult, int plus) {
	MepCustomEntityPlayer player = MepMain.instance.getCustomEntityPlayerInstance();
	double levelModifier = player.getCurrentLevel() / 2;
	return (float) ((Math.random() * mult + plus) + levelModifier);
}

public static double set(int amount) {
	MepCustomEntityPlayer player = MepMain.instance.getCustomEntityPlayerInstance();
	double levelModifier = player.getCurrentLevel() / 2;
	return (int) (amount + levelModifier);
}

public static double setByTier(int par1, String type) {
	switch (par1) {
	case 1:
		switch (type) {
		case "expGiven":
			return random(3, 4);
		case "strength":
			return random(2, 4);
		case "defense":
			return random(1, 4);
		case "maxHealth":
			return set(250);
		}
	case 2:
		switch (type) {
		case "expGiven":
			return random(7, 4);
		case "strength":
			return random(5, 4);
		case "defense":
			return random(7, 4);
		case "maxHealth":
			return set(400);
		}
	case 3:
		switch (type) {
		case "expGiven":
			return random(14, 4);
		case "strength":
			return random(16, 4);
		case "defense":
			return random(13, 4);
		case "maxHealth":
			return set(740);
		}
	case 4:
		switch (type) {
		case "expGiven":
			return random(28, 4);
		case "strength":
			return random(24, 4);
		case "defense":
			return random(21, 4);
		case "maxHealth":
			return set(1430);
		}
	default:
		return 0;
	}
}
}

 

And the function that register the mob in the registry:

public static MepCustomEntityLiving getEntityFromName(String name) {
	for(int i = 0; i < entities.length; i++) {
		if (name.equals(entities[i].getName())) {
			return new MepCustomEntityLiving(entities[i].getTier(), entities[i].getName());
		}
	}
	return null;
}

 

But it doesn't work, it only works the exp given randomly.

Link to comment
Share on other sites

You never do anything with

expGiven

after setting its value.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Are you sure you are getting a matching entry based on the entity's display name? What does your 'entities' array contain, and where did you get those names from?

 

Using the display name is a bad idea anyway - what if I'm playing in Chinese? The server has no idea what language I'm using, so it's expecting 'entity.zombie.name' or something like that, but I'm sending it 殭屍? Not gonna work.

Link to comment
Share on other sites

Are you sure you are getting a matching entry based on the entity's display name? What does your 'entities' array contain, and where did you get those names from?

 

Using the display name is a bad idea anyway - what if I'm playing in Chinese? The server has no idea what language I'm using, so it's expecting 'entity.zombie.name' or something like that, but I'm sending it 殭屍? Not gonna work.

 

Actually it works, but yes you're right, I need to change it.

I cannot edit the attributes

Link to comment
Share on other sites

EDIT: I found a solution but it returns a NullPointer in the ".applyModifier(modifier);" method:

@SubscribeEvent
public void onEntityConstructing(EntityConstructing e) {
if (e.entity instanceof EntityLiving) {
	EntityLiving entity = (EntityLiving) e.entity;
	String entityName = entity.getName();
	MepCustomEntityLiving customEntity = MepEntityEnumRegistry.getEntityFromName(entityName);
	if (customEntity != null) {
		double newHealth = customEntity.getMaxHealth();
		MepAttributeList attributeList = MepMain.ATTRIBUTELIST;
		if (!attributeList.exists()) {
			EntityPlayer player = (EntityPlayer) Minecraft.getMinecraft().thePlayer;
			AttributeModifier modifier = new AttributeModifier(entity.getUniqueID(), "maxHealthModifier",
					newHealth, 0);
			if (modifier != null) {
				entity.getAttributeMap().getAttributeInstance(SharedMonsterAttributes.maxHealth)
						.applyModifier(modifier);
			}
		}
	}
}
}

 

EDIT 2: Solved, I just changed the event to LivingSpawnEvent and added a .removeAllModifiers(); method over the .applyModifier(modifier) one.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Try deliting feur builder  It caused this issue in my modpack
    • I am not using hardcoded recipes, I'm using Vanilla's already existing code for leather armor dying. (via extending and implementing DyeableArmorItem / DyeableLeatherItem respectively) I have actually figured out that it's something to do with registering item colors to the ItemColors instance, but I'm trying to figure out where exactly in my mod's code I would be placing a call to the required event handler. Unfortunately the tutorial is criminally undescriptive. The most I've found is that it has to be done during client initialization. I'm currently trying to do the necessary setup via hijacking the item registry since trying to modify the item classes directly (via using SubscribeEvent in the item's constructor didn't work. Class so far: // mrrp mrow - mcmod item painter v1.0 - catzrule ch package catzadvitems.init; import net.minecraft.client.color.item.ItemColors; import net.minecraft.world.item.Item; import net.minecraftforge.registries.ObjectHolder; import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.client.event.ColorHandlerEvent; import catzadvitems.item.DyeableWoolArmorItem; @Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD) public class Painter { @ObjectHolder("cai:dyeable_wool_chestplate") public static final Item W_CHEST = null; @ObjectHolder("cai:dyeable_wool_leggings") public static final Item W_LEGS = null; @ObjectHolder("cai:dyeable_wool_boots") public static final Item W_SOCKS = null; public Painter() { // left blank, idk if forge throws a fit if constructors are missing, not taking the chance of it happening. } @SubscribeEvent public static void init(FMLClientSetupEvent event) { new Painter(); } @Mod.EventBusSubscriber private static class ForgeBusEvents { @SubscribeEvent public static void registerItemColors(ColorHandlerEvent.Item event) { ItemColors col = event.getItemColors(); col.register(DyeableUnderArmorItem::getItemDyedColor, W_CHEST, W_LEGS, W_SOCKS); //placeholder for other dye-able items here later.. } } } (for those wondering, i couldn't think of a creative wool helmet name)
    • nvm found out it was because i had create h and not f
    • Maybe there's something happening in the 'leather armor + dye' recipe itself that would be updating the held item texture?
    • @SubscribeEvent public static void onRenderPlayer(RenderPlayerEvent.Pre e) { e.setCanceled(true); model.renderToBuffer(e.getPoseStack(), pBuffer, e.getPackedLight(), 0f, 0f, 0f, 0f, 0f); //ToaPlayerRenderer.render(); } Since getting the render method from a separate class is proving to be bit of a brick wall for me (but seems to be the solution in older versions of minecraft/forge) I've decided to try and pursue using the renderToBuffer method directly from the model itself. I've tried this route before but can't figure out what variables to feed it for the vertexConsumer and still can't seem to figure it out; if this is even a path to pursue.  The vanilla model files do not include any form of render methods, and seem to be fully constructed from their layer definitions? Their renderer files seem to take their layers which are used by the render method in the vanilla MobRenderer class. But for modded entities we @Override this function and don't have to feed the method variables because of that? I assume that the render method in the extended renderer takes the layer definitions from the renderer classes which take those from the model files. Or maybe instead of trying to use a render method I should be calling the super from the renderer like   new ToaPlayerRenderer(context, false); Except I'm not sure what I would provide for context? There's a context method in the vanilla EntityRendererProvider class which doesn't look especially helpful. I've been trying something like <e.getEntity(), model<e.getEntity()>> since that generally seems to be what is provided to the renderers for context, but I don't know if it's THE context I'm looking for? Especially since the method being called doesn't want to take this or variations of this.   In short; I feel like I'm super super close but I have to be missing something obvious? Maybe this insane inane ramble post will provide some insight into this puzzle?
  • Topics

×
×
  • Create New...

Important Information

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