Skip to content

Commit

Permalink
Merge pull request #36 from TridentSDK/bleeding-edge
Browse files Browse the repository at this point in the history
Pull in for updated release
  • Loading branch information
mythbusterma committed Jul 14, 2015
2 parents 0ad6cfe + b22172c commit 872f6ec
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 78 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>net.tridentsdk</groupId>
<artifactId>trident</artifactId>
<version>1.0-SNAPSHOT</version>
<version>0.3-SNAPSHOT</version>
<name>Trident</name>
<url>http://www.tridentsdk.net/</url>
<packaging>jar</packaging>
Expand Down
20 changes: 9 additions & 11 deletions src/main/java/net/tridentsdk/server/entity/TridentEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.tridentsdk.server.packets.play.out.PacketPlayOutEntityVelocity;
import net.tridentsdk.server.player.TridentPlayer;
import net.tridentsdk.server.threads.ThreadsHandler;
import net.tridentsdk.server.world.TridentChunk;
import net.tridentsdk.server.world.TridentWorld;
import net.tridentsdk.util.Vector;
import net.tridentsdk.util.WeakEntity;
Expand Down Expand Up @@ -183,16 +184,6 @@ protected void encodeMetadata(ProtocolMetadata protocolMeta) {
doEncodeMeta(protocolMeta);
}

/**
* Moves the entity to the new coordinates. Not for teleportation.
*
* @param newCoords the new location for the entity
*/
public void doMove(Position newCoords) {
HANDLER.trackMovement(this, position(), newCoords);
this.setLocation(newCoords);
}

@Override
public void teleport(double x, double y, double z) {
this.teleport(Position.create(this.world(), x, y, z));
Expand Down Expand Up @@ -233,7 +224,14 @@ public Position position() {
return this.loc;
}

public void setLocation(Position loc) {
public void setPosition(Position loc) {
TridentChunk from = (TridentChunk) position().chunk();
TridentChunk chunk = (TridentChunk) loc.chunk();
if (!from.equals(chunk)) {
from.entitiesInternal().remove(this);
chunk.entitiesInternal().add(this);
}

this.loc = loc;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,16 @@ public void handleReceived(ClientConnection connection) {

// parse the response and set the ID
JsonArray array = PacketLoginInEncryptionResponse.GSON.fromJson(sb.toString(), JsonArray.class);
JsonArray jsonArray = array.getAsJsonArray();
if (jsonArray.size() == 0) {
// TODO more checks, session validity
connection.sendPacket(
new PacketLoginOutDisconnect().setJsonMessage("This server is in online-mode"));
return;
}

id = UUID.fromString(PacketLoginInEncryptionResponse.idDash.matcher(
array.getAsJsonArray().get(0).getAsJsonObject().get("id").getAsString())
jsonArray.get(0).getAsJsonObject().get("id").getAsString())
.replaceAll("$1-$2-$3-$4-$5"));

if (TridentPlayer.getPlayer(id) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,6 @@ public void handleReceived(ClientConnection connection) {
return;
}

player.setLocation(to);
player.setPosition(to);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public void handleReceived(ClientConnection connection) {
return;
}

player.setLocation(to);
player.setPosition(to);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
package net.tridentsdk.server.packets.play.out;

import io.netty.buffer.ByteBuf;
import net.tridentsdk.server.data.Slot;
import net.tridentsdk.server.netty.Codec;
import net.tridentsdk.server.netty.packet.OutPacket;

public class PacketPlayOutEntityEquipment extends OutPacket {

protected int entityId;
protected short slot;
protected long item;
// TODO: mojang slot shit
protected Slot item;

@Override
public int id() {
Expand All @@ -41,10 +41,14 @@ public short slot() {
return this.slot;
}

public Slot item() {
return this.item;
}

@Override
public void encode(ByteBuf buf) {
Codec.writeVarInt32(buf, this.entityId);
buf.writeShort((int) this.slot);
buf.writeLong(item); // TODO this is the way?
item.write(buf);
}
}
45 changes: 24 additions & 21 deletions src/main/java/net/tridentsdk/server/player/TridentPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.tridentsdk.docs.InternalUseOnly;
import net.tridentsdk.entity.Entity;
import net.tridentsdk.entity.living.Player;
import net.tridentsdk.entity.types.EntityType;
import net.tridentsdk.event.player.PlayerJoinEvent;
import net.tridentsdk.factory.Factories;
import net.tridentsdk.meta.ChatColor;
Expand All @@ -36,7 +37,6 @@
import net.tridentsdk.server.netty.ClientConnection;
import net.tridentsdk.server.netty.packet.Packet;
import net.tridentsdk.server.packets.play.out.*;
import net.tridentsdk.server.threads.TaskGroup;
import net.tridentsdk.server.threads.ThreadsHandler;
import net.tridentsdk.server.world.TridentChunk;
import net.tridentsdk.server.world.TridentWorld;
Expand Down Expand Up @@ -117,7 +117,7 @@ public static TridentPlayer spawnPlayer(ClientConnection connection, UUID id, St
p.abilities.canFly = 1;

// DEBUG =====
p.setLocation(new Position(p.world(), 0, 255, 0));
p.setPosition(new Position(p.world(), 0, 255, 0));
p.spawnLocation = new Position(p.world(), 0, 255, 0);
// =====

Expand Down Expand Up @@ -152,6 +152,7 @@ public static TridentPlayer spawnPlayer(ClientConnection connection, UUID id, St
p.encodeMetadata(metadata);
});

p.spawn();
return p;
}

Expand Down Expand Up @@ -225,11 +226,6 @@ public void resumeLogin() {
}
}

@Override // Overridden to execute on the player handler instead
public void tick() {
ThreadsHandler.playerExecutor().execute(this::doTick);
}

@Override
protected void doTick() {
int distance = viewDistance();
Expand All @@ -245,29 +241,34 @@ protected void doTick() {
ticksExisted.incrementAndGet();
}

public Set<ChunkLocation> cleanChunks() {
if (knownChunks.size() > 441) {
public void cleanChunks() {
int toClean = knownChunks.size() - 441;
if (toClean > 0) {
Position pos = position();
int x = (int) pos.x() / 16;
int z = (int) pos.z() / 16;
int viewDist = viewDistance();

TaskGroup.process(knownChunks).every(4).with(ThreadsHandler.chunkExecutor()).using((location) -> {
int cleaned = 0;
for (ChunkLocation location : knownChunks) {
int cx = location.x();
int cz = location.z();

int abs = Math.abs(cx - x);
int abs1 = Math.abs(cz - z);

// TODO some possibility of deviating
if (abs > viewDist || abs1 > viewDist) {
connection.sendPacket(new PacketPlayOutChunkData(new byte[512], location, true, (short) 0));
knownChunks.remove(location);
boolean tried = ((TridentWorld) world()).loadedChunks.tryRemove(location);
if (tried) {
connection.sendPacket(new PacketPlayOutChunkData(new byte[0], location, true, (short) 0));
knownChunks.remove(location);
cleaned++;
}
}
});
}

return knownChunks;
if (cleaned >= toClean) return;
}
}
}

@Override
Expand All @@ -279,10 +280,7 @@ protected void doRemove() {
}

@Override
public void setLocation(Position loc) {
ProtocolMetadata metadata = new ProtocolMetadata();
encodeMetadata(metadata);

public void setPosition(Position loc) {
players().stream()
.filter((p) -> !p.equals(this))
.forEach((p) -> {
Expand All @@ -292,7 +290,7 @@ public void setLocation(Position loc) {
.set("onGround", onGround));
});

super.setLocation(loc);
super.setPosition(loc);
}

/*
Expand Down Expand Up @@ -446,4 +444,9 @@ public void setViewDistance(int viewDistance) {
public int viewDistance() {
return Math.min(viewDistance, MAX_VIEW);
}

@Override
public EntityType type() {
return EntityType.PLAYER;
}
}
56 changes: 34 additions & 22 deletions src/main/java/net/tridentsdk/server/world/ChunkCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.google.common.collect.Lists;
import net.tridentsdk.concurrent.HeldValueLatch;
import net.tridentsdk.docs.AccessNoDoc;
import net.tridentsdk.server.threads.TaskGroup;
import net.tridentsdk.entity.types.EntityType;
import net.tridentsdk.server.threads.ThreadsHandler;
import net.tridentsdk.util.TridentLogger;
import net.tridentsdk.world.Chunk;
Expand All @@ -29,9 +29,10 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;

@AccessNoDoc
class ChunkCache {
public class ChunkCache {
private final ConcurrentMap<ChunkLocation, HeldValueLatch<TridentChunk>> cachedChunks = new ConcurrentHashMap<>();
private final TridentWorld world;

Expand Down Expand Up @@ -79,27 +80,38 @@ public TridentChunk get(ChunkLocation location, boolean gen) {
return (TridentChunk) chunk;
}

public void retain(Set<ChunkLocation> locations) {
// Generate is much more appropriate thread pool
// not only because it is used only for loading bytes
// but because using the chunk executor interferes
// with other tasks and might cause livelocks for no
// reason
TaskGroup.process(keys()).every(100).with(ThreadsHandler.saver()).using((loc) -> {
HeldValueLatch<TridentChunk> chunk = cachedChunks.get(loc);
TridentChunk rem;
if (chunk != null && chunk.hasValue()) {
rem = chunk.get();
} else {
// Remove the chunk once it is available
return;
}
public boolean tryRemove(ChunkLocation location) {
try {
return ThreadsHandler.genExecutor().submit(() -> {
HeldValueLatch<TridentChunk> chunk = cachedChunks.get(location);
if (chunk == null) {
return false;
}

if (!locations.contains(loc) && chunk.hasValue()) {
world.loader().saveChunk(rem);
cachedChunks.remove(loc);
}
});
if (chunk.hasValue()) { // No value = needs to generate
Chunk c = chunk.get();
if (c.entities() // Ensure there are no players
.stream()
.filter(e -> e.type().equals(EntityType.PLAYER))
.count() == 0) {
remove(location);
c.unload();

return true;
}
}

return false;
}).get();
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}

return false;
}

public void remove(ChunkLocation location) {
cachedChunks.remove(location);
}

public Set<ChunkLocation> keys() {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/net/tridentsdk/server/world/TridentChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@

package net.tridentsdk.server.world;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Lists;
import net.tridentsdk.Position;
import net.tridentsdk.base.Block;
import net.tridentsdk.base.Substance;
import net.tridentsdk.concurrent.TaskExecutor;
import net.tridentsdk.entity.Entity;
import net.tridentsdk.factory.Factories;
import net.tridentsdk.meta.nbt.*;
import net.tridentsdk.server.packets.play.out.PacketPlayOutChunkData;
import net.tridentsdk.server.threads.ThreadsHandler;
Expand All @@ -36,13 +39,15 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.stream.Stream;

public class TridentChunk implements Chunk {
private final TridentWorld world;
private final ChunkLocation location;
private final TaskExecutor executor = ThreadsHandler.chunkExecutor().scaledThread();
private final Set<Entity> entities = Factories.collect().createSet();
public volatile ChunkSection[] sections;
private volatile int lastFileAccess;
private volatile long lastModified;
Expand Down Expand Up @@ -78,6 +83,15 @@ protected void setLastFileAccess(int last) {
this.lastFileAccess = last;
}

@Override
public Set<Entity> entities() {
return ImmutableSet.copyOf(entities);
}

public Set<Entity> entitiesInternal() {
return entities;
}

@Override
public void generate() {
executor.addTask(() -> {
Expand Down Expand Up @@ -306,6 +320,12 @@ public void load(CompoundTag root) {
this.inhabitedTime = inhabitedTime.value(); // Cumulative number of ticks player have been in the chunk
}

@Override
public void unload() {
world.loader().saveChunk(this);
world.loadedChunks.remove(location);
}

public CompoundTag asNbt() {
CompoundTag root = new CompoundTag("root");
CompoundTag level = new CompoundTag("Level");
Expand Down
Loading

0 comments on commit 872f6ec

Please sign in to comment.