use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.
the class GlowPlayer method pulse.
@Override
public void pulse() {
super.pulse();
if (usageItem != null) {
if (usageItem == getItemInHand()) {
if (--usageTime == 0) {
ItemType item = ItemTable.instance().getItem(usageItem.getType());
if (item instanceof ItemFood) {
((ItemFood) item).eat(this, usageItem);
}
}
} else {
usageItem = null;
usageTime = 0;
}
}
if (digging != null) {
pulseDigging();
}
if (exhaustion > 4.0f) {
exhaustion -= 4.0f;
if (saturation > 0f) {
saturation = Math.max(saturation - 1f, 0f);
sendHealth();
} else if (world.getDifficulty() != Difficulty.PEACEFUL) {
FoodLevelChangeEvent event = EventFactory.callEvent(new FoodLevelChangeEvent(this, Math.max(food - 1, 0)));
if (!event.isCancelled()) {
food = event.getFoodLevel();
}
sendHealth();
}
}
if (getHealth() < getMaxHealth() && !isDead()) {
if (food > 18 && ticksLived % 80 == 0 || world.getDifficulty() == Difficulty.PEACEFUL) {
EntityRegainHealthEvent event1 = new EntityRegainHealthEvent(this, 1f, RegainReason.SATIATED);
EventFactory.callEvent(event1);
if (!event1.isCancelled()) {
setHealth(getHealth() + 1);
}
exhaustion = Math.min(exhaustion + 3.0f, 40.0f);
saturation -= 3;
}
}
if (food == 0 && getHealth() > 1 && ticksLived % 80 == 0) {
damage(1, DamageCause.STARVATION);
}
// stream world
streamBlocks();
processBlockChanges();
// add to playtime
incrementStatistic(Statistic.PLAY_ONE_TICK);
if (isSneaking()) {
incrementStatistic(Statistic.SNEAK_TIME);
}
// update inventory
for (InventoryMonitor.Entry entry : invMonitor.getChanges()) {
sendItemChange(entry.slot, entry.item);
}
// send changed metadata
List<MetadataMap.Entry> changes = metadata.getChanges();
if (!changes.isEmpty()) {
session.send(new EntityMetadataMessage(SELF_ID, changes));
}
// update or remove entities
List<Integer> destroyIds = new LinkedList<>();
for (Iterator<GlowEntity> it = knownEntities.iterator(); it.hasNext(); ) {
GlowEntity entity = it.next();
if (!isWithinDistance(entity) || entity.isRemoved()) {
destroyIds.add(entity.getEntityId());
it.remove();
} else {
entity.createUpdateMessage().forEach(session::send);
}
}
if (!destroyIds.isEmpty()) {
session.send(new DestroyEntitiesMessage(destroyIds));
}
// add entities
knownChunks.parallelStream().forEach(key -> {
GlowChunk chunk = world.getChunkAt(key.getX(), key.getZ());
chunk.getRawEntities().stream().filter(entity -> this != entity).filter(this::isWithinDistance).filter(entity -> !entity.isDead()).filter(entity -> !knownEntities.contains(entity)).filter(entity -> !hiddenEntities.contains(entity.getUniqueId())).forEach((entity) -> {
knownEntities.add(entity);
entity.createSpawnMessage().forEach(session::send);
});
});
if (passengerChanged) {
session.send(new SetPassengerMessage(SELF_ID, getPassengers().stream().mapToInt(Entity::getEntityId).toArray()));
}
getAttributeManager().sendMessages(session);
}
use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.
the class GlowWorld method setKeepSpawnInMemory.
@Override
public void setKeepSpawnInMemory(boolean keepLoaded) {
keepSpawnLoaded = keepLoaded;
if (spawnChunkLock != null) {
// update the chunk lock as needed
spawnChunkLock.clear();
if (keepSpawnLoaded) {
int centerX = spawnLocation.getBlockX() >> 4;
int centerZ = spawnLocation.getBlockZ() >> 4;
int radius = 4 * server.getViewDistance() / 3;
long loadTime = System.currentTimeMillis();
int total = ((radius << 1) + 1) * ((radius << 1) + 1), current = 0;
for (int x = centerX - radius; x <= centerX + radius; ++x) {
for (int z = centerZ - radius; z <= centerZ + radius; ++z) {
++current;
if (populateAnchoredChunks) {
getChunkManager().forcePopulation(x, z);
} else {
loadChunk(x, z);
}
spawnChunkLock.acquire(new Key(x, z));
if (System.currentTimeMillis() >= loadTime + 1000) {
int progress = 100 * current / total;
GlowServer.logger.info("Preparing spawn for " + name + ": " + progress + "%");
loadTime = System.currentTimeMillis();
}
}
}
} else {
// attempt to immediately unload the spawn
chunks.unloadOldChunks();
}
}
}
use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.
the class GlowWorld method updateBlocksInActiveChunks.
private void updateBlocksInActiveChunks() {
for (Key key : activeChunksSet) {
int cx = key.getX();
int cz = key.getZ();
// check the chunk is loaded
if (isChunkLoaded(cx, cz)) {
GlowChunk chunk = getChunkAt(cx, cz);
// thunder
maybeStrikeLightningInChunk(cx, cz);
// block ticking
// we will choose 3 blocks per chunk's section
ChunkSection[] sections = chunk.getSections();
for (int i = 0; i < sections.length; i++) {
updateBlocksInSection(chunk, sections[i], i);
}
}
}
}
use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.
the class BlockEntity method updateInRange.
/**
* Update this BlockEntity's visible state to all players in range.
*/
public final void updateInRange() {
Key key = new Key(block.getChunk().getX(), block.getChunk().getZ());
block.getWorld().getRawPlayers().stream().filter(player -> player.canSeeChunk(key)).forEach(this::update);
}
use of net.glowstone.chunk.GlowChunk.Key in project Glowstone by GlowstoneMC.
the class NbtStructureDataService method readStructuresData.
@Override
public Map<Integer, GlowStructure> readStructuresData() {
Map<Integer, GlowStructure> structures = new HashMap<>();
for (StructureStore<?> store : StructureStorage.getStructureStores()) {
File structureFile = new File(structureDir, store.getId() + ".dat");
if (structureFile.exists()) {
try (NBTInputStream in = new NBTInputStream(new FileInputStream(structureFile))) {
CompoundTag data = new CompoundTag();
data = in.readCompound();
if (data.isCompound("data")) {
data = data.getCompound("data");
if (data.isCompound("Features")) {
CompoundTag features = data.getCompound("Features");
features.getValue().keySet().stream().filter(features::isCompound).forEach(key -> {
GlowStructure structure = StructureStorage.loadStructure(world, features.getCompound(key));
structures.put(new Key(structure.getChunkX(), structure.getChunkZ()).hashCode(), structure);
});
}
} else {
server.getLogger().log(Level.SEVERE, "No data tag in " + structureFile);
}
} catch (IOException e) {
server.getLogger().log(Level.SEVERE, "Failed to read structure data from " + structureFile, e);
}
}
}
return structures;
}
Aggregations