下面列出了怎么用net.minecraft.world.DifficultyInstance的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
if (!this.isChild()) {
int i = this.rand.nextInt(this.getVariantMax()) + 1; // Values 1 to 3
if (i == 3 && this.rand.nextInt(4) != 0) { // 1/4 chance it remains white (overall 1/12 chance of white)
i = this.rand.nextInt(2) + 1; // 1 - 2
}
if (livingdata instanceof TypeData) {
i = ((TypeData) livingdata).typeData;
} else {
livingdata = new TypeData(i);
}
this.setType(i);
}
return livingdata;
}
/**
* Called only once on an entity when first time spawned, via egg, mob
* spawner, natural spawning etc, but not called when entity is reloaded
* from nbt. Mainly used for initializing attributes and inventory
*/
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
setCanPickUpLoot(true);
setEquipmentBasedOnDifficulty(difficulty);
setEnchantmentBasedOnDifficulty(difficulty);
setHeldItem(EnumHand.MAIN_HAND, new ItemStack(Items.DIAMOND_SWORD, 1));
setHeldItem(EnumHand.OFF_HAND, new ItemStack(Items.SHIELD, 1));
// addArmor();
return livingdata;
}
private void spawnMount() {
if(isRiding() || !spawned) {
return;
}
EntityFallenMount mount = null;
if(Config.fallenMountEnabled && rand.nextFloat() <= Config.fallenKnightChanceMounted) {
mount = new EntityFallenMount(world);
mount.setLocationAndAngles(posX, posY, posZ, rotationYaw, 0.0F);
DifficultyInstance di = world.getDifficultyForLocation(new BlockPos(mount));
mount.onInitialSpawn(di, null);
//NB: don;t check for entity collisions as we know the knight will collide
if(!SpawnUtil.isSpaceAvailableForSpawn(world, mount, false)) {
mount = null;
}
}
if(mount != null) {
setCanPickUpLoot(false);
setCanBreakDoors(false);
world.spawnEntity(mount);
startRiding(mount);
}
}
@Override
public IEntityLivingData onInitialSpawn(DifficultyInstance di, IEntityLivingData livingData) {
spawned = true;
//From base entity living class
getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).applyModifier(new AttributeModifier("Random spawn bonus", rand.nextGaussian() * 0.05D, 1));
// func_189768_a(SkeletonType.NORMAL);//skeleton types do not exist anymore in 1.11.2. so its always normal.
addRandomArmor();
setEnchantmentBasedOnDifficulty(di); //enchantEquipment();
float f = di.getClampedAdditionalDifficulty();
this.setCanPickUpLoot(this.rand.nextFloat() < 0.55F * f);
setCanPickUpLoot(rand.nextFloat() < 0.55F * f);
setCanBreakDoors(rand.nextFloat() < f * 0.1F);
return livingData;
}
/**
* Gives armor or weapon for entity based on given DifficultyInstance
*/
protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
if (this.world.rand.nextInt(3) == 0) {
this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(ItemLoader.diamondTofuSword));
} else {
this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(ItemLoader.metalTofuSword));
}
}
/**
* Called only once on an entity when first time spawned, via egg, mob spawner, natural spawning etc, but not called
* when entity is reloaded from nbt. Mainly used for initializing attributes and inventory
*/
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
this.setEquipmentBasedOnDifficulty(difficulty);
this.setEnchantmentBasedOnDifficulty(difficulty);
return livingdata;
}
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
Biome biome = this.world.getBiome(new BlockPos(this));
if (biome instanceof BiomeZundaTofuPlains) {
this.setVariant(1);
} else {
this.setVariant(0);
}
return super.onInitialSpawn(difficulty, livingdata);
}
/**
* Called only once on an entity when first time spawned, via egg, mob spawner, natural spawning etc, but not called
* when entity is reloaded from nbt. Mainly used for initializing attributes and inventory
*/
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
IEntityLivingData ientitylivingdata = super.onInitialSpawn(difficulty, livingdata);
this.setEquipmentBasedOnDifficulty(difficulty);
this.setEnchantmentBasedOnDifficulty(difficulty);
return ientitylivingdata;
}
/**
* Gives armor or weapon for entity based on given DifficultyInstance
*/
protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
float f = this.world.getDifficultyForLocation(new BlockPos(this)).getAdditionalDifficulty();
if (this.rand.nextFloat() < f * 0.2F) {
this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(ItemLoader.TACHI));
} else {
this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(ItemLoader.KATANA));
}
}
@Override
public IEntityLivingData onInitialSpawn(DifficultyInstance diff, IEntityLivingData data)
{
data = super.onInitialSpawn(diff, data);
//this.setCarried(Blocks.CHEST);
//this.assignedGnode = new TileEntityGnomeCache();
return data;
}
@Override
public IEntityLivingData onInitialSpawn(DifficultyInstance diff, IEntityLivingData data)
{
data = super.onInitialSpawn(diff, data);
this.setCarriedToAir();
return data;
}
protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(Items.DIAMOND_SWORD));
setItemStackToSlot(EntityEquipmentSlot.HEAD, colorArmor(new ItemStack(Items.LEATHER_HELMET, 1), 8339378));
setItemStackToSlot(EntityEquipmentSlot.CHEST, colorArmor(new ItemStack(Items.LEATHER_CHESTPLATE, 1), 3361970));
setItemStackToSlot(EntityEquipmentSlot.LEGS, colorArmor(new ItemStack(Items.LEATHER_LEGGINGS, 1), 0xffff00));
setItemStackToSlot(EntityEquipmentSlot.FEET, colorArmor(new ItemStack(Items.LEATHER_BOOTS, 1), 10040115));
}
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
setCanPickUpLoot(false);
addArmor();
if (isEntityAlive()) {
setHasLord(true);
}
return livingdata;
}
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
setCanPickUpLoot(true);
setEquipmentBasedOnDifficulty(difficulty);
setEnchantmentBasedOnDifficulty(difficulty);
setHeldItem(EnumHand.MAIN_HAND, new ItemStack(Items.IRON_SWORD, 1));
addArmor();
return livingdata;
}
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
setLeftHanded(false);
setHeldItem(EnumHand.MAIN_HAND, new ItemStack(Items.GOLDEN_SWORD, 1));
setCanPickUpLoot(false);
addArmor();
return livingdata;
}
@Override
protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty)
{
super.setEquipmentBasedOnDifficulty(difficulty);
if (CyberwareConfig.KATANA && !CyberwareConfig.NO_CLOTHES && this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND) != null && this.getItemStackFromSlot(EntityEquipmentSlot.MAINHAND).getItem() == Items.IRON_SWORD)
{
this.setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(CyberwareContent.katana));
this.setDropChance(EntityEquipmentSlot.MAINHAND, 0F);
}
}
@Override
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
return this.initData(super.onInitialSpawn(difficulty, livingdata));
}
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
setEquipmentBasedOnDifficulty(difficulty);
return livingdata;
}
@Override
public IEntityLivingData finalizeMobSpawn(DifficultyInstance p_190672_1_, @Nullable IEntityLivingData p_190672_2_, boolean p_190672_3_) {
return p_190672_2_;
}
@Override
public IEntityLivingData finalizeMobSpawn(DifficultyInstance p_190672_1_, @Nullable IEntityLivingData p_190672_2_, boolean p_190672_3_) {
return p_190672_2_;
}
protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
setItemStackToSlot(EntityEquipmentSlot.MAINHAND, new ItemStack(Items.STONE_SWORD));
setItemStackToSlot(EntityEquipmentSlot.HEAD, colorArmor(new ItemStack(Items.LEATHER_HELMET, 1), 0xb0b0b0));
}
/**
* Called only once on an entity when first time spawned, via egg, mob
* spawner, natural spawning etc, but not called when entity is reloaded
* from nbt. Mainly used for initializing attributes and inventory
*/
@Nullable
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
livingdata = super.onInitialSpawn(difficulty, livingdata);
return livingdata;
}
@Override
public IEntityLivingData onInitialSpawn(DifficultyInstance di, IEntityLivingData data) {
setHorseArmorStack(ItemStack.EMPTY);
setHorseSaddled(true);
setGrowingAge(0);
getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(Config.fallenMountHealth);
getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.2);
getAttributeMap().getAttributeInstanceByName("horse.jumpStrength").setBaseValue(0.5);
setHealth(getMaxHealth());
float chanceOfArmor = world.getDifficulty() == EnumDifficulty.HARD ? Config.fallenMountChanceArmoredHard
: Config.fallenMountChanceArmored;
if(rand.nextFloat() <= chanceOfArmor) {
//Value between 0 and 1 (normal) - 1.5 based on how long a chunk has been occupied and the moon phase
//float occupiedDiffcultyMultiplier = worldObj.func_147462_b(posX, posY, posZ);
float occupiedDiffcultyMultiplier = di.getClampedAdditionalDifficulty();
//TODO: Do I need this normalised still?
occupiedDiffcultyMultiplier /= 1.5f; // normalize
float chanceImprovedArmor = world.getDifficulty() == EnumDifficulty.HARD ? Config.fallenMountChanceArmorUpgradeHard
: Config.fallenMountChanceArmorUpgrade;
chanceImprovedArmor *= (1 + occupiedDiffcultyMultiplier); //If we have the max occupied factor, double the chance of improved armor
int armorLevel = 0;
for (int i = 0; i < 2; i++) {
if(rand.nextFloat() <= chanceImprovedArmor) {
armorLevel++;
}
}
Item armorItem = Items.IRON_HORSE_ARMOR;
switch (armorLevel) {
case 1:
armorItem = Items.GOLDEN_HORSE_ARMOR;
break;
case 2:
armorItem = Items.DIAMOND_HORSE_ARMOR;
break;
}
armor = new ItemStack(armorItem);
setHorseArmorStack(armor);
} else {
armor = ItemStack.EMPTY;
setHorseArmorStack(armor);
}
return data;
}
@Override
public IEntityLivingData onInitialSpawn(DifficultyInstance di, IEntityLivingData livingData) {
spawned = true;
return super.onInitialSpawn(di, livingData);
}
@Nullable
@Override
public IEntityLivingData onInitialSpawn(DifficultyInstance difficulty, @Nullable IEntityLivingData livingdata) {
IEntityLivingData data = super.onInitialSpawn(difficulty, livingdata);
initTofuProfession();
updateEntityAI();
initTrades();
return data;
}
protected void setEquipmentBasedOnDifficulty(DifficultyInstance difficulty) {
}