Skip to content

Instantly share code, notes, and snippets.

@aksource
Last active January 26, 2016 11:52
Show Gist options
  • Save aksource/67990bf5cb44e897253d to your computer and use it in GitHub Desktop.
Save aksource/67990bf5cb44e897253d to your computer and use it in GitHub Desktop.
package ak.sampleMod;
import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import javax.annotation.Nonnull;
/**
* 攻撃力可変ソードクラス
*/
public class SampleVariableSword extends ItemSword {
/** 属性保存のキー */
private static final String NBT_ATTRIBUTE_MODIFIERS_KEY = "AttributeModifiers";
/** 属性値名称保存キー */
private static final String NBT_ATTRIBUTE_MODIFIERS_NAME = "Name";
/** 属性名キー */
private static final String NBT_ATTRIBUTE_MODIFIERS_NAME_KEY = "AttributeName";
/** 属性値double valueキー */
private static final String NBT_ATTRIBUTE_MODIFIERS_AMOUNT = "Amount";
/** 属性値Opキー */
private static final String NBT_ATTRIBUTE_MODIFIERS_OPERATION = "Operation";
/** 攻撃力の属性値の名称 */
private static final String NBT_ATTRIBUTE_MODIFIERS_NAME_WEAPON = "Weapon modifier";
/** UUIDMostを表すlong値用のキー */
private static final String NBT_ATTRIBUTE_MODIFIERS_UUID_MOST = "UUIDMost";
/** UUIDLeastを表すlong値用のキー */
private static final String NBT_ATTRIBUTE_MODIFIERS_UUID_LEAST = "UUIDLeast";
public SampleVariableSword(ToolMaterial toolMaterial) {
super(toolMaterial);
GameRegistry.registerItem(this, "sample_variable_sword");
}
@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) {
if (!world.isRemote) {
int randAttack = world.rand.nextInt(100);
this.setAttribute(itemStack, player, SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(),
NBT_ATTRIBUTE_MODIFIERS_NAME_WEAPON, randAttack, 0);
}
return super.onItemRightClick(itemStack, world, player);
}
/**
* 武器の属性変更メソッド
* @param itemStack 変更したい武器のItemStack
* @param player 所有プレイヤー
* @param attributeKey 属性のMultiMap上でのKey文字列
* @param attributeName 属性のAttributeModifierName
* @param amount 属性の値
* @param op 属性のOp
*/
protected void setAttribute(@Nonnull ItemStack itemStack, @Nonnull EntityPlayer player, @Nonnull String attributeKey,
@Nonnull String attributeName, @Nonnull double amount, @Nonnull int op) {
NBTTagList nbtTagList;
NBTTagCompound nbtTagCompound;
if (!itemStack.hasTagCompound()) {
itemStack.setTagCompound(new NBTTagCompound());
}
if (itemStack.getTagCompound().hasKey(NBT_ATTRIBUTE_MODIFIERS_KEY, 9)) {
nbtTagList = itemStack.getTagCompound().getTagList(NBT_ATTRIBUTE_MODIFIERS_KEY, 10);
for (int tagIndex = 0; tagIndex < nbtTagList.tagCount(); tagIndex++) {
nbtTagCompound = nbtTagList.getCompoundTagAt(tagIndex);
if (attributeKey.equals(nbtTagCompound.getString(NBT_ATTRIBUTE_MODIFIERS_NAME_KEY))
&& attributeName.equals(nbtTagCompound.getString(NBT_ATTRIBUTE_MODIFIERS_NAME))) {
nbtTagCompound.setDouble(NBT_ATTRIBUTE_MODIFIERS_AMOUNT, amount);
break;
}
}
} else {
nbtTagList = new NBTTagList();
nbtTagCompound = new NBTTagCompound();
nbtTagCompound.setLong(NBT_ATTRIBUTE_MODIFIERS_UUID_MOST, Item.field_111210_e.getMostSignificantBits());
nbtTagCompound.setLong(NBT_ATTRIBUTE_MODIFIERS_UUID_LEAST, Item.field_111210_e.getLeastSignificantBits());
nbtTagCompound.setString(NBT_ATTRIBUTE_MODIFIERS_NAME_KEY, attributeKey);
nbtTagCompound.setString(NBT_ATTRIBUTE_MODIFIERS_NAME, attributeName);
nbtTagCompound.setDouble(NBT_ATTRIBUTE_MODIFIERS_AMOUNT, amount);
nbtTagCompound.setInteger(NBT_ATTRIBUTE_MODIFIERS_OPERATION, op);
nbtTagList.appendTag(nbtTagCompound);
if (!itemStack.hasTagCompound()) {
itemStack.setTagCompound(new NBTTagCompound());
}
itemStack.getTagCompound().setTag(NBT_ATTRIBUTE_MODIFIERS_KEY, nbtTagList);
}
player.getAttributeMap().applyAttributeModifiers(itemStack.getAttributeModifiers());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment