下面列出了怎么用net.minecraft.world.chunk.Chunk的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void updateBetweenY(int minY, int maxY)
{
World world = SchematicWorldHandler.getSchematicWorld();
if (world != null)
{
RenderGlobal rg = LitematicaRenderer.getInstance().getWorldRenderer();
Long2ObjectMap<Chunk> schematicChunks = ((IMixinChunkProviderClient) (Object) world.getChunkProvider()).getLoadedChunks();
Long2ObjectMap<Chunk> clientChunks = ((IMixinChunkProviderClient) (Object) Minecraft.getMinecraft().world.getChunkProvider()).getLoadedChunks();
for (Chunk chunk : schematicChunks.values())
{
// Only mark chunks that are actually rendered (if the schematic world contains more chunks)
if (chunk.isEmpty() == false && clientChunks.containsKey(ChunkPos.asLong(chunk.x, chunk.z)))
{
rg.markBlockRangeForRenderUpdate((chunk.x << 4) - 1, minY, (chunk.z << 4) - 1, (chunk.x << 4) + 16, maxY, (chunk.z << 4) + 16);
}
}
}
}
private static int getSpawnMode(Chunk chunk, int x, int y, int z) {
World world = chunk.getWorld();
BlockPos pos = new BlockPos(x, y, z);
if (!SpawnerAnimals.canCreatureTypeSpawnAtLocation(SpawnPlacementType.ON_GROUND, world, pos) ||
chunk.getLightFor(EnumSkyBlock.BLOCK, pos) >= 8)
return 0;
c.set(x+0.2, y+0.01, z+0.2, x+0.8, y+1.8, z+0.8);
AxisAlignedBB aabb = c.aabb();
if (!world.checkNoEntityCollision(aabb) ||
!world.getCollidingBoundingBoxes(dummyEntity, aabb).isEmpty() ||
world.isAnyLiquid(aabb))
return 0;
if (chunk.getLightFor(EnumSkyBlock.SKY, pos) >= 8)
return 1;
return 2;
}
@SubscribeEvent
public void onChunkLoad(ChunkEvent.Load event) {
IChunk chunk = event.getChunk();
Map<BlockPos, TileEntity> tiles = null;
if (chunk instanceof ChunkPrimerWrapper) {
chunk = ((ChunkPrimerWrapper) chunk).func_217336_u();
}
if (chunk instanceof Chunk) {
tiles = ((Chunk) chunk).getTileEntityMap();
}
if (chunk instanceof ChunkPrimer) {
tiles = ((ChunkPrimer) chunk).getTileEntities();
}
if (tiles != null) {
for (TileEntity tile : tiles.values()) {
if (tile instanceof IChunkLoadTile) {
((IChunkLoadTile) tile).onChunkLoad();
}
}
}
}
public static MwChunk copyToMwChunk(Chunk chunk) {
byte[][] msbArray = new byte[16][];
byte[][] lsbArray = new byte[16][];
byte[][] metaArray = new byte[16][];
byte[][] lightingArray = new byte[16][];
ExtendedBlockStorage[] storageArrays = chunk.getBlockStorageArray();
if (storageArrays != null) {
for (ExtendedBlockStorage storage : storageArrays) {
if (storage != null) {
int y = (storage.getYLocation() >> 4) & 0xf;
lsbArray[y] = storage.getBlockLSBArray();
msbArray[y] = (storage.getBlockMSBArray() != null) ? storage.getBlockMSBArray().data : null;
metaArray[y] = (storage.getMetadataArray() != null) ? storage.getMetadataArray().data : null;
lightingArray[y] = (storage.getBlocklightArray() != null) ? storage.getBlocklightArray().data : null;
}
}
}
return new MwChunk(chunk.xPosition, chunk.zPosition, chunk.worldObj.provider.dimensionId,
msbArray, lsbArray, metaArray, lightingArray, chunk.getBiomeArray());
}
/**
* Initializes an entities type on construction to specify what group this
* entity is in for activation ranges.
*
* @param entity
* @return group id
*/
public static byte initializeEntityActivationType(Entity entity)
{
Chunk chunk = null;
// Cauldron start - account for entities that dont extend EntityMob, EntityAmbientCreature, EntityCreature
if ( entity instanceof EntityMob || entity instanceof EntitySlime || entity.isCreatureType(EnumCreatureType.monster, false)) // Cauldron - account for entities that dont extend EntityMob
{
return 1; // Monster
} else if ( entity instanceof EntityCreature || entity instanceof EntityAmbientCreature || entity.isCreatureType(EnumCreatureType.creature, false)
|| entity.isCreatureType(EnumCreatureType.waterCreature, false) || entity.isCreatureType(EnumCreatureType.ambient, false))
{
return 2; // Animal
// Cauldron end
} else
{
return 3; // Misc
}
}
@Nonnull
@Override
public Chunk generateChunk(int x, int z)
{
ChunkPrimer chunkprimer = new ChunkPrimer();
// Get a list of blocks to check lighting for, as a "side effect" of
// actually generating the clouds
List<Pair<BlockPos, BlockPos>> litBlocks = generate(x, z, chunkprimer);
Chunk chunk = new Chunk(world, chunkprimer, x, z);
litBlocks.forEach(pair ->
{
BlockPos lower = pair.getFirst();
BlockPos upper = pair.getSecond();
for (int i = 0; i < 15; i++)
{
if (lower.getY() + i > upper.getY()) return;
chunk.setLightFor(EnumSkyBlock.BLOCK, lower.up(i), 15 - i);
}
});
return chunk;
}
private void setSemiBlock(World world, int x, int y, int z, ISemiBlock semiBlock, Chunk chunk){
if(semiBlock != null && !registeredTypes.containsValue(semiBlock.getClass())) throw new IllegalStateException("ISemiBlock \"" + semiBlock + "\" was not registered!");
ChunkPosition pos = new ChunkPosition(x, y, z);
if(semiBlock != null) {
semiBlock.initialize(world, pos);
addingBlocks.add(semiBlock);
} else {
ISemiBlock removedBlock = getOrCreateMap(chunk).get(pos);
if(removedBlock != null) {
removedBlock.invalidate();
for(EntityPlayerMP player : syncList.get(chunk)) {
NetworkHandler.sendTo(new PacketSetSemiBlock(pos, null), player);
}
}
}
chunk.setChunkModified();
}
public static void markSchematicChunkForRenderUpdate(BlockPos pos)
{
World world = SchematicWorldHandler.getSchematicWorld();
if (world != null)
{
Long2ObjectMap<Chunk> schematicChunks = ((IMixinChunkProviderClient) (Object) world.getChunkProvider()).getLoadedChunks();
Long2ObjectMap<Chunk> clientChunks = ((IMixinChunkProviderClient) (Object) Minecraft.getMinecraft().world.getChunkProvider()).getLoadedChunks();
long key = ChunkPos.asLong(pos.getX() >> 4, pos.getZ() >> 4);
if (schematicChunks.containsKey(key) && clientChunks.containsKey(key))
{
RenderGlobal rg = LitematicaRenderer.getInstance().getWorldRenderer();
rg.markBlockRangeForRenderUpdate(pos.getX() - 1, pos.getY() - 1, pos.getZ() - 1,pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
}
}
}
public static WizardryChunk get(Chunk chunk)
{
WizardryChunk cap = chunk.getCapability(capability(), null);
if (cap == null)
throw new IllegalStateException("Missing capability: " + chunk.getWorld().getWorldInfo().getWorldName() + "/" + chunk.getWorld().provider.getDimensionType().getName() + "/(" + chunk.x + ","+ chunk.z + ")");
return cap;
}
public boolean hasEntities(Chunk nmsChunk) {
ClassInheritanceMultiMap<Entity>[] entities = nmsChunk.getEntityLists();
for (int i = 0; i < entities.length; i++) {
ClassInheritanceMultiMap<Entity> slice = entities[i];
if (slice != null && !slice.isEmpty()) {
return true;
}
}
return false;
}
public boolean hasEntities(Chunk nmsChunk) {
ClassInheritanceMultiMap<Entity>[] entities = nmsChunk.getEntityLists();
for (int i = 0; i < entities.length; i++) {
ClassInheritanceMultiMap<Entity> slice = entities[i];
if (slice != null && !slice.isEmpty()) {
return true;
}
}
return false;
}
@Nullable
public static TileEntity getTileSafe(IBlockAccess worldIn, BlockPos pos) {
TileEntity target = worldIn instanceof ChunkCache ?
((ChunkCache)worldIn).getTileEntity(pos, Chunk.EnumCreateEntityType.CHECK)
:
worldIn.getTileEntity(pos);
return target;
}
@Override
public void setHeightMap(FaweChunk chunk, byte[] heightMap) {
Chunk forgeChunk = (Chunk) chunk.getChunk();
if (forgeChunk != null) {
int[] otherMap = forgeChunk.getHeightMap();
for (int i = 0; i < heightMap.length; i++) {
int newHeight = heightMap[i] & 0xFF;
int currentHeight = otherMap[i];
if (newHeight > currentHeight) {
otherMap[i] = newHeight;
}
}
}
}
private Map<ChunkPosition, ISemiBlock> getOrCreateMap(Chunk chunk){
Map<ChunkPosition, ISemiBlock> map = semiBlocks.get(chunk);
if(map == null) {
map = new HashMap<ChunkPosition, ISemiBlock>();
semiBlocks.put(chunk, map);
syncList.put(chunk, new HashSet<EntityPlayerMP>());
}
return map;
}
@Override
public ExtendedBlockStorage[] getCachedSections(World world, int cx, int cz) {
Chunk chunk = world.getChunkProvider().getLoadedChunk(cx, cz);
if (chunk != null) {
return chunk.getBlockStorageArray();
}
return null;
}
@Override
public void add(long key, Object value) {
if(value instanceof Chunk)
{
Chunk c = (Chunk) value;
chunkt_TH.put(c);
vanilla.put(V2B(key), c);
}
}
@Override
public void exec(String input) {
if (!this.clamp(input, 2, 2)) {
this.printUsage();
return;
}
final Minecraft mc = Minecraft.getMinecraft();
final BlockPos pos = mc.player.getPosition();
final Chunk chunk = mc.world.getChunk(pos);
final Biome biome = chunk.getBiome(pos, mc.world.getBiomeProvider());
System.out.println(biome.getSpawnableList(EnumCreatureType.CREATURE));
}
@Override
public CompoundTag getTileEntity(Chunk chunk, int x, int y, int z) {
Map<BlockPos, TileEntity> tiles = chunk.getTileEntityMap();
pos.setPos(x, y, z);
TileEntity tile = tiles.get(pos);
return tile != null ? getTag(tile) : null;
}
private void addSearcher(Chunk chunk, Block block, int dimensionId)
{
stopPool2Tasks();
ChunkSearcher searcher = new ChunkSearcher(chunk, block, dimensionId);
searchers.put(chunk, searcher);
searcher.startSearching(pool1);
}
/**
* @param world
* @param oldPos
* @param newPos
* @param physicsObjectOptional Used when we're using this to copy from world to physics object;
* should be empty when other way around.
*/
public static void copyBlockToPos(World world, BlockPos oldPos, BlockPos newPos,
Optional<PhysicsObject> physicsObjectOptional) {
// To avoid any updates crap, just edit the chunk data array directly.
// These look switched, but trust me they aren't
IBlockState oldState = world.getBlockState(newPos);
IBlockState newState = world.getBlockState(oldPos);
// A hacky way to set the block state within the chunk while avoiding any block updates.
Chunk chunkToSet = world.getChunk(newPos);
int storageIndex = newPos.getY() >> 4;
// Check that we're placing the block in a valid position
if (storageIndex < 0 || storageIndex >= chunkToSet.storageArrays.length) {
// Invalid position, abort!
return;
}
if (chunkToSet.storageArrays[storageIndex] == Chunk.NULL_BLOCK_STORAGE) {
chunkToSet.storageArrays[storageIndex] = new ExtendedBlockStorage(storageIndex << 4,
true);
}
chunkToSet.storageArrays[storageIndex]
.set(newPos.getX() & 15, newPos.getY() & 15, newPos.getZ() & 15, newState);
// Only want to send the update to clients and nothing else, so we use flag 2.
world.notifyBlockUpdate(newPos, oldState, newState, 2);
// Pretty messy to put this here but it works. Basically the ship keeps track of which of its chunks are
// actually being used for performance reasons.
if (physicsObjectOptional.isPresent()) {
int minChunkX = physicsObjectOptional.get()
.getOwnedChunks()
.minX();
int minChunkZ = physicsObjectOptional.get()
.getOwnedChunks()
.minZ();
}
// Now that we've copied the block to the position, copy the tile entity
copyTileEntityToPos(world, oldPos, newPos, physicsObjectOptional);
}
private void addSearchersInRange(ChunkPos center, int chunkRange,
Block block)
{
ArrayList<Chunk> chunksInRange = getChunksInRange(center, chunkRange);
for(Chunk chunk : chunksInRange)
{
if(searchers.containsKey(chunk))
continue;
addSearcher(chunk, block);
}
}
@Override
public Chunk loadChunk(World world, int x, int z, boolean generate) {
ChunkProviderServer provider = (ChunkProviderServer) world.getChunkProvider();
if (generate) {
return provider.provideChunk(x, z);
} else {
return provider.loadChunk(x, z);
}
}
@Override
public CompoundTag getTileEntity(Chunk chunk, int x, int y, int z) {
Map<BlockPos, TileEntity> tiles = chunk.getTileEntityMap();
pos.setPos(x, y, z);
TileEntity tile = tiles.get(pos);
return tile != null ? getTag(tile) : null;
}
private void addSearcher(Chunk chunk, Block block)
{
stopPool2Tasks();
ChunkSearcher searcher = new ChunkSearcher(chunk, block);
searchers.put(chunk, searcher);
searcher.startSearching(pool1);
}
@Override
public CompoundTag getTileEntity(Chunk chunk, int x, int y, int z) {
Map<BlockPos, TileEntity> tiles = chunk.getTileEntityMap();
pos.setPos(x, y, z);
TileEntity tile = tiles.get(pos);
return tile != null ? getTag(tile) : null;
}
public synchronized void saveChunks() {
for (Map.Entry<Chunk, Integer> entry : this.chunkMap.entrySet()) {
int flags = entry.getValue();
if ((flags & VIEWED_FLAG) != 0) {
this.addSaveChunkTask(entry.getKey());
}
}
}
@Override
public CompoundTag getTileEntity(Chunk chunk, int x, int y, int z) {
Map<BlockPos, TileEntity> tiles = chunk.getTileEntityMap();
pos.set(x, y, z);
TileEntity tile = tiles.get(pos);
return tile != null ? getTag(tile) : null;
}
@Override
public ExtendedBlockStorage[] getCachedSections(World world, int cx, int cz) {
Chunk chunk = world.getChunkProvider().getLoadedChunk(cx, cz);
if (chunk != null) {
return chunk.getBlockStorageArray();
}
return null;
}
/**
* Driver for the update loop. If the machine is assembled, runs
* the game logic update method.
* @see erogenousbeef.core.multiblock.MultiblockControllerBase#update() //TODO Fix this Javadoc
*/
public final void updateMultiblockEntity() {
if(connectedParts.isEmpty()) {
// This shouldn't happen, but just in case...
MultiblockRegistry.addDeadController(this.worldObj, this);
return;
}
if(this.assemblyState != AssemblyState.Assembled) {
// Not assembled - don't run game logic
return;
}
if(worldObj.isRemote) {
updateClient();
}
else if(updateServer()) {
// If this returns true, the server has changed its internal data.
// If our chunks are loaded (they should be), we must mark our chunks as dirty.
if(minimumCoord != null && maximumCoord != null &&
this.worldObj.checkChunksExist(minimumCoord.x, minimumCoord.y, minimumCoord.z,
maximumCoord.x, maximumCoord.y, maximumCoord.z)) {
int minChunkX = minimumCoord.x >> 4;
int minChunkZ = minimumCoord.z >> 4;
int maxChunkX = maximumCoord.x >> 4;
int maxChunkZ = maximumCoord.z >> 4;
for(int x = minChunkX; x <= maxChunkX; x++) {
for(int z = minChunkZ; z <= maxChunkZ; z++) {
// Ensure that we save our data, even if the our save delegate is in has no TEs.
Chunk chunkToSave = this.worldObj.getChunkFromChunkCoords(x, z);
chunkToSave.setChunkModified();
}
}
}
}
// Else: Server, but no need to save data.
}
private StructureStart forceStructureStart(IWorld worldIn, ChunkGenerator <? extends ChunkGeneratorConfig > generator, Random rand, long packedChunkPos)
{
ChunkPos chunkpos = new ChunkPos(packedChunkPos);
StructureStart structurestart;
Chunk ichunk = worldIn.getChunk(chunkpos.x, chunkpos.z, ChunkStatus.STRUCTURE_STARTS, false);
if (ichunk != null)
{
structurestart = ichunk.getStructureStart(this.getName());
if (structurestart != null && structurestart != StructureStart.DEFAULT)
{
return structurestart;
}
}
Biome biome_1 = generator.getBiomeSource().getBiomeForNoiseGen((chunkpos.getStartX() + 9) >> 2, 0, (chunkpos.getStartZ() + 9) >> 2 );
StructureStart structurestart1 = getStructureStartFactory().create((StructureFeature)(Object)this, chunkpos.x, chunkpos.z, BlockBox.empty(),0,generator.getSeed());
structurestart1.initialize(generator, ((ServerWorld)worldIn).getStructureManager() , chunkpos.x, chunkpos.z, biome_1);
structurestart = structurestart1.hasChildren() ? structurestart1 : StructureStart.DEFAULT;
if (structurestart.hasChildren())
{
worldIn.getChunk(chunkpos.x, chunkpos.z).setStructureStart(this.getName(), structurestart);
}
//long2objectmap.put(packedChunkPos, structurestart);
return structurestart;
}