Skip to content

Commit

Permalink
Add mc.entity to change web user Sheep or disable. Closes GH-16
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshinm committed Apr 16, 2017
1 parent aadf9a3 commit 49d74e6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Configures what part of your world to expose:
* `y_center` (78): " ", Y coordinate
* `z_center` (93): " ", Z coordinate
* `radius` (16): range out of the center to expose in each direction (cube), setting too high will slow down web client loading
* `entity` ("Sheep"): name of entity class to spawn on server for web users, set to "" to disable
* `entity_custom_names` (true): add web player names to the spawned entity's nametag if true
* `entity_disable_gravity` (true): disable gravity for the spawned entities if true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public void onEnable() {

int httpPort = 4081;

String entityClassName = "Sheep";
boolean setCustomNames = true;
boolean disableGravity = true;

Expand Down Expand Up @@ -69,6 +70,7 @@ public void onEnable() {
config.addDefault("http.external_address", ourExternalAddress);
config.addDefault("http.external_port", ourExternalPort);

config.addDefault("mc.entity", entityClassName);
config.addDefault("mc.entity_custom_names", setCustomNames);
config.addDefault("mc.entity_disable_gravity", disableGravity);
config.addDefault("mc.world", world);
Expand All @@ -89,6 +91,7 @@ public void onEnable() {
ourExternalAddress = this.getConfig().getString("http.external_address");
ourExternalPort = this.getConfig().getInt("http.external_port");

entityClassName = this.getConfig().getString("mc.entity");
setCustomNames = this.getConfig().getBoolean("mc.entity_custom_names");
disableGravity = this.getConfig().getBoolean("mc.entity_disable_gravity");
world = this.getConfig().getString("mc.world");
Expand All @@ -111,7 +114,7 @@ public void onEnable() {

webSocketServerThread.blockBridge = new BlockBridge(webSocketServerThread, world, x_center, y_center, z_center, radius, y_offset, allowBreakPlaceBlocks, allowSigns);
webSocketServerThread.playersBridge = new PlayersBridge(webSocketServerThread, allowChatting, seeChat, seePlayers);
webSocketServerThread.webPlayerBridge = new WebPlayerBridge(webSocketServerThread, setCustomNames, disableGravity);
webSocketServerThread.webPlayerBridge = new WebPlayerBridge(webSocketServerThread, setCustomNames, disableGravity, entityClassName);

// Register our events
PluginManager pm = getServer().getPluginManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,28 @@ public class WebPlayerBridge {

private boolean setCustomNames;
private boolean disableGravity;
private Class entityClass;

public WebPlayerBridge(WebSocketServerThread webSocketServerThread, boolean setCustomNames, boolean disableGravity) {
public WebPlayerBridge(WebSocketServerThread webSocketServerThread, boolean setCustomNames, boolean disableGravity, String entityClassName) {
this.webSocketServerThread = webSocketServerThread;
this.setCustomNames = setCustomNames;
this.disableGravity = disableGravity;

if (entityClassName == null || "".equals(entityClassName)) {
this.entityClass = null;
} else {
try {
this.entityClass = Class.forName("org.bukkit.entity." + entityClassName);
} catch (ClassNotFoundException ex) {
ex.printStackTrace();

// HumanEntity.class fails on Glowstone with https://gist.github.com/satoshinm/ebc87cdf1d782ba91b893fe24cd8ffd2
// so use sheep instead for now. TODO: spawn ala GlowNPC: https://github.com/satoshinm/WebSandboxMC/issues/13
System.out.println("No such entity class " + entityClassName + ", falling back to Sheep");
this.entityClass = Sheep.class;
}
}

this.lastPlayerID = 0;
this.channelId2name = new HashMap<ChannelId, String>();
this.name2channelId = new HashMap<String, ChannelId>();
Expand All @@ -46,28 +62,23 @@ public String newPlayer(final Channel channel) {

webSocketServerThread.sendLine(channel, "T,Welcome to WebSandboxMC, "+theirName+"!");

// HumanEntity.class fails on Glowstone with https://gist.github.com/satoshinm/ebc87cdf1d782ba91b893fe24cd8ffd2
// so use sheep instead for now. TODO: spawn ala GlowNPC: https://github.com/satoshinm/WebSandboxMC/issues/13
// TODO: configurable Bukkit entity types
// TODO: allow disabling spawning Bukkit entity, but if disabled, would still have to track id for web entity
//Class entityClass = HumanEntity.class;
Class entityClass = Sheep.class;

// Spawn an entity in the web user's place
Location location = webSocketServerThread.blockBridge.spawnLocation;
Entity entity = webSocketServerThread.blockBridge.world.spawn(location, entityClass);
if (setCustomNames) {
entity.setCustomName(theirName); // name tag
entity.setCustomNameVisible(true);
if (this.entityClass != null) {
// Spawn an entity in the web user's place
Location location = webSocketServerThread.blockBridge.spawnLocation;
Entity entity = webSocketServerThread.blockBridge.world.spawn(location, this.entityClass);
if (setCustomNames) {
entity.setCustomName(theirName); // name tag
entity.setCustomNameVisible(true);
}
if (disableGravity) {
entity.setGravity(false); // allow flying TODO: this doesn't seem to work on Glowstone? drops like a rock
}
channelId2Entity.put(channel.id(), entity);

// Notify other web clients (except this one) of this new user
webSocketServerThread.broadcastLineExcept(channel.id(), "P," + entity.getEntityId() + "," + webSocketServerThread.playersBridge.encodeLocation(location));
webSocketServerThread.broadcastLineExcept(channel.id(), "N," + entity.getEntityId() + "," + theirName);
}
if (disableGravity) {
entity.setGravity(false); // allow flying TODO: this doesn't seem to work on Glowstone? drops like a rock
}
channelId2Entity.put(channel.id(), entity);

// Notify other web clients (except this one) of this new user
webSocketServerThread.broadcastLineExcept(channel.id(), "P,"+entity.getEntityId()+","+webSocketServerThread.playersBridge.encodeLocation(location));
webSocketServerThread.broadcastLineExcept(channel.id(), "N,"+entity.getEntityId()+","+theirName);

// TODO: should this go to Bukkit chat, too/instead? make configurable?
webSocketServerThread.broadcastLine("T," + theirName + " has joined.");
Expand All @@ -76,6 +87,11 @@ public String newPlayer(final Channel channel) {
}

public void clientMoved(final Channel channel, final double x, final double y, final double z, final double rx, final double ry) {
if (this.entityClass == null) {
// No bukkit entity, no web-bsaed entity for other players either TODO: synthesize a placeholder entity id for web-to-web only?
return;
}

final Entity entity = this.channelId2Entity.get(channel.id());

Location location = webSocketServerThread.blockBridge.toBukkitPlayerLocation(x, y, z);
Expand Down

0 comments on commit 49d74e6

Please sign in to comment.