Pillager .java

net.minecraft.world.entity.monster.illager.Pillager (class): members: IS_CHARGING_CROSSBOW, isChargingCrossbow, SLOT_OFFSET, enchantSpawnedWeapon, getSlot.

net.minecraft.world.entity.monster.illagerclass260 linescommonextends AbstractIllager9.5 KB

Implements

API Surface (23)

void registerGoals()
AttributeSupplier.Builder createAttributes()
void defineSynchedData(final SynchedEntityData.Builder entityData)
boolean canUseNonMeleeWeapon(final ItemStack item)
boolean isChargingCrossbow()
void setChargingCrossbow(final boolean isCharging)
void onCrossbowAttackPerformed()
TagKey<Item> getPreferredWeaponType()
void addAdditionalSaveData(final ValueOutput output)
AbstractIllager.IllagerArmPose getArmPose()
void readAdditionalSaveData(final ValueInput input)
float getWalkTargetValue(final BlockPos pos, final LevelReader level)
int getMaxSpawnClusterSize()
void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty)
void enchantSpawnedWeapon(final ServerLevelAccessor level, final RandomSource random, final DifficultyInstance difficulty)
SoundEvent getAmbientSound()
SoundEvent getDeathSound()
SoundEvent getHurtSound(final DamageSource source)
void performRangedAttack(final LivingEntity target, final float power)
SimpleContainer getInventory()
void pickUpItem(final ServerLevel level, final ItemEntity entity)
void applyRaidBuffs(final ServerLevel level, final int wave, final boolean isCaptain)
SoundEvent getCelebrateSound()

Fields & Constants (5)

IS_CHARGING_CROSSBOW
INVENTORY_SIZE
SLOT_OFFSET
inventory
spawnReason

Code Graph: Dependencies (53)

Edges (calls / imports / inherits / references) resolved by graphify from the AST. Every chip opens the class source.

Full Source

Pillager.java260 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.monster.illager;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.network.syncher.EntityDataAccessor;
5
import net.minecraft.network.syncher.EntityDataSerializers;
6
import net.minecraft.network.syncher.SynchedEntityData;
7
import net.minecraft.resources.ResourceKey;
8
import net.minecraft.server.level.ServerLevel;
9
import net.minecraft.sounds.SoundEvent;
10
import net.minecraft.sounds.SoundEvents;
11
import net.minecraft.tags.ItemTags;
12
import net.minecraft.tags.TagKey;
13
import net.minecraft.util.RandomSource;
14
import net.minecraft.world.Difficulty;
15
import net.minecraft.world.DifficultyInstance;
16
import net.minecraft.world.SimpleContainer;
17
import net.minecraft.world.damagesource.DamageSource;
18
import net.minecraft.world.entity.EntitySpawnReason;
19
import net.minecraft.world.entity.EntityType;
20
import net.minecraft.world.entity.EquipmentSlot;
21
import net.minecraft.world.entity.LivingEntity;
22
import net.minecraft.world.entity.Mob;
23
import net.minecraft.world.entity.SlotAccess;
24
import net.minecraft.world.entity.SpawnGroupData;
25
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
26
import net.minecraft.world.entity.ai.attributes.Attributes;
27
import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
28
import net.minecraft.world.entity.ai.goal.FloatGoal;
29
import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
30
import net.minecraft.world.entity.ai.goal.RandomStrollGoal;
31
import net.minecraft.world.entity.ai.goal.RangedCrossbowAttackGoal;
32
import net.minecraft.world.entity.ai.goal.target.HurtByTargetGoal;
33
import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
34
import net.minecraft.world.entity.animal.golem.IronGolem;
35
import net.minecraft.world.entity.item.ItemEntity;
36
import net.minecraft.world.entity.monster.CrossbowAttackMob;
37
import net.minecraft.world.entity.monster.Monster;
38
import net.minecraft.world.entity.monster.creaking.Creaking;
39
import net.minecraft.world.entity.npc.InventoryCarrier;
40
import net.minecraft.world.entity.npc.villager.AbstractVillager;
41
import net.minecraft.world.entity.player.Player;
42
import net.minecraft.world.entity.raid.Raid;
43
import net.minecraft.world.entity.raid.Raider;
44
import net.minecraft.world.item.BannerItem;
45
import net.minecraft.world.item.Item;
46
import net.minecraft.world.item.ItemStack;
47
import net.minecraft.world.item.Items;
48
import net.minecraft.world.item.enchantment.EnchantmentHelper;
49
import net.minecraft.world.item.enchantment.providers.EnchantmentProvider;
50
import net.minecraft.world.item.enchantment.providers.VanillaEnchantmentProviders;
51
import net.minecraft.world.level.Level;
52
import net.minecraft.world.level.LevelReader;
53
import net.minecraft.world.level.ServerLevelAccessor;
54
import net.minecraft.world.level.storage.ValueInput;
55
import net.minecraft.world.level.storage.ValueOutput;
56
import org.jspecify.annotations.Nullable;
57
58
public class Pillager extends AbstractIllager implements CrossbowAttackMob, InventoryCarrier {
59
	private static final EntityDataAccessor<Boolean> IS_CHARGING_CROSSBOW = SynchedEntityData.defineId(Pillager.class, EntityDataSerializers.BOOLEAN);
60
	private static final int INVENTORY_SIZE = 5;
61
	private static final int SLOT_OFFSET = 300;
62
	private final SimpleContainer inventory = new SimpleContainer(5);
63
64
	public Pillager(final EntityType<? extends Pillager> type, final Level level) {
65
		super(type, level);
66
	}
67
68
	@Override
69
	protected void registerGoals() {
70
		super.registerGoals();
71
		this.goalSelector.addGoal(0, new FloatGoal(this));
72
		this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Creaking.class, 8.0F, 1.0, 1.2));
73
		this.goalSelector.addGoal(2, new Raider.HoldGroundAttackGoal(this, 10.0F));
74
		this.goalSelector.addGoal(3, new RangedCrossbowAttackGoal<>(this, 1.0, 8.0F));
75
		this.goalSelector.addGoal(8, new RandomStrollGoal(this, 0.6));
76
		this.goalSelector.addGoal(9, new LookAtPlayerGoal(this, Player.class, 15.0F, 1.0F));
77
		this.goalSelector.addGoal(10, new LookAtPlayerGoal(this, Mob.class, 15.0F));
78
		this.targetSelector.addGoal(1, new HurtByTargetGoal(this, Raider.class).setAlertOthers());
79
		this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
80
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, AbstractVillager.class, false));
81
		this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, IronGolem.class, true));
82
	}
83
84
	public static AttributeSupplier.Builder createAttributes() {
85
		return Monster.createMonsterAttributes()
86
			.add(Attributes.MOVEMENT_SPEED, 0.35F)
87
			.add(Attributes.MAX_HEALTH, 24.0)
88
			.add(Attributes.ATTACK_DAMAGE, 5.0)
89
			.add(Attributes.FOLLOW_RANGE, 32.0);
90
	}
91
92
	@Override
93
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
94
		super.defineSynchedData(entityData);
95
		entityData.define(IS_CHARGING_CROSSBOW, false);
96
	}
97
98
	@Override
99
	public boolean canUseNonMeleeWeapon(final ItemStack item) {
100
		return item.getItem() == Items.CROSSBOW;
101
	}
102
103
	public boolean isChargingCrossbow() {
104
		return this.entityData.get(IS_CHARGING_CROSSBOW);
105
	}
106
107
	@Override
108
	public void setChargingCrossbow(final boolean isCharging) {
109
		this.entityData.set(IS_CHARGING_CROSSBOW, isCharging);
110
	}
111
112
	@Override
113
	public void onCrossbowAttackPerformed() {
114
		this.noActionTime = 0;
115
	}
116
117
	@Override
118
	public TagKey<Item> getPreferredWeaponType() {
119
		return ItemTags.PILLAGER_PREFERRED_WEAPONS;
120
	}
121
122
	@Override
123
	protected void addAdditionalSaveData(final ValueOutput output) {
124
		super.addAdditionalSaveData(output);
125
		this.writeInventoryToTag(output);
126
	}
127
128
	@Override
129
	public AbstractIllager.IllagerArmPose getArmPose() {
130
		if (this.isChargingCrossbow()) {
131
			return AbstractIllager.IllagerArmPose.CROSSBOW_CHARGE;
132
		} else if (this.isHolding(Items.CROSSBOW)) {
133
			return AbstractIllager.IllagerArmPose.CROSSBOW_HOLD;
134
		} else {
135
			return this.isAggressive() ? AbstractIllager.IllagerArmPose.ATTACKING : AbstractIllager.IllagerArmPose.NEUTRAL;
136
		}
137
	}
138
139
	@Override
140
	protected void readAdditionalSaveData(final ValueInput input) {
141
		super.readAdditionalSaveData(input);
142
		this.readInventoryFromTag(input);
143
		this.setCanPickUpLoot(true);
144
	}
145
146
	@Override
147
	public float getWalkTargetValue(final BlockPos pos, final LevelReader level) {
148
		return 0.0F;
149
	}
150
151
	@Override
152
	public int getMaxSpawnClusterSize() {
153
		return 1;
154
	}
155
156
	@Override
157
	public @Nullable SpawnGroupData finalizeSpawn(
158
		final ServerLevelAccessor level, final DifficultyInstance difficulty, final EntitySpawnReason spawnReason, final @Nullable SpawnGroupData groupData
159
	) {
160
		RandomSource random = level.getRandom();
161
		this.populateDefaultEquipmentSlots(random, difficulty);
162
		this.populateDefaultEquipmentEnchantments(level, random, difficulty);
163
		return super.finalizeSpawn(level, difficulty, spawnReason, groupData);
164
	}
165
166
	@Override
167
	protected void populateDefaultEquipmentSlots(final RandomSource random, final DifficultyInstance difficulty) {
168
		this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.CROSSBOW));
169
	}
170
171
	@Override
172
	protected void enchantSpawnedWeapon(final ServerLevelAccessor level, final RandomSource random, final DifficultyInstance difficulty) {
173
		super.enchantSpawnedWeapon(level, random, difficulty);
174
		if (random.nextInt(300) == 0) {
175
			ItemStack weapon = this.getMainHandItem();
176
			if (weapon.is(Items.CROSSBOW)) {
177
				EnchantmentHelper.enchantItemFromProvider(weapon, level.registryAccess(), VanillaEnchantmentProviders.PILLAGER_SPAWN_CROSSBOW, difficulty, random);
178
			}
179
		}
180
	}
181
182
	@Override
183
	protected SoundEvent getAmbientSound() {
184
		return SoundEvents.PILLAGER_AMBIENT;
185
	}
186
187
	@Override
188
	protected SoundEvent getDeathSound() {
189
		return SoundEvents.PILLAGER_DEATH;
190
	}
191
192
	@Override
193
	protected SoundEvent getHurtSound(final DamageSource source) {
194
		return SoundEvents.PILLAGER_HURT;
195
	}
196
197
	@Override
198
	public void performRangedAttack(final LivingEntity target, final float power) {
199
		this.performCrossbowAttack(this, 1.6F);
200
	}
201
202
	@Override
203
	public SimpleContainer getInventory() {
204
		return this.inventory;
205
	}
206
207
	@Override
208
	protected void pickUpItem(final ServerLevel level, final ItemEntity entity) {
209
		ItemStack itemStack = entity.getItem();
210
		if (itemStack.getItem() instanceof BannerItem) {
211
			super.pickUpItem(level, entity);
212
		} else if (this.wantsItem(itemStack)) {
213
			this.onItemPickup(entity);
214
			ItemStack remainder = this.inventory.addItem(itemStack);
215
			if (remainder.isEmpty()) {
216
				entity.discard();
217
			} else {
218
				itemStack.setCount(remainder.getCount());
219
			}
220
		}
221
	}
222
223
	private boolean wantsItem(final ItemStack itemStack) {
224
		return this.hasActiveRaid() && itemStack.is(Items.BANNER.white());
225
	}
226
227
	@Override
228
	public @Nullable SlotAccess getSlot(final int slot) {
229
		int inventorySlot = slot - 300;
230
		return inventorySlot >= 0 && inventorySlot < this.inventory.getContainerSize() ? this.inventory.getSlot(inventorySlot) : super.getSlot(slot);
231
	}
232
233
	@Override
234
	public void applyRaidBuffs(final ServerLevel level, final int wave, final boolean isCaptain) {
235
		Raid raid = this.getCurrentRaid();
236
		boolean shouldEnchant = this.random.nextFloat() <= raid.getEnchantOdds();
237
		if (shouldEnchant) {
238
			ItemStack crossbow = new ItemStack(Items.CROSSBOW);
239
			ResourceKey<EnchantmentProvider> provider;
240
			if (wave > raid.getNumGroups(Difficulty.NORMAL)) {
241
				provider = VanillaEnchantmentProviders.RAID_PILLAGER_POST_WAVE_5;
242
			} else if (wave > raid.getNumGroups(Difficulty.EASY)) {
243
				provider = VanillaEnchantmentProviders.RAID_PILLAGER_POST_WAVE_3;
244
			} else {
245
				provider = null;
246
			}
247
248
			if (provider != null) {
249
				EnchantmentHelper.enchantItemFromProvider(crossbow, level.registryAccess(), provider, level.getCurrentDifficultyAt(this.blockPosition()), this.getRandom());
250
				this.setItemSlot(EquipmentSlot.MAINHAND, crossbow);
251
			}
252
		}
253
	}
254
255
	@Override
256
	public SoundEvent getCelebrateSound() {
257
		return SoundEvents.PILLAGER_CELEBRATE;
258
	}
259
}
260

Decompiled Minecraft 26.3 source of Pillager (net/minecraft/world/entity/monster/illager/Pillager.java) with Mojang official mappings, stored verbatim on the site.