简体   繁体   English

如何在仍然保持类型的同时访问我正在使用的构造函数的构造函数

[英]How can I access the constructor of the constructor I'm using while still maintaining the type

I'm a buildding minecraft modder as of about 18 hours ago.大约 18 小时前,我是一个建筑 minecraft modder。 I'm trying to create an item which is of type ItemPickaxe , but would also like to be able to modify a parameter in the ItemPickaxe 's super constructor that's not one of the parameters for ItemPickaxe .我正在尝试创建一个ItemPickaxe类型的ItemPickaxe ,但也希望能够修改ItemPickaxe的超级构造函数中的一个参数,该参数不是ItemPickaxe的参数ItemPickaxe Here's an explanation with code as the above makes little sense.这是代码的解释,因为上面没有什么意义。

Here is the ItemPickaxe class constructor:这是ItemPickaxe类构造函数:

public class ItemPickaxe extends ItemTool
{
    protected ItemPickaxe(Item.ToolMaterial material)
    {
        super(2.0F, material, field); // 2.0F is an efficiency, material is the material, and field is a set of blocks that the tool works on.

    }
}

This is the ItemTool class constructor:这是ItemTool类的构造函数:

public class ItemTool extends Item
{

protected ItemTool(float p_i45333_1_, Item.ToolMaterial p_i45333_2_, Set p_i45333_3_)
{
    this.toolMaterial = p_i45333_2_;
    this.field_150914_c = p_i45333_3_;
    this.maxStackSize = 1;
    this.setMaxDamage(p_i45333_2_.getMaxUses());
    this.efficiencyOnProperMaterial = p_i45333_2_.getEfficiencyOnProperMaterial();
    this.damageVsEntity = p_i45333_1_ + p_i45333_2_.getDamageVsEntity();
    this.setCreativeTab(CreativeTabs.tabTools);
    if (this instanceof ItemPickaxe)
    {
        toolClass = "pickaxe";
    }
    else if (this instanceof ItemAxe)
    {
        toolClass = "axe";
    }
    else if (this instanceof ItemSpade)
    {
        toolClass = "shovel";
    }
}

What I'm trying to do is create a class that extends itemTool with toolClass = "pickaxe" and a custom set (the third parameter in the ItemTool constructor), but the toolClass string is a private variable.我想要做的就是创建一个类extends itemTooltoolClass = "pickaxe"和一组自定义(在第三个参数ItemTool构造函数),但toolClass字符串是private变量。 How can I create either我怎样才能创建

  1. a class extending ItemPickaxe and but still pass the set in the constructor to it's ItemTool constructor一个扩展ItemPickaxe的类,但仍然将构造函数中的集合传递给它的ItemTool构造函数
  2. a class extending ItemTool and have toolClass = "pickaxe"一个扩展ItemTool并具有toolClass = "pickaxe"

ItemPickaxe and ItemTool can't be modified by me. ItemPickaxeItemTool不能被我修改。 Otherwise I would have made the toolClass variable public.否则我会公开toolClass变量。

The rest of the ItemTool class was requested, and is as follows. ItemTool类的其余部分已被请求,如下所示。

package net.minecraft.item;


public class ItemTool extends Item
{
    private Set field_150914_c;
    protected float efficiencyOnProperMaterial = 4.0F;
    /** Damage versus entities. */
    private float damageVsEntity;
    /** The material this tool is made from. */
    protected Item.ToolMaterial toolMaterial;
    private static final String __OBFID = "CL_00000019";
    protected ItemTool(float p_i45333_1_, Item.ToolMaterial p_i45333_2_, Set p_i45333_3_)
{
    this.toolMaterial = p_i45333_2_;
    this.field_150914_c = p_i45333_3_;
    this.maxStackSize = 1;
    this.setMaxDamage(p_i45333_2_.getMaxUses());
    this.efficiencyOnProperMaterial = p_i45333_2_.getEfficiencyOnProperMaterial();
    this.damageVsEntity = p_i45333_1_ + p_i45333_2_.getDamageVsEntity();
    this.setCreativeTab(CreativeTabs.tabTools);
    if (this instanceof ItemPickaxe)
    {
        toolClass = "pickaxe";
    }
    else if (this instanceof ItemAxe)
    {
        toolClass = "axe";
    }
    else if (this instanceof ItemSpade)
    {
        toolClass = "shovel";
    }
}

public float func_150893_a(ItemStack p_150893_1_, Block p_150893_2_)
{
    return this.field_150914_c.contains(p_150893_2_) ? this.efficiencyOnProperMaterial : 1.0F;
}

/**
 * Current implementations of this method in child classes do not use the entry argument beside ev. They just raise
 * the damage on the stack.
 */
public boolean hitEntity(ItemStack p_77644_1_, EntityLivingBase p_77644_2_, EntityLivingBase p_77644_3_)
{
    p_77644_1_.damageItem(2, p_77644_3_);
    return true;
}

public boolean onBlockDestroyed(ItemStack p_150894_1_, World p_150894_2_, Block p_150894_3_, int p_150894_4_, int p_150894_5_, int p_150894_6_, EntityLivingBase p_150894_7_)
{
    if ((double)p_150894_3_.getBlockHardness(p_150894_2_, p_150894_4_, p_150894_5_, p_150894_6_) != 0.0D)
    {
        p_150894_1_.damageItem(1, p_150894_7_);
    }

    return true;
}

/**
 * Returns True is the item is renderer in full 3D when hold.
 */
@SideOnly(Side.CLIENT)
public boolean isFull3D()
{
    return true;
}

public Item.ToolMaterial func_150913_i()
{
    return this.toolMaterial;
}

/**
 * Return the enchantability factor of the item, most of the time is based on material.
 */
public int getItemEnchantability()
{
    return this.toolMaterial.getEnchantability();
}

/**
 * Return the name for this tool's material.
 */
public String getToolMaterialName()
{
    return this.toolMaterial.toString();
}

/**
 * Return whether this item is repairable in an anvil.
 */
public boolean getIsRepairable(ItemStack p_82789_1_, ItemStack p_82789_2_)
{
    ItemStack mat = this.toolMaterial.getRepairItemStack();
    if (mat != null && net.minecraftforge.oredict.OreDictionary.itemMatches(mat, p_82789_2_, false)) return true;
    return super.getIsRepairable(p_82789_1_, p_82789_2_);
}

/**
 * Gets a map of item attribute modifiers, used by ItemSword to increase hit damage.
 */
public Multimap getItemAttributeModifiers()
{
    Multimap multimap = super.getItemAttributeModifiers();
    multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(field_111210_e, "Tool modifier", (double)this.damageVsEntity, 0));
    return multimap;
}

/*===================================== FORGE START =================================*/
private String toolClass;
@Override
public int getHarvestLevel(ItemStack stack, String toolClass)
{
    int level = super.getHarvestLevel(stack, toolClass);
    if (level == -1 && toolClass != null && toolClass.equals(this.toolClass))
    {
        return this.toolMaterial.getHarvestLevel();
    }
    else
    {
        return level;
    }
}

@Override
public Set<String> getToolClasses(ItemStack stack)
{
    return toolClass != null ? ImmutableSet.of(toolClass) : super.getToolClasses(stack);
}

@Override
public float getDigSpeed(ItemStack stack, Block block, int meta)
{
    if (ForgeHooks.isToolEffective(stack, block, meta))
    {
        return efficiencyOnProperMaterial;
    }
    return super.getDigSpeed(stack, block, meta);
}
/*===================================== FORGE END =================================*/
}

The only way is to use reflection to assign a value to toolClass , overriding the private flag.唯一的方法是使用反射为toolClass ,覆盖私有标志。

https://stackoverflow.com/a/3239068/3622940 https://stackoverflow.com/a/3239068/3622940

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问 class 的私有构造函数? - How can I access a private constructor of a class? 有没有办法在调用这些方法时避免使用构造函数? - Is there a way I can avoid using a constructor while calling these methods? 如何从另一个类中的public类型的参数化构造函数中调用默认类型的参数化构造函数? - How can I call parameterized constructor of default type from a parameterized constructor of type public which is in another class? 如何以通用方式访问Java构造函数? - How can I access a Java constructor in a generic way? 如何从Constructor访问@Value注释变量? - How can I access to @Value annotated variable from Constructor? 如何在构造函数中传递枚举类型的值? - How can I pass a value of type enum in a constructor? 如何通过环绕构造函数替换类型的实例? - How can I replace the instanced of a type by wrapping around the constructor? 如何避免滥用具有多个相同类型参数的构造函数? - How can I avoid misuse of a constructor with multiple parameters of the same type? 如何使用Java中的反射确定泛型类型的构造函数参数数量? - How can I determine amount of constructor parameters for a generic type using reflections in java? 如何访问我的构造函数中定义的字段? - How to I access the fields defined in my constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM