initial commit

This commit is contained in:
Sen 2025-03-11 00:23:54 +01:00 committed by Sen
parent 3c9ee26b06
commit 22186c33b9
1458 changed files with 282792 additions and 0 deletions

View file

@ -0,0 +1,78 @@
package game.village;
import game.item.Item;
import game.item.ItemStack;
import game.nbt.NBTTagCompound;
public class MerchantRecipe
{
private ItemStack itemToBuy;
private ItemStack secondItemToBuy;
private ItemStack itemToSell;
public MerchantRecipe(NBTTagCompound tagCompound)
{
this.readFromTags(tagCompound);
}
public MerchantRecipe(ItemStack buy1, ItemStack buy2, ItemStack sell)
{
this.itemToBuy = buy1;
this.secondItemToBuy = buy2;
this.itemToSell = sell;
}
public MerchantRecipe(ItemStack buy1, ItemStack sell)
{
this(buy1, (ItemStack)null, sell);
}
public MerchantRecipe(ItemStack buy1, Item sellItem)
{
this(buy1, new ItemStack(sellItem));
}
public ItemStack getItemToBuy()
{
return this.itemToBuy;
}
public ItemStack getSecondItemToBuy()
{
return this.secondItemToBuy;
}
public boolean hasSecondItemToBuy()
{
return this.secondItemToBuy != null;
}
public ItemStack getItemToSell()
{
return this.itemToSell;
}
public void readFromTags(NBTTagCompound tagCompound)
{
NBTTagCompound nbttagcompound = tagCompound.getCompoundTag("buy");
this.itemToBuy = ItemStack.loadItemStackFromNBT(nbttagcompound);
NBTTagCompound nbttagcompound1 = tagCompound.getCompoundTag("sell");
this.itemToSell = ItemStack.loadItemStackFromNBT(nbttagcompound1);
if (tagCompound.hasKey("buyB", 10))
{
this.secondItemToBuy = ItemStack.loadItemStackFromNBT(tagCompound.getCompoundTag("buyB"));
}
}
public NBTTagCompound writeToTags()
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setTag("buy", this.itemToBuy.writeToNBT(new NBTTagCompound()));
nbttagcompound.setTag("sell", this.itemToSell.writeToNBT(new NBTTagCompound()));
if (this.secondItemToBuy != null)
{
nbttagcompound.setTag("buyB", this.secondItemToBuy.writeToNBT(new NBTTagCompound()));
}
return nbttagcompound;
}
}

View file

@ -0,0 +1,132 @@
package game.village;
import java.util.ArrayList;
import game.item.ItemStack;
import game.nbt.NBTTagCompound;
import game.nbt.NBTTagList;
import game.nbt.NBTUtil;
public class MerchantRecipeList extends ArrayList<MerchantRecipe>
{
public MerchantRecipeList()
{
}
public MerchantRecipeList(NBTTagCompound compound)
{
this.readRecipiesFromTags(compound);
}
/**
* can par1,par2 be used to in crafting recipe par3
*/
public MerchantRecipe canRecipeBeUsed(ItemStack p_77203_1_, ItemStack p_77203_2_, int p_77203_3_)
{
if (p_77203_3_ > 0 && p_77203_3_ < this.size())
{
MerchantRecipe merchantrecipe1 = (MerchantRecipe)this.get(p_77203_3_);
return !this.func_181078_a(p_77203_1_, merchantrecipe1.getItemToBuy()) || (p_77203_2_ != null || merchantrecipe1.hasSecondItemToBuy()) && (!merchantrecipe1.hasSecondItemToBuy() || !this.func_181078_a(p_77203_2_, merchantrecipe1.getSecondItemToBuy())) || p_77203_1_.stackSize < merchantrecipe1.getItemToBuy().stackSize || merchantrecipe1.hasSecondItemToBuy() && p_77203_2_.stackSize < merchantrecipe1.getSecondItemToBuy().stackSize ? null : merchantrecipe1;
}
else
{
for (int i = 0; i < this.size(); ++i)
{
MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
if (this.func_181078_a(p_77203_1_, merchantrecipe.getItemToBuy()) && p_77203_1_.stackSize >= merchantrecipe.getItemToBuy().stackSize && (!merchantrecipe.hasSecondItemToBuy() && p_77203_2_ == null || merchantrecipe.hasSecondItemToBuy() && this.func_181078_a(p_77203_2_, merchantrecipe.getSecondItemToBuy()) && p_77203_2_.stackSize >= merchantrecipe.getSecondItemToBuy().stackSize))
{
return merchantrecipe;
}
}
return null;
}
}
private boolean func_181078_a(ItemStack p_181078_1_, ItemStack p_181078_2_)
{
return ItemStack.areItemsEqual(p_181078_1_, p_181078_2_) && (!p_181078_2_.hasTagCompound() || p_181078_1_.hasTagCompound() && NBTUtil.compareTags(p_181078_2_.getTagCompound(), p_181078_1_.getTagCompound(), false));
}
// public void writeToBuf(PacketBuffer buffer)
// {
// buffer.writeByte((byte)(this.size() & 255));
//
// for (int i = 0; i < this.size(); ++i)
// {
// MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
// buffer.writeItemStackToBuffer(merchantrecipe.getItemToBuy());
// buffer.writeItemStackToBuffer(merchantrecipe.getItemToSell());
// ItemStack itemstack = merchantrecipe.getSecondItemToBuy();
// buffer.writeBoolean(itemstack != null);
//
// if (itemstack != null)
// {
// buffer.writeItemStackToBuffer(itemstack);
// }
//
// buffer.writeBoolean(merchantrecipe.isRecipeDisabled());
// buffer.writeInt(merchantrecipe.getToolUses());
// buffer.writeInt(merchantrecipe.getMaxTradeUses());
// }
// }
//
// public static MerchantRecipeList readFromBuf(PacketBuffer buffer) throws IOException
// {
// MerchantRecipeList merchantrecipelist = new MerchantRecipeList();
// int i = buffer.readByte() & 255;
//
// for (int j = 0; j < i; ++j)
// {
// ItemStack itemstack = buffer.readItemStackFromBuffer();
// ItemStack itemstack1 = buffer.readItemStackFromBuffer();
// ItemStack itemstack2 = null;
//
// if (buffer.readBoolean())
// {
// itemstack2 = buffer.readItemStackFromBuffer();
// }
//
// boolean flag = buffer.readBoolean();
// int k = buffer.readInt();
// int l = buffer.readInt();
// MerchantRecipe merchantrecipe = new MerchantRecipe(itemstack, itemstack2, itemstack1, k, l);
//
// if (flag)
// {
// merchantrecipe.compensateToolUses();
// }
//
// merchantrecipelist.add(merchantrecipe);
// }
//
// return merchantrecipelist;
// }
public void readRecipiesFromTags(NBTTagCompound compound)
{
NBTTagList nbttaglist = compound.getTagList("Recipes", 10);
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
this.add(new MerchantRecipe(nbttagcompound));
}
}
public NBTTagCompound getRecipiesAsTags()
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
NBTTagList nbttaglist = new NBTTagList();
for (int i = 0; i < this.size(); ++i)
{
MerchantRecipe merchantrecipe = (MerchantRecipe)this.get(i);
nbttaglist.appendTag(merchantrecipe.writeToTags());
}
nbttagcompound.setTag("Recipes", nbttaglist);
return nbttagcompound;
}
}

View file

@ -0,0 +1,260 @@
package game.village;
import java.util.Iterator;
import java.util.List;
import game.block.Block;
import game.block.BlockDoor;
import game.collect.Lists;
import game.material.Material;
import game.nbt.NBTTagCompound;
import game.nbt.NBTTagList;
import game.world.BlockPos;
import game.world.WorldServer;
public class Village
{
private final List<VillageDoorInfo> doors = Lists.<VillageDoorInfo>newArrayList();
private BlockPos doorRange = BlockPos.ORIGIN;
private BlockPos center = BlockPos.ORIGIN;
private int radius;
// public Village()
// {
// }
//
// public Village(WorldServer worldIn)
// {
// this.worldObj = worldIn;
// }
//
// public void setWorld(WorldServer worldIn)
// {
// this.worldObj = worldIn;
// }
public void tick(WorldServer world, int counter)
{
// this.tickCounter = counter;
boolean mod = false;
boolean reset = world.rand.zrange(50) == 0;
Iterator<VillageDoorInfo> iterator = this.doors.iterator();
while (iterator.hasNext())
{
VillageDoorInfo villagedoorinfo = (VillageDoorInfo)iterator.next();
if (reset)
{
villagedoorinfo.resetDoorOpeningRestrictionCounter();
}
if (!this.isWoodDoor(world, villagedoorinfo.getDoorBlockPos()) || Math.abs(counter - villagedoorinfo.getInsidePosY()) > 1200)
{
this.doorRange = this.doorRange.subtract(villagedoorinfo.getDoorBlockPos());
mod = true;
villagedoorinfo.setIsDetachedFromVillageFlag(true);
iterator.remove();
}
}
if (mod)
{
this.updatePosition();
}
}
public BlockPos getCenter()
{
return this.center;
}
public int getRadius()
{
return this.radius;
}
// public int getNumDoors()
// {
// return this.doors.size();
// }
// public int getSinceLastDoor()
// {
// return this.tickCounter - this.lastDoor;
// }
// public int getNumVillagers()
// {
// return this.numVillagers;
// }
// public boolean isInside(BlockPos pos)
// {
// return this.center.distanceSq(pos) < (double)(this.radius * this.radius);
// }
public List<VillageDoorInfo> getDoorList()
{
return this.doors;
}
public VillageDoorInfo getNearestDoor(BlockPos pos)
{
VillageDoorInfo villagedoorinfo = null;
int i = Integer.MAX_VALUE;
for (VillageDoorInfo villagedoorinfo1 : this.doors)
{
int j = villagedoorinfo1.getDistanceToDoorBlockSq(pos);
if (j < i)
{
villagedoorinfo = villagedoorinfo1;
i = j;
}
}
return villagedoorinfo;
}
public VillageDoorInfo getDoorInfo(BlockPos pos)
{
VillageDoorInfo villagedoorinfo = null;
int i = Integer.MAX_VALUE;
for (VillageDoorInfo villagedoorinfo1 : this.doors)
{
int j = villagedoorinfo1.getDistanceToDoorBlockSq(pos);
if (j > 256)
{
j = j * 1000;
}
else
{
j = villagedoorinfo1.getDoorOpeningRestrictionCounter();
}
if (j < i)
{
villagedoorinfo = villagedoorinfo1;
i = j;
}
}
return villagedoorinfo;
}
public VillageDoorInfo getExistedDoor(BlockPos doorBlock)
{
if (this.center.distanceSq(doorBlock) > (double)(this.radius * this.radius))
{
return null;
}
else
{
for (VillageDoorInfo villagedoorinfo : this.doors)
{
if (villagedoorinfo.getDoorBlockPos().getX() == doorBlock.getX() && villagedoorinfo.getDoorBlockPos().getZ() == doorBlock.getZ() && Math.abs(villagedoorinfo.getDoorBlockPos().getY() - doorBlock.getY()) <= 1)
{
return villagedoorinfo;
}
}
return null;
}
}
public void addDoor(VillageDoorInfo doorInfo)
{
this.doors.add(doorInfo);
this.doorRange = this.doorRange.add(doorInfo.getDoorBlockPos());
this.updatePosition();
// this.lastDoor = doorInfo.getInsidePosY();
}
public boolean isAnnihilated()
{
return this.doors.isEmpty();
}
private boolean isWoodDoor(WorldServer world, BlockPos pos)
{
Block block = world.getState(pos).getBlock();
return block instanceof BlockDoor ? block.getMaterial() == Material.wood : false;
}
private void updatePosition()
{
int i = this.doors.size();
if (i == 0)
{
this.center = new BlockPos(0, 0, 0);
this.radius = 0;
}
else
{
this.center = new BlockPos(this.doorRange.getX() / i, this.doorRange.getY() / i, this.doorRange.getZ() / i);
int j = 0;
for (VillageDoorInfo villagedoorinfo : this.doors)
{
j = Math.max(villagedoorinfo.getDistanceToDoorBlockSq(this.center), j);
}
this.radius = Math.max(32, (int)Math.sqrt((double)j) + 1);
}
}
public void readVillageDataFromNBT(NBTTagCompound compound)
{
// this.numVillagers = compound.getInteger("PopSize");
this.radius = compound.getInteger("Radius");
// this.lastDoor = compound.getInteger("Stable");
// this.tickCounter = compound.getInteger("Tick");
// this.noBreedTicks = compound.getInteger("MTick");
this.center = new BlockPos(compound.getInteger("CX"), compound.getInteger("CY"), compound.getInteger("CZ"));
this.doorRange = new BlockPos(compound.getInteger("ACX"), compound.getInteger("ACY"), compound.getInteger("ACZ"));
NBTTagList nbttaglist = compound.getTagList("Doors", 10);
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
VillageDoorInfo villagedoorinfo = new VillageDoorInfo(new BlockPos(nbttagcompound.getInteger("X"), nbttagcompound.getInteger("Y"), nbttagcompound.getInteger("Z")), nbttagcompound.getInteger("IDX"), nbttagcompound.getInteger("IDZ"), nbttagcompound.getInteger("TS"));
this.doors.add(villagedoorinfo);
}
}
public void writeVillageDataToNBT(NBTTagCompound compound)
{
// compound.setInteger("PopSize", this.numVillagers);
compound.setInteger("Radius", this.radius);
// compound.setInteger("Stable", this.lastDoor);
// compound.setInteger("Tick", this.tickCounter);
// compound.setInteger("MTick", this.noBreedTicks);
compound.setInteger("CX", this.center.getX());
compound.setInteger("CY", this.center.getY());
compound.setInteger("CZ", this.center.getZ());
compound.setInteger("ACX", this.doorRange.getX());
compound.setInteger("ACY", this.doorRange.getY());
compound.setInteger("ACZ", this.doorRange.getZ());
NBTTagList nbttaglist = new NBTTagList();
for (VillageDoorInfo villagedoorinfo : this.doors)
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setInteger("X", villagedoorinfo.getDoorBlockPos().getX());
nbttagcompound.setInteger("Y", villagedoorinfo.getDoorBlockPos().getY());
nbttagcompound.setInteger("Z", villagedoorinfo.getDoorBlockPos().getZ());
nbttagcompound.setInteger("IDX", villagedoorinfo.getInsideOffsetX());
nbttagcompound.setInteger("IDZ", villagedoorinfo.getInsideOffsetZ());
nbttagcompound.setInteger("TS", villagedoorinfo.getInsidePosY());
nbttaglist.appendTag(nbttagcompound);
}
compound.setTag("Doors", nbttaglist);
}
}

View file

@ -0,0 +1,271 @@
package game.village;
import java.util.Iterator;
import java.util.List;
import game.block.Block;
import game.block.BlockDoor;
import game.collect.Lists;
import game.material.Material;
import game.nbt.NBTTagCompound;
import game.nbt.NBTTagList;
import game.world.BlockPos;
import game.world.Facing;
import game.world.WorldServer;
public class VillageCollection
{
private final List<BlockPos> villagerPositionsList = Lists.<BlockPos>newArrayList();
private final List<VillageDoorInfo> newDoors = Lists.<VillageDoorInfo>newArrayList();
private final List<Village> villageList = Lists.<Village>newArrayList();
private int tickCounter;
private boolean dirty;
public VillageCollection(NBTTagCompound nbt) {
if(nbt != null) {
this.tickCounter = nbt.getInteger("Tick");
NBTTagList nbttaglist = nbt.getTagList("Villages", 10);
for (int i = 0; i < nbttaglist.tagCount(); ++i)
{
NBTTagCompound nbttagcompound = nbttaglist.getCompoundTagAt(i);
Village village = new Village();
village.readVillageDataFromNBT(nbttagcompound);
this.villageList.add(village);
}
}
}
public void addToVillagerPositionList(BlockPos pos)
{
if (this.villagerPositionsList.size() <= 64)
{
if (!this.positionInList(pos))
{
this.villagerPositionsList.add(pos);
}
}
}
public void tick(WorldServer world)
{
++this.tickCounter;
for (Village village : this.villageList)
{
village.tick(world, this.tickCounter);
}
this.removeAnnihilatedVillages();
this.dropOldestVillagerPosition(world);
this.addNewDoorsToVillageOrCreateVillage();
if (this.tickCounter % 400 == 0)
{
this.dirty = true;
}
}
private void removeAnnihilatedVillages()
{
Iterator<Village> iterator = this.villageList.iterator();
while (iterator.hasNext())
{
Village village = (Village)iterator.next();
if (village.isAnnihilated())
{
iterator.remove();
this.dirty = true;
}
}
}
public List<Village> getVillageList()
{
return this.villageList;
}
public Village getNearestVillage(BlockPos doorBlock, int radius)
{
Village village = null;
double d0 = 3.4028234663852886E38D;
for (Village village1 : this.villageList)
{
double d1 = village1.getCenter().distanceSq(doorBlock);
if (d1 < d0)
{
float f = (float)(radius + village1.getRadius());
if (d1 <= (double)(f * f))
{
village = village1;
d0 = d1;
}
}
}
return village;
}
private void dropOldestVillagerPosition(WorldServer world)
{
if (!this.villagerPositionsList.isEmpty())
{
this.addDoorsAround(world, this.villagerPositionsList.remove(0));
}
}
private void addNewDoorsToVillageOrCreateVillage()
{
for (int i = 0; i < this.newDoors.size(); ++i)
{
VillageDoorInfo villagedoorinfo = (VillageDoorInfo)this.newDoors.get(i);
Village village = this.getNearestVillage(villagedoorinfo.getDoorBlockPos(), 32);
if (village == null)
{
village = new Village();
this.villageList.add(village);
this.dirty = true;
}
village.addDoor(villagedoorinfo);
}
this.newDoors.clear();
}
private void addDoorsAround(WorldServer world, BlockPos central)
{
int i = 16;
int j = 4;
int k = 16;
for (int l = -i; l < i; ++l)
{
for (int i1 = -j; i1 < j; ++i1)
{
for (int j1 = -k; j1 < k; ++j1)
{
BlockPos blockpos = central.add(l, i1, j1);
if (this.isWoodDoor(world, blockpos))
{
VillageDoorInfo villagedoorinfo = this.checkDoorExistence(blockpos);
if (villagedoorinfo == null)
{
this.addToNewDoorsList(world, blockpos);
}
else
{
villagedoorinfo.func_179849_a(this.tickCounter);
}
}
}
}
}
}
private VillageDoorInfo checkDoorExistence(BlockPos doorBlock)
{
for (VillageDoorInfo villagedoorinfo : this.newDoors)
{
if (villagedoorinfo.getDoorBlockPos().getX() == doorBlock.getX() && villagedoorinfo.getDoorBlockPos().getZ() == doorBlock.getZ() && Math.abs(villagedoorinfo.getDoorBlockPos().getY() - doorBlock.getY()) <= 1)
{
return villagedoorinfo;
}
}
for (Village village : this.villageList)
{
VillageDoorInfo villagedoorinfo1 = village.getExistedDoor(doorBlock);
if (villagedoorinfo1 != null)
{
return villagedoorinfo1;
}
}
return null;
}
private void addToNewDoorsList(WorldServer world, BlockPos doorBlock)
{
Facing enumfacing = BlockDoor.getFacing(world, doorBlock);
Facing enumfacing1 = enumfacing.getOpposite();
int i = this.countBlocksCanSeeSky(world, doorBlock, enumfacing, 5);
int j = this.countBlocksCanSeeSky(world, doorBlock, enumfacing1, i + 1);
if (i != j)
{
this.newDoors.add(new VillageDoorInfo(doorBlock, i < j ? enumfacing : enumfacing1, this.tickCounter));
}
}
private int countBlocksCanSeeSky(WorldServer world, BlockPos centerPos, Facing direction, int limitation)
{
int i = 0;
for (int j = 1; j <= 5; ++j)
{
if (world.canSeeSky(centerPos.offset(direction, j)))
{
++i;
if (i >= limitation)
{
return i;
}
}
}
return i;
}
private boolean positionInList(BlockPos pos)
{
for (BlockPos blockpos : this.villagerPositionsList)
{
if (blockpos.equals(pos))
{
return true;
}
}
return false;
}
private boolean isWoodDoor(WorldServer world, BlockPos doorPos)
{
Block block = world.getState(doorPos).getBlock();
return block instanceof BlockDoor ? block.getMaterial() == Material.wood : false;
}
public NBTTagCompound writeToNBT()
{
NBTTagCompound nbt = new NBTTagCompound();
nbt.setInteger("Tick", this.tickCounter);
NBTTagList nbttaglist = new NBTTagList();
for (Village village : this.villageList)
{
NBTTagCompound nbttagcompound = new NBTTagCompound();
village.writeVillageDataToNBT(nbttagcompound);
nbttaglist.appendTag(nbttagcompound);
}
nbt.setTag("Villages", nbttaglist);
this.dirty = false;
return nbt;
}
public boolean isDirty()
{
return this.dirty;
}
}

View file

@ -0,0 +1,115 @@
package game.village;
import game.world.BlockPos;
import game.world.Facing;
public class VillageDoorInfo
{
/** a block representing the door. Could be either upper or lower part */
private final BlockPos doorBlockPos;
private final BlockPos insideBlock;
/** the inside direction is where can see less sky */
private final Facing insideDirection;
private int lastActivityTimestamp;
private boolean isDetachedFromVillageFlag;
private int doorOpeningRestrictionCounter;
public VillageDoorInfo(BlockPos pos, int p_i45871_2_, int p_i45871_3_, int p_i45871_4_)
{
this(pos, getFaceDirection(p_i45871_2_, p_i45871_3_), p_i45871_4_);
}
private static Facing getFaceDirection(int deltaX, int deltaZ)
{
return deltaX < 0 ? Facing.WEST : (deltaX > 0 ? Facing.EAST : (deltaZ < 0 ? Facing.NORTH : Facing.SOUTH));
}
public VillageDoorInfo(BlockPos pos, Facing facing, int timestamp)
{
this.doorBlockPos = pos;
this.insideDirection = facing;
this.insideBlock = pos.offset(facing, 2);
this.lastActivityTimestamp = timestamp;
}
/**
* Returns the squared distance between this door and the given coordinate.
*/
public int getDistanceSquared(int x, int y, int z)
{
return (int)this.doorBlockPos.distanceSq((double)x, (double)y, (double)z);
}
public int getDistanceToDoorBlockSq(BlockPos pos)
{
return (int)pos.distanceSq(this.getDoorBlockPos());
}
public int getDistanceToInsideBlockSq(BlockPos pos)
{
return (int)this.insideBlock.distanceSq(pos);
}
public boolean func_179850_c(BlockPos pos)
{
int i = pos.getX() - this.doorBlockPos.getX();
int j = pos.getZ() - this.doorBlockPos.getY();
return i * this.insideDirection.getFrontOffsetX() + j * this.insideDirection.getFrontOffsetZ() >= 0;
}
public void resetDoorOpeningRestrictionCounter()
{
this.doorOpeningRestrictionCounter = 0;
}
public void incrementDoorOpeningRestrictionCounter()
{
++this.doorOpeningRestrictionCounter;
}
public int getDoorOpeningRestrictionCounter()
{
return this.doorOpeningRestrictionCounter;
}
public BlockPos getDoorBlockPos()
{
return this.doorBlockPos;
}
public BlockPos getInsideBlockPos()
{
return this.insideBlock;
}
public int getInsideOffsetX()
{
return this.insideDirection.getFrontOffsetX() * 2;
}
public int getInsideOffsetZ()
{
return this.insideDirection.getFrontOffsetZ() * 2;
}
public int getInsidePosY()
{
return this.lastActivityTimestamp;
}
public void func_179849_a(int timestamp)
{
this.lastActivityTimestamp = timestamp;
}
public boolean getIsDetachedFromVillageFlag()
{
return this.isDetachedFromVillageFlag;
}
public void setIsDetachedFromVillageFlag(boolean detached)
{
this.isDetachedFromVillageFlag = detached;
}
}