Add initial implementation of WECUI handling via Packet250 for Spout

Update for latest Spout changes
This commit is contained in:
zml2008 2012-08-02 19:26:19 -07:00
parent b81fec1776
commit d0684c7582
7 changed files with 196 additions and 52 deletions

View File

@ -30,8 +30,10 @@ import com.sk89q.worldedit.WorldVector;
import com.sk89q.worldedit.bags.BlockBag;
import com.sk89q.worldedit.cui.CUIEvent;
import org.spout.api.Client;
import org.spout.api.chat.style.ChatStyle;
import org.spout.api.entity.Entity;
import org.spout.api.entity.component.Controller;
import org.spout.api.geo.discrete.Point;
import org.spout.api.inventory.ItemStack;
import org.spout.api.player.Player;
@ -52,9 +54,13 @@ public class SpoutPlayer extends LocalPlayer {
@Override
public int getItemInHand() {
VanillaPlayer vanillaPlayer = (VanillaPlayer) player.getEntity().getController();
ItemStack itemStack = vanillaPlayer.getInventory().getQuickbar().getCurrentItem();
return itemStack != null ? ((VanillaMaterial) itemStack.getMaterial()).getMinecraftId() : 0;
Controller controller = player.getController();
if (controller instanceof VanillaPlayer) {
ItemStack itemStack = ((VanillaPlayer) controller).getInventory().getQuickbar().getCurrentItem();
return itemStack != null ? ((VanillaMaterial) itemStack.getMaterial()).getMinecraftId() : 0;
} else {
return 0;
}
}
@Override
@ -64,25 +70,28 @@ public class SpoutPlayer extends LocalPlayer {
@Override
public WorldVector getPosition() {
Point loc = player.getEntity().getPosition();
Point loc = player.getPosition();
return new WorldVector(SpoutUtil.getLocalWorld(loc.getWorld()),
loc.getX(), loc.getY(), loc.getZ());
}
@Override
public double getPitch() {
return player.getEntity().getPitch();
return player.getPitch();
}
@Override
public double getYaw() {
return player.getEntity().getYaw();
return player.getYaw();
}
@Override
public void giveItem(int type, int amt) {
VanillaPlayer vanillaPlayer = (VanillaPlayer) player.getEntity().getController();
vanillaPlayer.getInventory().addItem(new ItemStack(VanillaMaterials.getMaterial((short) type), amt), false);
Controller controller = player.getController();
if (controller instanceof VanillaPlayer) {
((VanillaPlayer) controller).getInventory()
.addItem(new ItemStack(VanillaMaterials.getMaterial((short) type), amt), false);
}
}
@Override
@ -115,10 +124,9 @@ public class SpoutPlayer extends LocalPlayer {
@Override
public void setPosition(Vector pos, float pitch, float yaw) {
final Entity entity = player.getEntity();
entity.setPosition(SpoutUtil.toPoint(entity.getWorld(), pos));
entity.setPitch(pitch);
entity.setYaw(yaw);
player.setPosition(SpoutUtil.toPoint(player.getWorld(), pos));
player.setPitch(pitch);
player.setYaw(yaw);
player.getNetworkSynchronizer().setPositionDirty();
}
@ -139,24 +147,12 @@ public class SpoutPlayer extends LocalPlayer {
@Override
public LocalWorld getWorld() {
return SpoutUtil.getLocalWorld(player.getEntity().getWorld());
return SpoutUtil.getLocalWorld(player.getWorld());
}
@Override
public void dispatchCUIEvent(CUIEvent event) {
String[] params = event.getParameters();
if (params.length > 0) {
player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + event.getTypeId()
+ "|" + StringUtil.joinString(params, "|"));
} else {
player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75" + event.getTypeId());
}
}
@Override
public void dispatchCUIHandshake() {
player.sendRawMessage("\u00A75\u00A76\u00A74\u00A75");
player.getSession().send(player.getSession().getEngine() instanceof Client, new WorldEditCUIMessage(event));
}
public Player getPlayer() {

View File

@ -60,7 +60,7 @@ public class SpoutPlayerBlockBag extends BlockBag {
*/
private void loadInventory() {
if (items == null) {
items = VanillaPlayerUtil.getInventory(player.getEntity()).getContents();
items = VanillaPlayerUtil.getInventory(player).getContents();
}
}
@ -207,7 +207,7 @@ public class SpoutPlayerBlockBag extends BlockBag {
@Override
public void flushChanges() {
if (items != null) {
InventoryBase inv = VanillaPlayerUtil.getInventory(player.getEntity());
InventoryBase inv = VanillaPlayerUtil.getInventory(player);
for (int i = 0; i < items.length && i < inv.getSize(); ++i) {
inv.setItem(i, items[i]);
}

View File

@ -0,0 +1,60 @@
/*
* WorldEdit
* Copyright (C) 2012 sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.spout;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.spout.api.protocol.MessageCodec;
import org.spout.api.util.Named;
import java.nio.charset.Charset;
/**
* @author zml2008
*/
public class WorldEditCUICodec extends MessageCodec<WorldEditCUIMessage> implements Named {
public static final Charset UTF_8_CHARSET = Charset.forName("UTF-8");
public WorldEditCUICodec(int opcode) {
super(WorldEditCUIMessage.class, opcode);
}
@Override
public ChannelBuffer encode(WorldEditCUIMessage message) {
byte[] data = message.getMessage().getBytes(UTF_8_CHARSET);
ChannelBuffer buffer = ChannelBuffers.buffer(data.length + 2);
buffer.writeShort(data.length);
buffer.writeBytes(data);
return buffer;
}
@Override
public WorldEditCUIMessage decode(ChannelBuffer buffer) {
byte[] data = new byte[buffer.readShort()];
buffer.readBytes(data);
String message = new String(data, UTF_8_CHARSET);
return new WorldEditCUIMessage(message);
}
@Override
public String getName() {
return "WECUI";
}
}

View File

@ -0,0 +1,53 @@
/*
* WorldEdit
* Copyright (C) 2012 sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.spout;
import com.sk89q.util.StringUtil;
import com.sk89q.worldedit.cui.CUIEvent;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.spout.api.protocol.Message;
import org.spout.api.util.SpoutToStringStyle;
public class WorldEditCUIMessage extends Message {
private final String message;
public WorldEditCUIMessage(String mesage) {
this.message = mesage;
}
public WorldEditCUIMessage(CUIEvent event) {
String[] params = event.getParameters();
if (params.length > 0) {
this.message = event.getTypeId() + '|' + StringUtil.joinString(event.getParameters(), "|");
} else {
this.message = event.getTypeId();
}
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return new ToStringBuilder(this, SpoutToStringStyle.INSTANCE)
.append("message", message)
.toString();
}
}

View File

@ -0,0 +1,52 @@
/*
* WorldEdit
* Copyright (C) 2012 sk89q <http://www.sk89q.com> and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.spout;
import com.sk89q.worldedit.LocalSession;
import org.spout.api.protocol.MessageHandler;
import org.spout.api.protocol.Session;
/**
* MessageHandler for WorldEditCUIMessage
*/
public class WorldEditCUIMessageHandler extends MessageHandler<WorldEditCUIMessage> {
private final WorldEditPlugin plugin;
public WorldEditCUIMessageHandler(WorldEditPlugin plugin) {
this.plugin = plugin;
}
@Override
public void handleServer(Session session, WorldEditCUIMessage message) {
LocalSession localSession = plugin.getSession(session.getPlayer());
if (localSession.hasCUISupport()) { // Already initialized
return;
}
String[] text = message.getMessage().split("\\|");
if (text.length > 1 && text[0].equalsIgnoreCase("v")) { // enough fields and right message
localSession.setCUISupport(true);
try {
localSession.setCUIVersion(Integer.parseInt(text[1]));
} catch (NumberFormatException e) {
plugin.getLogger().warning("Error while reading CUI init message: " + e.getMessage());
}
}
}
}

View File

@ -179,28 +179,6 @@ public class WorldEditListener implements Listener {
}
}
@EventHandler
public void onPlayerChat(PlayerChatEvent event) {
if (event.isCancelled()) {
return;
}
Matcher matcher = cuipattern.matcher(event.getMessage().getPlainString());
if (matcher.find()) {
String type = matcher.group(1);
String args = matcher.group(2);
if( type.equals("v") ) {
try {
plugin.getSession(event.getPlayer()).setCUIVersion(Integer.parseInt(args));
event.setCancelled(true);
} catch( NumberFormatException e ) {
}
}
}
}
@EventHandler
public void onWorldLoad(WorldLoadEvent event) {
if (event.getWorld().getGenerator() instanceof BiomeGenerator) {

View File

@ -36,6 +36,7 @@ import org.spout.api.command.CommandSource;
import org.spout.api.geo.World;
import org.spout.api.player.Player;
import org.spout.api.plugin.CommonPlugin;
import org.spout.api.protocol.Protocol;
import org.spout.api.scheduler.TaskPriority;
import java.io.*;
@ -99,6 +100,10 @@ public class WorldEditPlugin extends CommonPlugin {
// Now we can register events!
registerEvents();
for (Protocol proto : Protocol.getProtocols()) {
proto.registerPacket(WorldEditCUICodec.class, new WorldEditCUIMessageHandler(this));
}
getEngine().getScheduler().scheduleAsyncRepeatingTask(this,
new SessionTimer(controller, getEngine()), 6 * 1000, 6 * 1000, TaskPriority.LOWEST);
}
@ -304,7 +309,7 @@ public class WorldEditPlugin extends CommonPlugin {
}
LocalSession session = controller.getSession(wrapPlayer(player));
RegionSelector selector = session.getRegionSelector(SpoutUtil.getLocalWorld(player.getEntity().getWorld()));
RegionSelector selector = session.getRegionSelector(SpoutUtil.getLocalWorld(player.getWorld()));
try {
Region region = selector.getRegion();
@ -341,7 +346,7 @@ public class WorldEditPlugin extends CommonPlugin {
LocalSession session = controller.getSession(wrapPlayer(player));
RegionSelector sel = selection.getRegionSelector();
session.setRegionSelector(SpoutUtil.getLocalWorld(player.getEntity().getWorld()), sel);
session.setRegionSelector(SpoutUtil.getLocalWorld(player.getWorld()), sel);
session.dispatchCUISelection(wrapPlayer(player));
}