Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make barbarians place ladders and break walls #2856

Merged
merged 12 commits into from
Sep 10, 2018
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public static class Gameplay
@Config.Comment("The max size of a barbarian horde")
public int maxBarbarianSize = 20;

@Config.Comment("Whether or not to barbarians can break, scale, bridge obstacles")
public boolean doBarbariansBreakThroughWalls = true;

@Config.Comment("The average amount of nights between raids")
public int averageNumberOfNightsBetweenRaids = 3;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.minecolonies.api.util.constant;

import net.minecraft.potion.Potion;

/**
* Barbarian constants class.
*/
public final class BarbarianConstants
{
/**
* The amount of EXP to drop on entity death.
*/
public static final int BARBARIAN_EXP_DROP = 1;

/**
* The range for the barb to move away.
*/
public static final int MOVE_AWAY_RANGE = 4;

public static final int BARBARIAN_HORDE_DIFFICULTY_FIVE = 5;

/**
* Values used to choose whether or not to play sound
*/
public static final int OUT_OF_ONE_HUNDRED = 100;

public static final int ONE = 1;

/**
* Values used for sword effect.
*/
public static final Potion SPEED_EFFECT = Potion.getPotionById(1);
public static final int TIME_TO_COUNTDOWN = 240;
public static final int COUNTDOWN_SECOND_MULTIPLIER = 4;
public static final int SPEED_EFFECT_DISTANCE = 7;
public static final int SPEED_EFFECT_DURATION = 240;
public static final int SPEED_EFFECT_MULTIPLIER = 2;

/**
* Amount of ladders to place before destroying blocks.
*/
public static final int LADDERS_TO_PLACE = 10;

/**
* Amount of ticks to despawn the barbarian.
*/
public static final int TICKS_TO_DESPAWN = Constants.TICKS_SECOND * Constants.SECONDS_A_MINUTE * 10;

/**
* Randomly execute it every this ticks.
*/
public static final int EVERY_X_TICKS = 20;

/**
* Private constructor to hide implicit one.
*/
private BarbarianConstants()
{
/*
* Intentionally left empty.
*/
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,21 @@ public final class NbtTagConstants
public static final String TAG_PROGRESS_LIST = "progressList";
public static final String TAG_PRINT_PROGRESS = "printProgrss";

/**
* String to store the existing time to NBT.
*/
public static final String TAG_TIME = "time";

/**
* String to store the stuck counter to NBT.
*/
public static final String TAG_STUCK_COUNTER = "stuck";

/**
* String to store the ladder counter to NBT.
*/
public static final String TAG_LADDER_COUNTER = "ladder";

/**
* Private constructor to hide the implicit one.
*/
Expand Down
47 changes: 2 additions & 45 deletions src/main/java/com/minecolonies/coremod/entity/EntityCitizen.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Objects;

Expand All @@ -68,20 +67,13 @@
import static com.minecolonies.api.util.constant.NbtTagConstants.*;
import static com.minecolonies.api.util.constant.Suppression.INCREMENT_AND_DECREMENT_OPERATORS_SHOULD_NOT_BE_USED_IN_A_METHOD_CALL_OR_MIXED_WITH_OTHER_OPERATORS_IN_AN_EXPRESSION;
import static com.minecolonies.api.util.constant.Suppression.UNCHECKED;
import static com.minecolonies.api.util.constant.TranslationConstants.CITIZEN_RENAME_NOT_ALLOWED;
import static com.minecolonies.api.util.constant.TranslationConstants.CITIZEN_RENAME_SAME;
import static com.minecolonies.api.util.constant.TranslationConstants.COM_MINECOLONIES_COREMOD_MOURN;
import static com.minecolonies.api.util.constant.TranslationConstants.*;

/**
* The Class used to represent the citizen entities.
*/
public class EntityCitizen extends AbstractEntityCitizen
{
/**
* The navigator field of the citizen.
*/
private static Field navigatorField;

/**
* The New PathNavigate navigator.
*/
Expand Down Expand Up @@ -189,7 +181,7 @@ public EntityCitizen(final World world)
this.enablePersistence();
this.setAlwaysRenderNameTag(Configurations.gameplay.alwaysRenderNameTag);
this.newNavigator = new PathNavigate(this, world);
updateNavigatorField();
this.navigator = newNavigator;
if (CompatibilityUtils.getWorld(this).isRemote)
{
setRenderDistanceWeight(RENDER_DISTANCE_WEIGHT);
Expand All @@ -199,41 +191,6 @@ public EntityCitizen(final World world)
initTasks();
}

/**
* Method used to update the navigator field.
* Gets the minecraft path navigate through reflection.
*/
private synchronized void updateNavigatorField()
{
if (navigatorField == null)
{
final Field[] fields = EntityLiving.class.getDeclaredFields();
for (@NotNull final Field field : fields)
{
if (field.getType().equals(net.minecraft.pathfinding.PathNavigate.class))
{
field.setAccessible(true);
navigatorField = field;
break;
}
}
}

if (navigatorField == null)
{
throw new IllegalStateException("Navigator field should not be null, contact developers.");
}

try
{
navigatorField.set(this, this.newNavigator);
}
catch (final IllegalAccessException e)
{
Log.getLogger().error("Navigator error", e);
}
}

/**
* Initiates citizen tasks
* Suppressing Sonar Rule Squid:S881
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,42 @@

import com.minecolonies.api.configuration.Configurations;
import com.minecolonies.api.util.CompatibilityUtils;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.coremod.colony.Colony;
import com.minecolonies.coremod.colony.ColonyManager;
import com.minecolonies.coremod.entity.ai.mobs.util.BarbarianSpawnUtils;
import com.minecolonies.coremod.entity.ai.mobs.util.BarbarianUtils;
import com.minecolonies.coremod.entity.pathfinding.PathNavigate;
import com.minecolonies.coremod.items.ItemChiefSword;
import com.minecolonies.coremod.sounds.BarbarianSounds;
import net.minecraft.entity.IEntityLivingData;
import net.minecraft.entity.item.EntityXPOrb;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.util.DamageSource;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.world.DifficultyInstance;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;
import java.util.Random;

import static com.minecolonies.api.util.constant.BarbarianConstants.*;
import static com.minecolonies.api.util.constant.NbtTagConstants.*;

/**
* Abstract for all Barbarian entities.
*/
public abstract class AbstractEntityBarbarian extends EntityMob
{
/**
* String to store the existing time to NBT.
*/
private static final String TAG_TIME = "time";

/**
* The amount of EXP to drop on entity death.
*/
private static final int BARBARIAN_EXP_DROP = 1;

private static final int BARBARIAN_HORDE_DIFFICULTY_FIVE = 5;

/**
* Values used to choose whether or not to play sound
* The New PathNavigate navigator.
*/
private static final int OUT_OF_ONE_HUNDRED = 100;

private static final int ONE = 1;

/**
* Values used for sword effect.
*/
private static final Potion SPEED_EFFECT = Potion.getPotionById(1);
private static final int TIME_TO_COUNTDOWN = 240;
private static final int COUNTDOWN_SECOND_MULTIPLIER = 4;
private static final int SPEED_EFFECT_DISTANCE = 7;
private static final int SPEED_EFFECT_DURATION = 240;
private static final int SPEED_EFFECT_MULTIPLIER = 2;

/**
* Amount of ticks to despawn the barbarian.
*/
private static final int TICKS_TO_DESPAWN = Constants.TICKS_SECOND * Constants.SECONDS_A_MINUTE * 5;

/**
* Randomly execute it every this ticks.
*/
private static final int EVERY_X_TICKS = 20;
private PathNavigate newNavigator;

/**
* Sets the barbarians target colony on spawn Thus it never changes.
Expand All @@ -79,19 +48,32 @@ public abstract class AbstractEntityBarbarian extends EntityMob
* Random object.
*/
private final Random random = new Random();

/**
* Current count of ticks.
*/
private int currentCount = 0;

/**
* The world time when the barbarian spawns.
*/
private long worldTimeAtSpawn = 0;

/**
* The current tick since creation.
*/
private int currentTick = 1;

/**
* Amount of time the barb got stuck.
*/
private int stuckCounter = 0;

/**
* Amount of time the barb got stuck.
*/
private int ladderCounter = 0;

/**
* Constructor method for Abstract Barbarians.
*
Expand Down Expand Up @@ -235,6 +217,42 @@ public void onLivingUpdate()
super.onLivingUpdate();
}

/**
* Get the stack counter.
* @return the amount it got stuck already.
*/
public int getStuckCounter()
{
return stuckCounter;
}

/**
* Set the stack counter.
* @param stuckCounter the amount.
*/
public void setStuckCounter(final int stuckCounter)
{
this.stuckCounter = stuckCounter;
}

/**
* Get the ladder counter.
* @return the amount it got stuck and placed a ladder already.
*/
public int getLadderCounter()
{
return ladderCounter;
}

/**
* Set the ladder counter.
* @param ladderCounter the amount.
*/
public void setLadderCounter(final int ladderCounter)
{
this.ladderCounter = ladderCounter;
}

@Override
protected SoundEvent getHurtSound(final DamageSource damageSourceIn)
{
Expand All @@ -251,13 +269,18 @@ protected SoundEvent getDeathSound()
public NBTTagCompound writeToNBT(final NBTTagCompound compound)
{
compound.setLong(TAG_TIME, worldTimeAtSpawn);
compound.setInteger(TAG_STUCK_COUNTER, stuckCounter);
compound.setInteger(TAG_LADDER_COUNTER, ladderCounter);
return super.writeToNBT(compound);
}

@Override
public void readFromNBT(final NBTTagCompound compound)
{
worldTimeAtSpawn = compound.getLong(TAG_TIME);
stuckCounter = compound.getInteger(TAG_STUCK_COUNTER);
ladderCounter = compound.getInteger(TAG_LADDER_COUNTER);

super.readFromNBT(compound);
}

Expand All @@ -271,6 +294,20 @@ public void onDeath(final DamageSource cause)
}
}

@NotNull
@Override
public PathNavigate getNavigator()
{
if (this.newNavigator == null)
{
this.newNavigator = new PathNavigate(this, world);
this.navigator = newNavigator;
this.newNavigator.setCanSwim(true);
this.newNavigator.setEnterDoors(false);
}
return newNavigator;
}

@Override
protected void onDeathUpdate()
{
Expand Down
Loading