Java 如何访问构造函数I';我在使用的同时仍在维护类型
大约18小时前,我是一名正在建造的地雷船改装工。我正在尝试创建一个类型为Java 如何访问构造函数I';我在使用的同时仍在维护类型,java,constructor,minecraft,Java,Constructor,Minecraft,大约18小时前,我是一名正在建造的地雷船改装工。我正在尝试创建一个类型为ItemPickaxe的项,但也希望能够修改ItemPickaxe的超级构造函数中的参数,该参数不是ItemPickaxe的参数之一。这里有一个解释,上面的代码没有什么意义 下面是ItemPickaxe类构造函数: public class ItemPickaxe extends ItemTool { protected ItemPickaxe(Item.ToolMaterial material) {
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.
}
}
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";
}
}
这是ItemTool
类构造函数:
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.
}
}
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";
}
}
我想做的是创建一个类,该类使用toolClass=“pickaxe”
扩展itemTool和一个自定义集(itemTool构造函数中的第三个参数),但是toolClass
字符串是一个private
变量。我怎样才能创建它们呢
ItemPickaxe
,但仍然将构造函数中的集合传递给它的ItemTool
构造函数ItemTool
,并具有toolClass=“pickaxe”
ItemPickaxe
和ItemTool
我无法修改。否则,我会将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 =================================*/
}
package net.minecraft.item;
公共类ItemTool扩展了Item
{
专用集字段_150914_c;
保护浮子效率性能材料=4.0F;
/**对实体的伤害*/
私人浮动损害;
/**这个工具是用什么材料制成的*/
受保护项目。工具材料工具材料;
私有静态最终字符串\uuu OBFID=“CL\u000000 19”;
受保护项目工具(浮动p_i45333_1_、项目工具材料p_i45333_2_、设置p_i45333_3_)
{
this.toolMaterial=p_i45333_2;
这个.field_150914_c=p_i45333_3;
此参数为.maxStackSize=1;
这个.setMaxDamage(p_i45333_2_.getMaxUses());
this.efficiencyonpropertimatial=p_i45333_2_.getefficiencyonpropertimatial();
this.damageVsEntity=p_i45333_1_+p_i45333_2_.getDamageVsEntity();
这个.setCreativeTab(CreativeTabs.tabTools);
if(项目PickAXE的此实例)
{
toolClass=“鹤嘴锄”;
}
else if(itemaxes的此实例)
{
toolClass=“axe”;
}
else if(ItemSpade的此实例)
{
toolClass=“电铲”;
}
}
公共浮动功能(项目堆栈p_150893_1_,块p_150893_2_)
{
返回此.field_150914_c.contains(p_150893_2_)?this.efficiencyOnProperty属性:1.0F;
}
/**
*当前在子类中此方法的实现不使用ev旁边的entry参数。它们只是引发
*堆栈上的损坏。
*/
公共布尔hitEntity(ItemStack p_77644_1_、EntityLivingBase p_77644_2_、EntityLivingBase p_77644_3_)
{
p_77644_1.损坏项目(2,p_77644_3);
返回true;
}
公共布尔onBlockDestroyed(项目堆栈p_150894_1_、世界p_150894_2_、块p_150894_3_、int p_150894_4_、int p_150894_5_、int p_150894_6_、EntityLivingBase p_150894_7_)
{
如果((双)p_150894_3_)块硬度(p_150894_2_,p_150894_4_,p_150894_5_,p_150894_6_)!=0.0D)
{
p_150894_1.损坏项目(1,p_150894_7);
}
返回true;
}
/**
*如果项目在按住时为全3D渲染器,则返回True。
*/
@SideOnly(Side.CLIENT)
公共布尔值isFull3D()
{
返回true;
}
public Item.ToolMaterial func_150913_i()
{
归还此工具材料;
}
/**
*返回物品的附魔因子,大多数情况下基于材质。
*/
public int getItemEnchantability()
{
返回此.toolMaterial.getEnchantability();
}
/**
*返回此工具的材质的名称。
*/
公共字符串getToolMaterialName()
{
返回此.toolMaterial.toString();
}
/**
*返回此项目是否可在铁砧中修复。
*/
公共布尔值getIsRepairable(ItemStack p\u 82789\u 1\u、ItemStack p\u 82789\u 2\u)
{
ItemStack mat=this.toolMaterial.getRepairItemStack();
if(mat!=null&&net.minecraftforge.oredict.OreDictionary.itemMatches(mat,p\u 82789\u 2,false))返回true;
返回super.getIsRepairable(p\u 82789\u 1,p\u 82789\u 2);
}
/**
*获取物品属性修饰符的映射,物品剑用来增加命中伤害。
*/
公共多映射getItemAttributeModifiers()
{
Multimap Multimap=super.getItemAttributeModifiers();
multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(),新的AttributeModifier(字段_111210_e,“工具修饰符”,(双精度)this.damageVsEntity,0));
返回多重映射;
}
/*=====================================================================锻造开始=================================*/
私有字符串工具类;
@凌驾
public int getHarvestLevel(ItemStack堆栈,字符串工具类)
{
int-level=super.getHarvestLevel(堆栈,工具类);
if(level==-1&&toolClass!=null&&toolClass.equals(this.toolClass))
{
返回此.toolMaterial.getHarvestLevel();
}
其他的
{
回报水平;
}
}
@凌驾
公共集GetToolClass(ItemStack堆栈)
{
return toolClass!=null?ImmutableSet.of(toolClass):super.gettoolclass(stack);
}
@凌驾
公共浮点getDigSpeed(ItemStack堆栈、块块、整型元)
{
if(ForgeHooks.istoleEffective(堆栈、块、元))
{
返回效率特性材料;
}
返回super.getDigSpeed(堆栈、块、元);
}
/*========================================================================锻造结束=================================*/
}
唯一的方法是使用反射为工具类
赋值,覆盖私有标志
您是否尝试过重载构造函数?如果没有ItemTool的更多源代码,就无法告诉您。如果toolClass是最终版本,那么您就被卡住了。如果不是,您可能可以用它做任何您想做的事情。我没有尝试重载构造函数,尽管我认为这不会有帮助,因为我不能重载ItemTool或ItemPickaxe。toolClass是在哪里定义的?我看不到这一点