簡體   English   中英

如何在仍然保持類型的同時訪問我正在使用的構造函數的構造函數

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

大約 18 小時前,我是一個建築 minecraft modder。 我正在嘗試創建一個ItemPickaxe類型的ItemPickaxe ,但也希望能夠修改ItemPickaxe的超級構造函數中的一個參數,該參數不是ItemPickaxe的參數ItemPickaxe 這是代碼的解釋,因為上面沒有什么意義。

這是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.

    }
}

這是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";
    }
}

我想要做的就是創建一個類extends itemTooltoolClass = "pickaxe"和一組自定義(在第三個參數ItemTool構造函數),但toolClass字符串是private變量。 我怎樣才能創建

  1. 一個擴展ItemPickaxe的類,但仍然將構造函數中的集合傳遞給它的ItemTool構造函數
  2. 一個擴展ItemTool並具有toolClass = "pickaxe"

ItemPickaxeItemTool不能被我修改。 否則我會公開toolClass變量。

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 =================================*/
}

唯一的方法是使用反射為toolClass ,覆蓋私有標志。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM