Leaderboard
Popular Content
Showing content with the highest reputation on 01/09/19 in all areas
-
If you create a new entity, you may have stumbled across the Attribute System, for example if you want a custom max. health value. Now a detailed explanation on what the Attribute System actually is can be found here: http://minecraft.gamepedia.com/Attribute Here I will show you how to create your own Attributes for your entity and modify all of them in code. NOTE: all attribute classes mentioned in this tutorial can be found in the package net.minecraft.entity.ai.attributes. Create a new Attribute Step 1: To create a new attribute, you first need to create a new IAttribute instance (preferably static final in your entity registry class or somewhere). Now you don't need to make a new class which implements that interface! Minecraft provides us with a standard class currently used for all its own Attributes called RangedAttribute. The constructor of that has the following parameters: IAttribute parentIn - a parent attribute if available, can be null String unlocalizedNameIn - the unlocalized name of the attribute. You can't add an attribute with the same name to an entity, so I usually precede the name with my Mod ID separated with a dot like TurretMod.MOD_ID + ".maxAmmoCapacity" double defaultValue - the default value of the attribute. This value is set as base value when the entity is first created. It can't be lower than the minimum nor higher than the maximum value or else it will throw an IllegalArgumentException double minimumValueIn - the minimum value of the attribute. This value is usually 0.0D. If the entity's attribute value falls below that, this value is used for it instead. It can't be bigger than the maximum value or else it will throw an IllegalArgumentException double maximumValueIn - the maximum value of the attribute. This value is usually Double.MAX_VALUE. If the entity's attribute value falls above that, this value is used for it instead. It can't be smaller than the minimum value or else it will throw an IllegalArgumentException Now at the end, my IAttribute instance variable looks like this: public static final IAttribute MAX_AMMO_CAPACITY = new RangedAttribute(null, TurretMod.MOD_ID + ".maxAmmoCapacity", 256.0D, 0.0D, Double.MAX_VALUE); But wait! If you want to share the attribute value and modifiers with the client, you'll need to call setShouldWatch(true) on that variable. The same way you can disable it, for whatever reason, with false instead of true as the parameter value. Conveniently it returns itself, so you can make a neat one-liner like this: public static final IAttribute MAX_AMMO_CAPACITY = new RangedAttribute(null, TurretMod.MOD_ID + ".maxAmmoCapacity", 256.0D, 0.0D, Double.MAX_VALUE).setShouldWatch(true); Step 2: Great, now you have this useless variable the entity doesn't know about. To make it useful and working with the entity, you first need to register it to the entity's attribute list. To do so, override applyEntityAttributes()(if you've not already done so while changing the entity's max. health) and call this.getAttributeMap().registerAttribute(MY_OWN_ATTRIBUTE_VARIABLE), where MY_OWN_ATTRIBUTE_VARIABLE is firstly an overly long name, but more importantly your variable of your attribute instance created in Step 1. Here's an example from my mod: @Override protected void applyEntityAttributes() { super.applyEntityAttributes(); // Make sure to ALWAYS call the super method, or else max. health and other attributes from Minecraft aren't registered!!! this.getAttributeMap().registerAttribute(TurretAttributes.MAX_AMMO_CAPACITY); } Step 3: The attribute is now instanciated and registered, but how do I use its value? Simple! Just call this.getEntityAttribute(MY_OWN_ATTRIBUTE_VARIABLE).getAttributeValue(). It returns a double. If you want it to be an int, just use the MathHelper from Minecraft, for example MathHelper.ceiling_double_int(doubleVal) Modify an Attribute Now, to modify an attribute, there are 2 ways of doing so: 1. modify its base value This is simple and straight forward, and as mentioned 2 times here so far, you may have already done this via the entity's max. health attribute. But to be complete, here's how you change the base value of an attribute with the example on the max. health: this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(40.0D); //sets the entity max. health to 40 HP (20 hearts) Please note: this does NOT get synched with the client! If you need to edit the base value, make sure you use packets if you want it to be shared (or change it on both sides). Otherwise use modifiers! 2. add modifiers to the attribute This is a "reversable" way of changing the attribute value w/o messing with the base value. What we will do here is essentally this: http://minecraft.gamepedia.com/Attribute#Modifiers That said, much like the Attribute Instance, we need to create our own AttributeModifier instance variable. Conveniently, there is already a class for us to use called AttributeModifier. It's constructor parameters are as follows: UUID idIn - the UUID of this modifier, much like the Attribute name, this needs to be unique per Attribute. I usually give it a pre-defined, hardcoded Ver. 4 UUID via UUID.fromString("MY-UUID"), which was generated on this site. Mine looks like this: UUID.fromString("e6107045-134f-4c54-a645-75c3ae5c7a27") String nameIn - the Modifier name, can be anything except empty, preferably readable and unique for the Attribute. Since the "uniqueness" of the modifier is already defined in the UUID, you can have duplicate names. double amountIn - the Modifier value, can be anything. Note that the attribute, as prevously explained, has a min/max value and it can't get past that! int operationIn - the Operation of the Modifier (careful, magic numbers over here!). Have a direct quote from the Minecraft Wiki: Brief explanation: Operation number is the value you feed the parameter. I also made an enum for it complete with javadoc for you to copy (with credits of course) if you want: https://github.com/SanAndreasP/SAPManagerPack/blob/master/java/de/sanandrew/core/manpack/util/EnumAttrModifierOperation.java Now I've covered the parameters, it's time to make a new instance like this: MY_CUSTOM_MODIFIER = new AttributeModifier(UUID.fromString("e6107045-134f-4c54-a645-75c3ae5c7a27"), "myCustomModifier420BlazeIt", 0.360D, EnumAttrModifierOperation.ADD_PERC_VAL_TO_SUM.ordinal()); You have the new modifier instance, but what do do with it? You can add or remove modifiers from an attribute like this (method names are self-explanatory): entity.getEntityAttribute(MY_ATTRIBUTE_INSTANCE).applyModifier(MY_CUSTOM_MODIFIER); entity.getEntityAttribute(MY_ATTRIBUTE_INSTANCE).removeModifier(MY_CUSTOM_MODIFIER); Alright, you know now everything there is to know about attributes... wait, there's one more thing: attributes are saved and loaded automatically by the entity, no need to do that manually! Just make sure the client knows about the Attribute (register it properly as I've shown).1 point
-
Yeah, my bad, you need DataFixers for TileEntities. Mojang has also recently open-sourced their datafixing library Mostly the harder to screw up1 point
-
Forge provides the MissingMappingsEvent, I think it also fires for tile entities. Don’t construct a ResourceLocation like that (domain+”:”+path), construct it with the overload that takes seperate parameters (domain, path)1 point
-
You should make a custom ItemStackHandler that extends ItemStackHandler in which you overwrite insertItem and extractItem, and check if the itemstack is valid. I have a small example of what I mean here: protected boolean canInsert(ItemStack stack, int slot) { return true; } protected boolean canExtract(ItemStack stack, int slot, int amount) { return true; } @Override public boolean isItemValid(int slot, @Nonnull ItemStack stack) { return this.canInsert(stack, slot); } @Nonnull @Override public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) { if (this.canInsert(stack, slot)) { return super.insertItem(slot, stack, simulate); } else { return stack; } } @Nonnull @Override public ItemStack extractItem(int slot, int amount, boolean simulate) { if (this.canExtract(this.getStackInSlot(slot), slot, amount)) { return super.extractItem(slot, amount, simulate); } else { return ItemStack.EMPTY; } } But this isn't necessarily needed, as you should be able to modify isItemValid when instantiating the ItemStackHandler: public final ItemStackHandler inv = new ItemStackHandler(1) { @Override public boolean isItemValid(int slot, @Nonnull ItemStack stack) { return super.isItemValid(slot, stack); //check stack. } }; Also, make sure you are exposing the ItemHandler capability, or your inventory will not work properly.1 point
-
I believe that isItemValid is a method in Slot. I have a custom inventory slot that extends the Slot class, and that's where I control which items can be placed in certain slots. Not sure if this is what you're looking for.1 point
-
Maybe I do not entirely understand you, but you want to change the Tile's ID, without losing the data from another world? I don't think there is any way to do this. If you change the ID, the game doesn't recognize the TE in the world, which will make it not load it into game. Why do you care about the data attached to this block so much? I'm guessing you are in a testing environment, so changes like this will break existing TEs, but you can just put down the block with the new TE id and it should work the same.1 point
-
Skill Trees Mod Basic Player Info Page Summary The Skill Trees mod is a small mod that adds an easy way for modders to implement and add skill trees to their mod and aims to provide maximum customizability should the default look not suit your needs. This mod also provides methods for skill tree makers and other modders to check if certain skills are obtained. This mod does not add skills into the game itself. If you would like to be a part of a mod that will, check this post out. And post to the forum here . Skills By default, there are two types of skills and attribute skills. Attribute skills make it easy to affect the player's attributes. As long as the skill is active and the player has this skill, the player's stats will be changed (i.e. Double damage, natural armor, speed, etc). These changes can be viewed in the Player Info Tab (Note: this tab will show all modifiers currently affecting the player). The base skill does not do anything much on its own but can be used as a requirement to do something (i.e. craft swords and other items, use certain tools and weapons, interact with mobs, etc). Modders can also choose to do something special when the skill is obtained or removed (give or take an item from player an item, code particle effects, etc). Skills can also be made to be togglable so that their effects aren't constant. A skill can also be made tickable, meaning you can have something happen every 20 ticks (1 second) the skill is active (night vision, underwater breathing, fire resistance, mana use, etc). Skills can also be linked to each other. So you can require having a certain skill before obtaining a more powerful skill or useful skill down the line. The skills can branch out into multiple trees... skill trees! A line will automatically be drawn between each connected skill. Skill Points By default, this mod does not add any items to the game that give the player skill points. However, it does provide methods for doing so in the API and a base Item.class called ItemSkillPoint that will give the player a number of skill points. How you want the player to get skill points is up to you! Skill Trees By default, you can view your skill trees by pressing 'k' (you can change this in settings). You'll be taken to the last viewed page or the Player Info Page. There are serval default backgrounds (the same as advancements) and an option to use your own. You can discern several pieces of information at a glance: whether or not you have the skill, if you have the requirements to get a skill, if it is active, and the skills parents. If a skill does not have a lock on it, you have already obtained that skill. If a skill has an unlocked lock, it means you are able to buy it, while a skill with a locked lock means its locked. An inactive skill is grey and an active skill is orange. Connected skills are connected with a line (the line can go either way; you do not need to place skills from right to left, you can place them in any format). You can get more info about a skill by hovering over it. That will display its name, requirements, and description (all of these can be defined in the lang file). By default, unfulfilled requirements are displayed as red and fulfilled requirements are green, names of obtained skills are yellow and unobtained are white, and descriptions are white (these can be changed at creation). To purchase a skill, simply click on it. The skill will become active once the skill is purchased. (Note: Once you click a skill, if you could have purchased it, you will!) If the skill is already purchased and the skill is togglable, then clicking it will toggle it instead. By default, skill trees are kept on death. You can change this in the config. (I have included my EasyConfig class in the mod, feel free to use it!). Skill Requirements Skill Requirements are very important because without them you would just be able to buy all the skills. There are 5 default requirements, but you can create your own easily by implementing ISkillRequirment. The first two added are the Name Requirement and Description Requirement. These two add names and the description to the Skill Tooltip and are automatically added and created for by default. There are a few methods to interact with them, but you will most likely not need them unless you are creating skills that act differently than the default. The next most common one is the parent requirement. This automatically created and added to the skill for you. The last two are Skill Point Requirement and Level Requirement. Ther former requires a certain number of skills points that are consumed when purchased, and the latter is the same but for levels. Commands This mod adds commands to interact with the skill tree. Currently, there are 4: reset, addPoints, give and remove. Type "/sk" for usage. Most arguments can be tabbed completed. Server Compatability This mod should be fully server compatible and has been tested. If any bugs are found please report them to the GitHub below. Download You can download it at curse here and visit the forum here Creating a Skill Tree For Documentation and an example Skill Tree and the soruce, visit the Github here. Example Skill Trees1 point