Village .java

net.minecraft.village.Village (class, 583 lines): members: findNearestVillageAggressor, getTicksSinceLastDoorAdding, setDefaultPlayerReputation, center.

net.minecraft.villageclass583 linescommon19.4 KB

API Surface (25)

void setWorld(World worldIn)
void tick(int p_75560_1_)
BlockPos getCenter()
int getVillageRadius()
int getNumVillageDoors()
int getTicksSinceLastDoorAdding()
int getNumVillagers()
boolean func_179866_a(BlockPos pos)
List<VillageDoorInfo> getVillageDoorInfoList()
VillageDoorInfo getNearestDoor(BlockPos pos)
VillageDoorInfo getDoorInfo(BlockPos pos)
VillageDoorInfo getExistedDoor(BlockPos doorBlock)
void addVillageDoorInfo(VillageDoorInfo doorInfo)
boolean isAnnihilated()
void addOrRenewAgressor(EntityLivingBase entitylivingbaseIn)
EntityLivingBase findNearestVillageAggressor(EntityLivingBase entitylivingbaseIn)
EntityPlayer getNearestTargetPlayer(EntityLivingBase villageDefender)
int getReputationForPlayer(String p_82684_1_)
int setReputationForPlayer(String p_82688_1_, int p_82688_2_)
boolean isPlayerReputationTooLow(String p_82687_1_)
void readVillageDataFromNBT(NBTTagCompound compound)
void writeVillageDataToNBT(NBTTagCompound compound)
void endMatingSeason()
boolean isMatingSeason()
void setDefaultPlayerReputation(int p_82683_1_)

Fields & Constants (12)

worldObj
villageDoorInfoList
centerHelper
center
villageRadius
lastAddDoorTimestamp
tickCounter
numVillagers
noBreedTicks
playerReputation
villageAgressors
numIronGolems

String literals (9)

"PopSize"
"Radius"
"Golems"
"Stable"
"Tick"
"MTick"
"Doors"
"Players"
"UUID"

Full Source

Village.java583 lines · MCP 1.8.9 · verbatimOriginal ↗
1
package net.minecraft.village;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.authlib.GameProfile;
5
import java.util.Iterator;
6
import java.util.List;
7
import java.util.TreeMap;
8
import java.util.UUID;
9
import net.minecraft.block.Block;
10
import net.minecraft.block.BlockDoor;
11
import net.minecraft.block.material.Material;
12
import net.minecraft.entity.EntityLivingBase;
13
import net.minecraft.entity.monster.EntityIronGolem;
14
import net.minecraft.entity.passive.EntityVillager;
15
import net.minecraft.entity.player.EntityPlayer;
16
import net.minecraft.nbt.NBTTagCompound;
17
import net.minecraft.nbt.NBTTagList;
18
import net.minecraft.server.MinecraftServer;
19
import net.minecraft.server.management.PlayerProfileCache;
20
import net.minecraft.util.AxisAlignedBB;
21
import net.minecraft.util.BlockPos;
22
import net.minecraft.util.MathHelper;
23
import net.minecraft.util.Vec3;
24
import net.minecraft.world.World;
25
26
public class Village
27
{
28
    private World worldObj;
29
    private final List<VillageDoorInfo> villageDoorInfoList = Lists.<VillageDoorInfo>newArrayList();
30
31
    /**
32
     * This is the sum of all door coordinates and used to calculate the actual village center by dividing by the number
33
     * of doors.
34
     */
35
    private BlockPos centerHelper = BlockPos.ORIGIN;
36
37
    /** This is the actual village center. */
38
    private BlockPos center = BlockPos.ORIGIN;
39
    private int villageRadius;
40
    private int lastAddDoorTimestamp;
41
    private int tickCounter;
42
    private int numVillagers;
43
44
    /** Timestamp of tick count when villager last bred */
45
    private int noBreedTicks;
46
    private TreeMap<String, Integer> playerReputation = new TreeMap();
47
    private List<Village.VillageAggressor> villageAgressors = Lists.<Village.VillageAggressor>newArrayList();
48
    private int numIronGolems;
49
50
    public Village()
51
    {
52
    }
53
54
    public Village(World worldIn)
55
    {
56
        this.worldObj = worldIn;
57
    }
58
59
    public void setWorld(World worldIn)
60
    {
61
        this.worldObj = worldIn;
62
    }
63
64
    /**
65
     * Called periodically by VillageCollection
66
     */
67
    public void tick(int p_75560_1_)
68
    {
69
        this.tickCounter = p_75560_1_;
70
        this.removeDeadAndOutOfRangeDoors();
71
        this.removeDeadAndOldAgressors();
72
73
        if (p_75560_1_ % 20 == 0)
74
        {
75
            this.updateNumVillagers();
76
        }
77
78
        if (p_75560_1_ % 30 == 0)
79
        {
80
            this.updateNumIronGolems();
81
        }
82
83
        int i = this.numVillagers / 10;
84
85
        if (this.numIronGolems < i && this.villageDoorInfoList.size() > 20 && this.worldObj.rand.nextInt(7000) == 0)
86
        {
87
            Vec3 vec3 = this.func_179862_a(this.center, 2, 4, 2);
88
89
            if (vec3 != null)
90
            {
91
                EntityIronGolem entityirongolem = new EntityIronGolem(this.worldObj);
92
                entityirongolem.setPosition(vec3.xCoord, vec3.yCoord, vec3.zCoord);
93
                this.worldObj.spawnEntityInWorld(entityirongolem);
94
                ++this.numIronGolems;
95
            }
96
        }
97
    }
98
99
    private Vec3 func_179862_a(BlockPos p_179862_1_, int p_179862_2_, int p_179862_3_, int p_179862_4_)
100
    {
101
        for (int i = 0; i < 10; ++i)
102
        {
103
            BlockPos blockpos = p_179862_1_.add(this.worldObj.rand.nextInt(16) - 8, this.worldObj.rand.nextInt(6) - 3, this.worldObj.rand.nextInt(16) - 8);
104
105
            if (this.func_179866_a(blockpos) && this.func_179861_a(new BlockPos(p_179862_2_, p_179862_3_, p_179862_4_), blockpos))
106
            {
107
                return new Vec3((double)blockpos.getX(), (double)blockpos.getY(), (double)blockpos.getZ());
108
            }
109
        }
110
111
        return null;
112
    }
113
114
    private boolean func_179861_a(BlockPos p_179861_1_, BlockPos p_179861_2_)
115
    {
116
        if (!World.doesBlockHaveSolidTopSurface(this.worldObj, p_179861_2_.down()))
117
        {
118
            return false;
119
        }
120
        else
121
        {
122
            int i = p_179861_2_.getX() - p_179861_1_.getX() / 2;
123
            int j = p_179861_2_.getZ() - p_179861_1_.getZ() / 2;
124
125
            for (int k = i; k < i + p_179861_1_.getX(); ++k)
126
            {
127
                for (int l = p_179861_2_.getY(); l < p_179861_2_.getY() + p_179861_1_.getY(); ++l)
128
                {
129
                    for (int i1 = j; i1 < j + p_179861_1_.getZ(); ++i1)
130
                    {
131
                        if (this.worldObj.getBlockState(new BlockPos(k, l, i1)).getBlock().isNormalCube())
132
                        {
133
                            return false;
134
                        }
135
                    }
136
                }
137
            }
138
139
            return true;
140
        }
141
    }
142
143
    private void updateNumIronGolems()
144
    {
145
        List<EntityIronGolem> list = this.worldObj.<EntityIronGolem>getEntitiesWithinAABB(EntityIronGolem.class, new AxisAlignedBB((double)(this.center.getX() - this.villageRadius), (double)(this.center.getY() - 4), (double)(this.center.getZ() - this.villageRadius), (double)(this.center.getX() + this.villageRadius), (double)(this.center.getY() + 4), (double)(this.center.getZ() + this.villageRadius)));
146
        this.numIronGolems = list.size();
147
    }
148
149
    private void updateNumVillagers()
150
    {
151
        List<EntityVillager> list = this.worldObj.<EntityVillager>getEntitiesWithinAABB(EntityVillager.class, new AxisAlignedBB((double)(this.center.getX() - this.villageRadius), (double)(this.center.getY() - 4), (double)(this.center.getZ() - this.villageRadius), (double)(this.center.getX() + this.villageRadius), (double)(this.center.getY() + 4), (double)(this.center.getZ() + this.villageRadius)));
152
        this.numVillagers = list.size();
153
154
        if (this.numVillagers == 0)
155
        {
156
            this.playerReputation.clear();
157
        }
158
    }
159
160
    public BlockPos getCenter()
161
    {
162
        return this.center;
163
    }
164
165
    public int getVillageRadius()
166
    {
167
        return this.villageRadius;
168
    }
169
170
    /**
171
     * Actually get num village door info entries, but that boils down to number of doors. Called by
172
     * EntityAIVillagerMate and VillageSiege
173
     */
174
    public int getNumVillageDoors()
175
    {
176
        return this.villageDoorInfoList.size();
177
    }
178
179
    public int getTicksSinceLastDoorAdding()
180
    {
181
        return this.tickCounter - this.lastAddDoorTimestamp;
182
    }
183
184
    public int getNumVillagers()
185
    {
186
        return this.numVillagers;
187
    }
188
189
    public boolean func_179866_a(BlockPos pos)
190
    {
191
        return this.center.distanceSq(pos) < (double)(this.villageRadius * this.villageRadius);
192
    }
193
194
    public List<VillageDoorInfo> getVillageDoorInfoList()
195
    {
196
        return this.villageDoorInfoList;
197
    }
198
199
    public VillageDoorInfo getNearestDoor(BlockPos pos)
200
    {
201
        VillageDoorInfo villagedoorinfo = null;
202
        int i = Integer.MAX_VALUE;
203
204
        for (VillageDoorInfo villagedoorinfo1 : this.villageDoorInfoList)
205
        {
206
            int j = villagedoorinfo1.getDistanceToDoorBlockSq(pos);
207
208
            if (j < i)
209
            {
210
                villagedoorinfo = villagedoorinfo1;
211
                i = j;
212
            }
213
        }
214
215
        return villagedoorinfo;
216
    }
217
218
    /**
219
     * Returns {@link net.minecraft.village.VillageDoorInfo VillageDoorInfo} from given block position
220
     */
221
    public VillageDoorInfo getDoorInfo(BlockPos pos)
222
    {
223
        VillageDoorInfo villagedoorinfo = null;
224
        int i = Integer.MAX_VALUE;
225
226
        for (VillageDoorInfo villagedoorinfo1 : this.villageDoorInfoList)
227
        {
228
            int j = villagedoorinfo1.getDistanceToDoorBlockSq(pos);
229
230
            if (j > 256)
231
            {
232
                j = j * 1000;
233
            }
234
            else
235
            {
236
                j = villagedoorinfo1.getDoorOpeningRestrictionCounter();
237
            }
238
239
            if (j < i)
240
            {
241
                villagedoorinfo = villagedoorinfo1;
242
                i = j;
243
            }
244
        }
245
246
        return villagedoorinfo;
247
    }
248
249
    /**
250
     * if door not existed in this village, null will be returned
251
     */
252
    public VillageDoorInfo getExistedDoor(BlockPos doorBlock)
253
    {
254
        if (this.center.distanceSq(doorBlock) > (double)(this.villageRadius * this.villageRadius))
255
        {
256
            return null;
257
        }
258
        else
259
        {
260
            for (VillageDoorInfo villagedoorinfo : this.villageDoorInfoList)
261
            {
262
                if (villagedoorinfo.getDoorBlockPos().getX() == doorBlock.getX() && villagedoorinfo.getDoorBlockPos().getZ() == doorBlock.getZ() && Math.abs(villagedoorinfo.getDoorBlockPos().getY() - doorBlock.getY()) <= 1)
263
                {
264
                    return villagedoorinfo;
265
                }
266
            }
267
268
            return null;
269
        }
270
    }
271
272
    public void addVillageDoorInfo(VillageDoorInfo doorInfo)
273
    {
274
        this.villageDoorInfoList.add(doorInfo);
275
        this.centerHelper = this.centerHelper.add(doorInfo.getDoorBlockPos());
276
        this.updateVillageRadiusAndCenter();
277
        this.lastAddDoorTimestamp = doorInfo.getInsidePosY();
278
    }
279
280
    /**
281
     * Returns true, if there is not a single village door left. Called by VillageCollection
282
     */
283
    public boolean isAnnihilated()
284
    {
285
        return this.villageDoorInfoList.isEmpty();
286
    }
287
288
    public void addOrRenewAgressor(EntityLivingBase entitylivingbaseIn)
289
    {
290
        for (Village.VillageAggressor village$villageaggressor : this.villageAgressors)
291
        {
292
            if (village$villageaggressor.agressor == entitylivingbaseIn)
293
            {
294
                village$villageaggressor.agressionTime = this.tickCounter;
295
                return;
296
            }
297
        }
298
299
        this.villageAgressors.add(new Village.VillageAggressor(entitylivingbaseIn, this.tickCounter));
300
    }
301
302
    public EntityLivingBase findNearestVillageAggressor(EntityLivingBase entitylivingbaseIn)
303
    {
304
        double d0 = Double.MAX_VALUE;
305
        Village.VillageAggressor village$villageaggressor = null;
306
307
        for (int i = 0; i < this.villageAgressors.size(); ++i)
308
        {
309
            Village.VillageAggressor village$villageaggressor1 = (Village.VillageAggressor)this.villageAgressors.get(i);
310
            double d1 = village$villageaggressor1.agressor.getDistanceSqToEntity(entitylivingbaseIn);
311
312
            if (d1 <= d0)
313
            {
314
                village$villageaggressor = village$villageaggressor1;
315
                d0 = d1;
316
            }
317
        }
318
319
        return village$villageaggressor != null ? village$villageaggressor.agressor : null;
320
    }
321
322
    public EntityPlayer getNearestTargetPlayer(EntityLivingBase villageDefender)
323
    {
324
        double d0 = Double.MAX_VALUE;
325
        EntityPlayer entityplayer = null;
326
327
        for (String s : this.playerReputation.keySet())
328
        {
329
            if (this.isPlayerReputationTooLow(s))
330
            {
331
                EntityPlayer entityplayer1 = this.worldObj.getPlayerEntityByName(s);
332
333
                if (entityplayer1 != null)
334
                {
335
                    double d1 = entityplayer1.getDistanceSqToEntity(villageDefender);
336
337
                    if (d1 <= d0)
338
                    {
339
                        entityplayer = entityplayer1;
340
                        d0 = d1;
341
                    }
342
                }
343
            }
344
        }
345
346
        return entityplayer;
347
    }
348
349
    private void removeDeadAndOldAgressors()
350
    {
351
        Iterator<Village.VillageAggressor> iterator = this.villageAgressors.iterator();
352
353
        while (iterator.hasNext())
354
        {
355
            Village.VillageAggressor village$villageaggressor = (Village.VillageAggressor)iterator.next();
356
357
            if (!village$villageaggressor.agressor.isEntityAlive() || Math.abs(this.tickCounter - village$villageaggressor.agressionTime) > 300)
358
            {
359
                iterator.remove();
360
            }
361
        }
362
    }
363
364
    private void removeDeadAndOutOfRangeDoors()
365
    {
366
        boolean flag = false;
367
        boolean flag1 = this.worldObj.rand.nextInt(50) == 0;
368
        Iterator<VillageDoorInfo> iterator = this.villageDoorInfoList.iterator();
369
370
        while (iterator.hasNext())
371
        {
372
            VillageDoorInfo villagedoorinfo = (VillageDoorInfo)iterator.next();
373
374
            if (flag1)
375
            {
376
                villagedoorinfo.resetDoorOpeningRestrictionCounter();
377
            }
378
379
            if (!this.isWoodDoor(villagedoorinfo.getDoorBlockPos()) || Math.abs(this.tickCounter - villagedoorinfo.getInsidePosY()) > 1200)
380
            {
381
                this.centerHelper = this.centerHelper.subtract(villagedoorinfo.getDoorBlockPos());
382
                flag = true;
383
                villagedoorinfo.setIsDetachedFromVillageFlag(true);
384
                iterator.remove();
385
            }
386
        }
387
388
        if (flag)
389
        {
390
            this.updateVillageRadiusAndCenter();
391
        }
392
    }
393
394
    private boolean isWoodDoor(BlockPos pos)
395
    {
396
        Block block = this.worldObj.getBlockState(pos).getBlock();
397
        return block instanceof BlockDoor ? block.getMaterial() == Material.wood : false;
398
    }
399
400
    private void updateVillageRadiusAndCenter()
401
    {
402
        int i = this.villageDoorInfoList.size();
403
404
        if (i == 0)
405
        {
406
            this.center = new BlockPos(0, 0, 0);
407
            this.villageRadius = 0;
408
        }
409
        else
410
        {
411
            this.center = new BlockPos(this.centerHelper.getX() / i, this.centerHelper.getY() / i, this.centerHelper.getZ() / i);
412
            int j = 0;
413
414
            for (VillageDoorInfo villagedoorinfo : this.villageDoorInfoList)
415
            {
416
                j = Math.max(villagedoorinfo.getDistanceToDoorBlockSq(this.center), j);
417
            }
418
419
            this.villageRadius = Math.max(32, (int)Math.sqrt((double)j) + 1);
420
        }
421
    }
422
423
    /**
424
     * Return the village reputation for a player
425
     */
426
    public int getReputationForPlayer(String p_82684_1_)
427
    {
428
        Integer integer = (Integer)this.playerReputation.get(p_82684_1_);
429
        return integer != null ? integer.intValue() : 0;
430
    }
431
432
    /**
433
     * Set the village reputation for a player.
434
     */
435
    public int setReputationForPlayer(String p_82688_1_, int p_82688_2_)
436
    {
437
        int i = this.getReputationForPlayer(p_82688_1_);
438
        int j = MathHelper.clamp_int(i + p_82688_2_, -30, 10);
439
        this.playerReputation.put(p_82688_1_, Integer.valueOf(j));
440
        return j;
441
    }
442
443
    /**
444
     * Return whether this player has a too low reputation with this village.
445
     */
446
    public boolean isPlayerReputationTooLow(String p_82687_1_)
447
    {
448
        return this.getReputationForPlayer(p_82687_1_) <= -15;
449
    }
450
451
    /**
452
     * Read this village's data from NBT.
453
     */
454
    public void readVillageDataFromNBT(NBTTagCompound compound)
455
    {
456
        this.numVillagers = compound.getInteger("PopSize");
457
        this.villageRadius = compound.getInteger("Radius");
458
        this.numIronGolems = compound.getInteger("Golems");
459
        this.lastAddDoorTimestamp = compound.getInteger("Stable");
460
        this.tickCounter = compound.getInteger("Tick");
461
        this.noBreedTicks = compound.getInteger("MTick");
462
        this.center = new BlockPos(compound.getInteger("CX"), compound.getInteger("CY"), compound.getInteger("CZ"));
463
        this.centerHelper = new BlockPos(compound.getInteger("ACX"), compound.getInteger("ACY"), compound.getInteger("ACZ"));
464
        NBTTagList nbttaglist = compound.getTagList("Doors", 10);
465
466
        for (int i = 0; i < nbttaglist.tagCount(); ++i)
467
        {
468
            NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
469
            VillageDoorInfo villagedoorinfo = new VillageDoorInfo(new BlockPos(nbttagcompound.getInteger("X"), nbttagcompound.getInteger("Y"), nbttagcompound.getInteger("Z")), nbttagcompound.getInteger("IDX"), nbttagcompound.getInteger("IDZ"), nbttagcompound.getInteger("TS"));
470
            this.villageDoorInfoList.add(villagedoorinfo);
471
        }
472
473
        NBTTagList nbttaglist1 = compound.getTagList("Players", 10);
474
475
        for (int j = 0; j < nbttaglist1.tagCount(); ++j)
476
        {
477
            NBTTagCompound nbttagcompound1 = nbttaglist1.getCompoundTagAt(j);
478
479
            if (nbttagcompound1.hasKey("UUID"))
480
            {
481
                PlayerProfileCache playerprofilecache = MinecraftServer.getServer().getPlayerProfileCache();
482
                GameProfile gameprofile = playerprofilecache.getProfileByUUID(UUID.fromString(nbttagcompound1.getString("UUID")));
483
484
                if (gameprofile != null)
485
                {
486
                    this.playerReputation.put(gameprofile.getName(), Integer.valueOf(nbttagcompound1.getInteger("S")));
487
                }
488
            }
489
            else
490
            {
491
                this.playerReputation.put(nbttagcompound1.getString("Name"), Integer.valueOf(nbttagcompound1.getInteger("S")));
492
            }
493
        }
494
    }
495
496
    /**
497
     * Write this village's data to NBT.
498
     */
499
    public void writeVillageDataToNBT(NBTTagCompound compound)
500
    {
501
        compound.setInteger("PopSize", this.numVillagers);
502
        compound.setInteger("Radius", this.villageRadius);
503
        compound.setInteger("Golems", this.numIronGolems);
504
        compound.setInteger("Stable", this.lastAddDoorTimestamp);
505
        compound.setInteger("Tick", this.tickCounter);
506
        compound.setInteger("MTick", this.noBreedTicks);
507
        compound.setInteger("CX", this.center.getX());
508
        compound.setInteger("CY", this.center.getY());
509
        compound.setInteger("CZ", this.center.getZ());
510
        compound.setInteger("ACX", this.centerHelper.getX());
511
        compound.setInteger("ACY", this.centerHelper.getY());
512
        compound.setInteger("ACZ", this.centerHelper.getZ());
513
        NBTTagList nbttaglist = new NBTTagList();
514
515
        for (VillageDoorInfo villagedoorinfo : this.villageDoorInfoList)
516
        {
517
            NBTTagCompound nbttagcompound = new NBTTagCompound();
518
            nbttagcompound.setInteger("X", villagedoorinfo.getDoorBlockPos().getX());
519
            nbttagcompound.setInteger("Y", villagedoorinfo.getDoorBlockPos().getY());
520
            nbttagcompound.setInteger("Z", villagedoorinfo.getDoorBlockPos().getZ());
521
            nbttagcompound.setInteger("IDX", villagedoorinfo.getInsideOffsetX());
522
            nbttagcompound.setInteger("IDZ", villagedoorinfo.getInsideOffsetZ());
523
            nbttagcompound.setInteger("TS", villagedoorinfo.getInsidePosY());
524
            nbttaglist.appendTag(nbttagcompound);
525
        }
526
527
        compound.setTag("Doors", nbttaglist);
528
        NBTTagList nbttaglist1 = new NBTTagList();
529
530
        for (String s : this.playerReputation.keySet())
531
        {
532
            NBTTagCompound nbttagcompound1 = new NBTTagCompound();
533
            PlayerProfileCache playerprofilecache = MinecraftServer.getServer().getPlayerProfileCache();
534
            GameProfile gameprofile = playerprofilecache.getGameProfileForUsername(s);
535
536
            if (gameprofile != null)
537
            {
538
                nbttagcompound1.setString("UUID", gameprofile.getId().toString());
539
                nbttagcompound1.setInteger("S", ((Integer)this.playerReputation.get(s)).intValue());
540
                nbttaglist1.appendTag(nbttagcompound1);
541
            }
542
        }
543
544
        compound.setTag("Players", nbttaglist1);
545
    }
546
547
    /**
548
     * Prevent villager breeding for a fixed interval of time
549
     */
550
    public void endMatingSeason()
551
    {
552
        this.noBreedTicks = this.tickCounter;
553
    }
554
555
    /**
556
     * Return whether villagers mating refractory period has passed
557
     */
558
    public boolean isMatingSeason()
559
    {
560
        return this.noBreedTicks == 0 || this.tickCounter - this.noBreedTicks >= 3600;
561
    }
562
563
    public void setDefaultPlayerReputation(int p_82683_1_)
564
    {
565
        for (String s : this.playerReputation.keySet())
566
        {
567
            this.setReputationForPlayer(s, p_82683_1_);
568
        }
569
    }
570
571
    class VillageAggressor
572
    {
573
        public EntityLivingBase agressor;
574
        public int agressionTime;
575
576
        VillageAggressor(EntityLivingBase p_i1674_2_, int p_i1674_3_)
577
        {
578
            this.agressor = p_i1674_2_;
579
            this.agressionTime = p_i1674_3_;
580
        }
581
    }
582
}
583

Deobfuscated MCP 1.8.9 source of Village from Marcelektro/MavenMCP-1.8.9 (net/minecraft/village/Village.java), stored verbatim.