use of com.minecolonies.coremod.items.ItemAdventureToken in project minecolonies by Minecolonies.
the class EntityAIWorkNether method stayInNether.
/**
* Stay "in the Nether" and process the queues
*/
protected IAIState stayInNether() {
if (building.getVaultLocation() == null) {
// Ensure we stay put in the portal
final BlockPos portal = building.getPortalLocation();
if (portal != null && walkToBlock(portal, 1)) {
return getState();
}
if (!worker.isInvisible()) {
worker.setInvisible(true);
}
}
// This is the adventure loop.
if (!job.getCraftedResults().isEmpty()) {
ItemStack currStack = job.getCraftedResults().poll();
if (currStack.getItem() instanceof ItemAdventureToken) {
if (currStack.hasTag()) {
CompoundNBT tag = currStack.getTag();
if (tag.contains(TAG_DAMAGE)) {
int slotOfSwordStack = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getItemHandlerCitizen(), itemStack -> !ItemStackUtils.isEmpty(itemStack) && itemStack.getItem() instanceof SwordItem);
DamageSource source = new DamageSource("nether");
// Set up the mob to do battle with
EntityType<?> mobType = EntityType.ZOMBIE;
if (tag.contains(TAG_ENTITY_TYPE)) {
mobType = EntityType.byString(tag.getString(TAG_ENTITY_TYPE)).orElse(EntityType.ZOMBIE);
}
LivingEntity mob = (LivingEntity) mobType.create(world);
float mobHealth = mob.getHealth();
// Calculate how much damage the mob will do if it lands a hit (Before armor)
float incomingDamage = tag.getFloat(TAG_DAMAGE);
incomingDamage -= incomingDamage * (getSecondarySkillLevel() * SECONDARY_DAMAGE_REDUCTION);
setEquipSlot(EquipmentSlotType.MAINHAND, true);
setEquipSlot(EquipmentSlotType.HEAD, true);
setEquipSlot(EquipmentSlotType.CHEST, true);
setEquipSlot(EquipmentSlotType.LEGS, true);
setEquipSlot(EquipmentSlotType.FEET, true);
for (int hit = 0; mobHealth > 0 && !worker.isDeadOrDying(); hit++) {
// Clear anti-hurt timers.
worker.hurtTime = 0;
worker.invulnerableTime = 0;
float damageToDo = BASE_PHYSICAL_DAMAGE;
// Figure out who gets to hit who this round
boolean doDamage = worker.getRandom().nextBoolean();
boolean takeDamage = worker.getRandom().nextBoolean();
// Calculate if the sword still exists, how much damage will be done to the mob
if (slotOfSwordStack != -1) {
final ItemStack sword = worker.getInventoryCitizen().getStackInSlot(slotOfSwordStack);
if (!sword.isEmpty()) {
if (sword.getItem() instanceof SwordItem) {
damageToDo += ((SwordItem) sword.getItem()).getDamage();
} else {
damageToDo += TinkersToolHelper.getDamage(sword);
}
damageToDo += EnchantmentHelper.getDamageBonus(sword, mob.getMobType()) / 2.5;
if (doDamage) {
sword.hurtAndBreak(1, mob, entity -> {
});
}
}
}
// Hit the mob
if (doDamage) {
mobHealth -= damageToDo;
}
// Get hit by the mob
if (takeDamage && !worker.hurt(source, incomingDamage)) {
// Shouldn't get here, but if we do we can force the damage.
incomingDamage = worker.calculateDamageAfterAbsorbs(source, incomingDamage);
worker.setHealth(worker.getHealth() - incomingDamage);
}
// Every other round, heal up if possible, to compensate for all of this happening in a single tick.
if (hit % 2 == 0) {
float healAmount = checkHeal(worker);
final float saturationFactor = 0.25f;
if (healAmount > 0) {
worker.heal(healAmount);
worker.getCitizenData().decreaseSaturation(healAmount * saturationFactor);
}
} else {
if (worker.getCitizenData().getSaturation() < AVERAGE_SATURATION) {
attemptToEat();
}
}
}
if (worker.isDeadOrDying()) {
// Stop processing loot table data, as the worker died before finishing the trip.
InventoryUtils.clearItemHandler(worker.getItemHandlerCitizen());
job.getCraftedResults().clear();
job.getProcessedResults().clear();
return IDLE;
} else {
// Generate loot for this mob, with all the right modifiers
LootContext context = this.getLootContext();
LootTable loot = world.getServer().getLootTables().get(mob.getLootTable());
List<ItemStack> mobLoot = loot.getRandomItems(context);
job.addProcessedResultsList(mobLoot);
}
}
if (currStack.getTag().contains(TAG_XP_DROPPED)) {
worker.getCitizenExperienceHandler().addExperience(worker.getCitizenItemHandler().applyMending(currStack.getTag().getInt(TAG_XP_DROPPED)));
}
setEquipSlot(EquipmentSlotType.MAINHAND, false);
setEquipSlot(EquipmentSlotType.HEAD, false);
setEquipSlot(EquipmentSlotType.CHEST, false);
setEquipSlot(EquipmentSlotType.LEGS, false);
setEquipSlot(EquipmentSlotType.FEET, false);
}
} else {
int itemDelay = 0;
if (currStack.getItem() instanceof BlockItem) {
final BlockItem bi = (BlockItem) currStack.getItem();
final Block block = bi.getBlock();
final net.minecraftforge.common.ToolType toolType;
if (block.getHarvestTool(block.defaultBlockState()) == null) {
toolType = net.minecraftforge.common.ToolType.PICKAXE;
} else {
toolType = block.getHarvestTool(block.defaultBlockState());
}
int slotOfStack = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getItemHandlerCitizen(), itemStack -> !ItemStackUtils.isEmpty(itemStack) && itemStack.getItem().getToolTypes(itemStack).contains(toolType));
if (slotOfStack != -1) {
ItemStack tool = worker.getInventoryCitizen().getStackInSlot(slotOfStack);
if (tool.getItem() instanceof ToolItem) {
worker.setItemSlot(EquipmentSlotType.MAINHAND, tool);
for (int i = 0; i < currStack.getCount() && !tool.isEmpty(); i++) {
LootContext context = this.getLootContext();
LootTable loot = world.getServer().getLootTables().get(block.getLootTable());
List<ItemStack> mobLoot = loot.getRandomItems(context);
job.addProcessedResultsList(mobLoot);
tool.hurtAndBreak(1, worker, entity -> {
});
worker.getCitizenExperienceHandler().addExperience(worker.getCitizenItemHandler().applyMending(xpOnDrop(block)));
itemDelay += TICK_DELAY;
}
}
worker.setItemSlot(EquipmentSlotType.MAINHAND, ItemStack.EMPTY);
} else {
// we didn't have a tool to use.
itemDelay = TICK_DELAY;
}
} else {
job.addProcessedResultsList(ImmutableList.of(currStack));
itemDelay = TICK_DELAY * currStack.getCount();
}
setDelay(itemDelay);
}
return getState();
}
if (!job.getProcessedResults().isEmpty()) {
if (!worker.isDeadOrDying()) {
ItemStack item = job.getProcessedResults().poll();
if (InventoryUtils.addItemStackToItemHandler(worker.getItemHandlerCitizen(), item)) {
worker.decreaseSaturationForContinuousAction();
worker.getCitizenExperienceHandler().addExperience(0.2);
}
} else {
job.getProcessedResults().clear();
}
return getState();
}
return NETHER_RETURN;
}
use of com.minecolonies.coremod.items.ItemAdventureToken in project minecolonies by ldtteam.
the class EntityAIWorkNether method stayInNether.
/**
* Stay "in the Nether" and process the queues
*/
protected IAIState stayInNether() {
if (getOwnBuilding().getVaultLocation() == null) {
// Ensure we stay put in the portal
final BlockPos portal = getOwnBuilding().getPortalLocation();
if (portal != null && walkToBlock(portal, 1)) {
return getState();
}
if (!worker.isInvisible()) {
worker.setInvisible(true);
}
}
// This is the adventure loop.
if (!job.getCraftedResults().isEmpty()) {
ItemStack currStack = job.getCraftedResults().poll();
if (currStack.getItem() instanceof ItemAdventureToken) {
if (currStack.hasTag()) {
CompoundNBT tag = currStack.getTag();
if (tag.contains(TAG_DAMAGE)) {
int slotOfSwordStack = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getItemHandlerCitizen(), itemStack -> !ItemStackUtils.isEmpty(itemStack) && itemStack.getItem() instanceof SwordItem);
DamageSource source = new DamageSource("nether");
// Set up the mob to do battle with
EntityType<?> mobType = EntityType.ZOMBIE;
if (tag.contains(TAG_ENTITY_TYPE)) {
mobType = EntityType.byString(tag.getString(TAG_ENTITY_TYPE)).orElse(EntityType.ZOMBIE);
}
LivingEntity mob = (LivingEntity) mobType.create(world);
float mobHealth = mob.getHealth();
// Calculate how much damage the mob will do if it lands a hit (Before armor)
float incomingDamage = tag.getFloat(TAG_DAMAGE);
incomingDamage -= incomingDamage * (getSecondarySkillLevel() * SECONDARY_DAMAGE_REDUCTION);
setEquipSlot(EquipmentSlotType.MAINHAND, true);
setEquipSlot(EquipmentSlotType.HEAD, true);
setEquipSlot(EquipmentSlotType.CHEST, true);
setEquipSlot(EquipmentSlotType.LEGS, true);
setEquipSlot(EquipmentSlotType.FEET, true);
for (int hit = 0; mobHealth > 0 && !worker.isDeadOrDying(); hit++) {
// Clear anti-hurt timers.
worker.hurtTime = 0;
worker.invulnerableTime = 0;
float damageToDo = BASE_PHYSICAL_DAMAGE;
// Figure out who gets to hit who this round
boolean doDamage = worker.getRandom().nextBoolean();
boolean takeDamage = worker.getRandom().nextBoolean();
// Calculate if the sword still exists, how much damage will be done to the mob
if (slotOfSwordStack != -1) {
final ItemStack sword = worker.getInventoryCitizen().getStackInSlot(slotOfSwordStack);
if (!sword.isEmpty()) {
if (sword.getItem() instanceof SwordItem) {
damageToDo += ((SwordItem) sword.getItem()).getDamage();
} else {
damageToDo += TinkersToolHelper.getDamage(sword);
}
damageToDo += EnchantmentHelper.getDamageBonus(sword, mob.getMobType()) / 2.5;
if (doDamage) {
sword.hurtAndBreak(1, mob, entity -> {
});
}
}
}
// Hit the mob
if (doDamage) {
mobHealth -= damageToDo;
}
// Get hit by the mob
if (takeDamage && !worker.hurt(source, incomingDamage)) {
// Shouldn't get here, but if we do we can force the damage.
incomingDamage = worker.calculateDamageAfterAbsorbs(source, incomingDamage);
worker.setHealth(worker.getHealth() - incomingDamage);
}
// Every other round, heal up if possible, to compensate for all of this happening in a single tick.
if (hit % 2 == 0) {
float healAmount = checkHeal(worker);
final float saturationFactor = 0.25f;
if (healAmount > 0) {
worker.heal(healAmount);
worker.getCitizenData().decreaseSaturation(healAmount * saturationFactor);
}
} else {
if (worker.getCitizenData().getSaturation() < AVERAGE_SATURATION) {
attemptToEat();
}
}
}
if (worker.isDeadOrDying()) {
// Stop processing loot table data, as the worker died before finishing the trip.
InventoryUtils.clearItemHandler(worker.getItemHandlerCitizen());
job.getCraftedResults().clear();
job.getProcessedResults().clear();
return IDLE;
} else {
// Generate loot for this mob, with all the right modifiers
LootContext context = this.getLootContext();
LootTable loot = world.getServer().getLootTables().get(mob.getLootTable());
List<ItemStack> mobLoot = loot.getRandomItems(context);
job.addProcessedResultsList(mobLoot);
}
}
if (currStack.getTag().contains(TAG_XP_DROPPED)) {
worker.getCitizenExperienceHandler().addExperience(worker.getCitizenItemHandler().applyMending(currStack.getTag().getInt(TAG_XP_DROPPED)));
}
setEquipSlot(EquipmentSlotType.MAINHAND, false);
setEquipSlot(EquipmentSlotType.HEAD, false);
setEquipSlot(EquipmentSlotType.CHEST, false);
setEquipSlot(EquipmentSlotType.LEGS, false);
setEquipSlot(EquipmentSlotType.FEET, false);
}
} else {
int itemDelay = 0;
if (currStack.getItem() instanceof BlockItem) {
final BlockItem bi = (BlockItem) currStack.getItem();
final Block block = bi.getBlock();
final net.minecraftforge.common.ToolType toolType;
if (block.getHarvestTool(block.defaultBlockState()) == null) {
toolType = net.minecraftforge.common.ToolType.PICKAXE;
} else {
toolType = block.getHarvestTool(block.defaultBlockState());
}
int slotOfStack = InventoryUtils.findFirstSlotInItemHandlerNotEmptyWith(worker.getItemHandlerCitizen(), itemStack -> !ItemStackUtils.isEmpty(itemStack) && itemStack.getItem().getToolTypes(itemStack).contains(toolType));
if (slotOfStack != -1) {
ItemStack tool = worker.getInventoryCitizen().getStackInSlot(slotOfStack);
if (tool.getItem() instanceof ToolItem) {
worker.setItemSlot(EquipmentSlotType.MAINHAND, tool);
for (int i = 0; i < currStack.getCount() && !tool.isEmpty(); i++) {
LootContext context = this.getLootContext();
LootTable loot = world.getServer().getLootTables().get(block.getLootTable());
List<ItemStack> mobLoot = loot.getRandomItems(context);
job.addProcessedResultsList(mobLoot);
tool.hurtAndBreak(1, worker, entity -> {
});
worker.getCitizenExperienceHandler().addExperience(worker.getCitizenItemHandler().applyMending(xpOnDrop(block)));
itemDelay += TICK_DELAY;
}
}
worker.setItemSlot(EquipmentSlotType.MAINHAND, ItemStack.EMPTY);
} else {
// we didn't have a tool to use.
itemDelay = TICK_DELAY;
}
} else {
job.addProcessedResultsList(ImmutableList.of(currStack));
itemDelay = TICK_DELAY * currStack.getCount();
}
setDelay(itemDelay);
}
return getState();
}
if (!job.getProcessedResults().isEmpty()) {
if (!worker.isDeadOrDying()) {
ItemStack item = job.getProcessedResults().poll();
if (InventoryUtils.addItemStackToItemHandler(worker.getItemHandlerCitizen(), item)) {
worker.decreaseSaturationForContinuousAction();
worker.getCitizenExperienceHandler().addExperience(0.2);
}
} else {
job.getProcessedResults().clear();
}
return getState();
}
return NETHER_RETURN;
}
Aggregations