07-17-2011, 09:45 PM
I really need help on understanding why this code will not work...
Also, how can I make it so chat messages can be longer? Would it be using this code?
I thought I could get a quicker response if I caught you on Buildism... I thought I had an account here, but I didn't, so now I do. haha.
Thanks.
Code:
package org.opencraft.server.net.packet.handler.impl;
/*
* OpenCraft License
*
* Copyright (c) 2009 Graham Edgecombe, S�ren Enevoldsen and Brett Russell.
* All rights reserved.
*
* Distribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Distributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Distributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of the OpenCraft nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import org.opencraft.server.game.impl.CTFGameMode;
import org.opencraft.server.model.Player;
import org.opencraft.server.model.PlayerUntagger;
import org.opencraft.server.model.Position;
import org.opencraft.server.model.Rotation;
import org.opencraft.server.model.World;
import org.opencraft.server.net.MinecraftSession;
import org.opencraft.server.net.packet.Packet;
import org.opencraft.server.net.packet.handler.PacketHandler;
/**
* A packet handler which handles movement packets.
* @author Graham Edgecombe
*/
public class MovementPacketHandler implements PacketHandler<MinecraftSession> {
Position lastPosition = null;
long lastMoveTime = 0;
@Override
public void handlePacket(MinecraftSession session, Packet packet) {
if (!session.isAuthenticated()) {
return;
}
final Player player = session.getPlayer();
Position oldPosition = player.getPosition();
Rotation oldRotation = player.getOldRotation();
final int oldX = oldPosition.getX();
final int oldY = oldPosition.getY();
final int oldZ = oldPosition.getZ();
final int x = packet.getNumericField("x").intValue();
final int y = packet.getNumericField("y").intValue();
final int z = packet.getNumericField("z").intValue();
Position blockPosition = new Position((x - 16)/32, (y - 16)/32, (z - 16)/32);
if(lastPosition == null || (Math.abs(blockPosition.getX() - lastPosition.getX()) > 2 || Math.abs(blockPosition.getY() - lastPosition.getY()) > 2 || Math.abs(blockPosition.getZ() - lastPosition.getZ()) > 2))
{
lastPosition = blockPosition;
lastMoveTime = System.currentTimeMillis();
}
else if(System.currentTimeMillis() - lastMoveTime > 300000)
{
player.getActionSender().sendLoginFailure("You were kicked for being AFK!");
World.getWorld().broadcast("- "+player.parseName()+" was kicked for being AFK!");
}
int dx = Math.abs(x - oldX);
int dy = Math.abs(y - oldY);
int dz = Math.abs(z - oldZ);
if((dx > 500 || dy > 500 || dz > 500))
{
int divider = World.getWorld().getLevel().divider;
if(player.team == 0 && blockPosition.getX() > divider)
{
player.sendToTeamSpawn();
player.getActionSender().sendChatMessage("- &eYou may not respawn on the other team's side!");
}
else if(player.team == 1 && blockPosition.getX() < divider)
{
player.sendToTeamSpawn();
player.getActionSender().sendChatMessage("- &eYou may not respawn on the other team's side!");
}
if(player.hasFlag)
{
CTFGameMode ctf = (CTFGameMode) World.getWorld().getGameMode();
if(player.team == 0)
{
ctf.blueFlagTaken = false;
ctf.placeBlueFlag();
}
else
{
ctf.redFlagTaken = false;
ctf.placeRedFlag();
}
ctf.antiStalemate = false;
player.hasFlag = false;
World.getWorld().broadcast("- "+player.parseName()+" dropped the flag due to respawning!");
}
}
if((z - 16)/32 < World.getWorld().getLevel().floor && !player.safe)
{
player.safe = true;
new Thread(new PlayerUntagger(player)).start();
World.getWorld().broadcast("- "+player.parseName()+" died!");
player.sendToTeamSpawn();
if(player.hasFlag)
{
CTFGameMode ctf = (CTFGameMode) World.getWorld().getGameMode();
if(player.team == 0)
{
ctf.blueFlagTaken = false;
ctf.placeBlueFlag();
}
else
{
ctf.redFlagTaken = false;
ctf.placeRedFlag();
}
ctf.antiStalemate = false;
player.hasFlag = false;
World.getWorld().broadcast("- "+player.parseName()+" dropped the flag!");
}
}
final int rotation = packet.getNumericField("rotation").intValue();
final int look = packet.getNumericField("look").intValue();
player.setPosition(new Position(x, y, z));
player.setRotation(new Rotation(rotation, look));
}
}
Also, how can I make it so chat messages can be longer? Would it be using this code?
Code:
public void sendChatMessage(int id, String message) {
PacketBuilder bldr = new PacketBuilder(PersistingPacketManager.getPacketManager().getOutgoingPacket(13));
String message2 = "";
if(message.length() > 64)
{
message2 = message.substring(64);
int idx = message.lastIndexOf("&");
if(idx != -1)
message2 = "&"+message.charAt(idx + 1)+message2;
message = message.substring(0, 64);
}
bldr.putByte("id", id);
bldr.putString("message", message);
session.send(bldr.toPacket());
if(!message2.equals(""))
sendChatMessage(id, "> "+message2);
}
I thought I could get a quicker response if I caught you on Buildism... I thought I had an account here, but I didn't, so now I do. haha.
Thanks.