Skip to content
This repository has been archived by the owner on May 3, 2018. It is now read-only.

Commit

Permalink
first beta release! (0.0.1-beta) 😄
Browse files Browse the repository at this point in the history
  • Loading branch information
DefinitlyEvil committed Jan 14, 2015
1 parent 12183c0 commit 6e0edf6
Show file tree
Hide file tree
Showing 24 changed files with 1,077 additions and 71 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def getVersionName() {

// Basic project information
group = 'org.dragonet'
version = '0.0.1-preR11'
version = '0.0.1-beta'
description = "Dragonet"
mainClassName = "net.glowstone.GlowServer"

Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/dragonet/DragonetServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public DragonetServer(GlowServer server) {
*/
public void initialize() {
/* Uncomment following 3 lines when release */
//this.logger.info("Sending statistic... ");
//StatisticSender statSender = new StatisticSender(DragonetVersioning.DRAGONET_VERSION, System.currentTimeMillis());
//statSender.sendStatistic();
this.logger.info("Sending statistic... ");
StatisticSender statSender = new StatisticSender(DragonetVersioning.DRAGONET_VERSION, System.currentTimeMillis());
statSender.sendStatistic();
File fileConfig = new File(this.server.getConfigDir() + File.separator + "dragonet.yml");
if (!fileConfig.exists()) {
try {
Expand All @@ -78,6 +78,8 @@ public void initialize() {
}
}
Configuration config = YamlConfiguration.loadConfiguration(fileConfig);
this.logger.info("Current Minecraft PC Version: " + DragonetVersioning.MINECRAFT_PC_VERSION);
this.logger.info("Current Minecraft: Pocket Edition Version: " + DragonetVersioning.MINECRAFT_PE_VERSION);
this.threadPool = Executors.newFixedThreadPool(64);
String ip = config.getString("server-ip", "0.0.0.0");
int port = config.getInt("server-port", 19132);
Expand All @@ -92,8 +94,8 @@ public void initialize() {
public void tickUpdate() {
this.networkHandler.onTick();
}
public void shutdown(){

public void shutdown() {
this.logger.info("Stopping Dragonet server... ");
this.networkHandler.getUdp().end();
this.threadPool.shutdown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static EntityMetaData getMetaDataFromPlayer(GlowPlayer player) {
data.set(0, new ByteMeta(flags));
data.set(1, new ShortMeta((short) 0));
data.set(16, new ByteMeta((byte) 0x00));
data.set(17, new CoordinateMeta(0, 0, 0));
data.set(17, new CoordinateMeta(player.getLocation().getBlockX(), player.getLocation().getBlockY(), player.getLocation().getBlockZ()));
return data;
}
}
92 changes: 92 additions & 0 deletions src/main/java/org/dragonet/inventory/ItemList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* GNU LESSER GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*
* You can view LICENCE file for details.
*
* @author The Dragonet Team
*/

package org.dragonet.inventory;

import com.google.common.collect.Lists;
import java.util.ArrayList;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class ItemList {
private ArrayList<ItemStack> items;

public ItemList() {
this.items = new ArrayList<>();
}

public ItemList(ArrayList<ItemStack> items) {
this.items = items;
}

public ItemList(ItemStack[] items){
this.items = Lists.newArrayList(items);
}

public ItemList(Inventory inventory){
this();
for(int i = 0; i < inventory.getSize(); i++){
if(inventory.getItem(i) != null){
this.items.add(inventory.getItem(i));
}
}
}

public boolean tryToRemove(ItemStack item){
ArrayList<ItemStack> original = this.cloneList();
if(item == null || item.getTypeId() == 0) return true;
int toRemove = item.getAmount();
for(int i = 0; i < items.size(); i++){
if(toRemove == 0) break;
if(items.get(i) == null) continue;
int typeID = items.get(i).getTypeId();
int damage = items.get(i).getDurability();
int amount = items.get(i).getAmount();
if(typeID == item.getTypeId() && damage == item.getDurability()){
//We found the item
if(amount > toRemove){
//SUCCESS
items.get(i).setAmount(amount - toRemove);
return true;
}else{
items.set(i, null);
toRemove -= amount;
}
}
}
if(toRemove <= 0){
return true;
}else{
this.items = original;
return false;
}
}

private ArrayList<ItemStack> cloneList(){
ArrayList<ItemStack> cloned = new ArrayList<>();
for(ItemStack item : this.items){
cloned.add(item.clone());
}
return cloned;
}

public ArrayList<ItemStack> getItems() {
return items;
}

public ItemStack[] getContents(){
return this.items.toArray(new ItemStack[0]);
}


}
46 changes: 41 additions & 5 deletions src/main/java/org/dragonet/net/DragonetSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import net.glowstone.entity.meta.profile.PlayerProfile;
import net.glowstone.io.PlayerDataService;
import net.glowstone.net.GlowSession;
import net.glowstone.net.message.KickMessage;
import net.glowstone.net.message.play.game.UserListItemMessage;
import net.glowstone.net.protocol.ProtocolType;
import org.apache.commons.lang.ArrayUtils;
Expand All @@ -49,10 +48,14 @@
import org.bukkit.event.player.PlayerLoginEvent;
import org.dragonet.DragonetServer;
import org.dragonet.entity.DragonetPlayer;
import org.dragonet.inventory.InventoryType;
import org.dragonet.inventory.PEInventorySlot;
import org.dragonet.inventory.PEWindowConstantID;
import org.dragonet.net.packet.EncapsulatedPacket;
import org.dragonet.net.packet.RaknetDataPacket;
import org.dragonet.net.packet.minecraft.AdventureSettingsPacket;
import org.dragonet.net.packet.minecraft.ClientConnectPacket;
import org.dragonet.net.packet.minecraft.DisconnectPacket;
import org.dragonet.net.packet.minecraft.FullChunkPacket;
import org.dragonet.net.packet.minecraft.LoginPacket;
import org.dragonet.net.packet.minecraft.LoginStatusPacket;
Expand All @@ -65,6 +68,7 @@
import org.dragonet.net.packet.minecraft.SetSpawnPositionPacket;
import org.dragonet.net.packet.minecraft.SetTimePacket;
import org.dragonet.net.packet.minecraft.StartGamePacket;
import org.dragonet.net.packet.minecraft.WindowItemsPacket;
import org.dragonet.net.translator.BaseTranslator;
import org.dragonet.net.translator.TranslatorProvider;
import org.dragonet.utilities.io.PEBinaryReader;
Expand Down Expand Up @@ -157,7 +161,7 @@ public void onTick() {
}
this.chunkManager.onTick();
if (this.sentAndReceivedChunks >= 56 && (this.player instanceof Player)) {
this.getLogger().info("PE player [" + this.player.getName() + "] has spawned. ");
this.getLogger().info("PE player [" + this.player.getName() + "] has spawned. ");
this.sentAndReceivedChunks = -1;
this.sendSettings();
SetTimePacket pkTime = new SetTimePacket((int) (this.getPlayer().getWorld().getTime() & 0xFFFFFFFF), true);
Expand Down Expand Up @@ -373,8 +377,8 @@ public void send(PEPacket packet, int reliability) {
if (!(packet instanceof PEPacket)) {
return;
}
if(!(packet instanceof FullChunkPacket) && !(packet instanceof StartGamePacket) && !(packet instanceof SetTimePacket) && !(packet instanceof SetDifficultyPacket) &&
!(packet instanceof LoginStatusPacket) && !(packet instanceof ServerHandshakePacket) && this.sentAndReceivedChunks != -1){
if (!(packet instanceof FullChunkPacket) && !(packet instanceof StartGamePacket) && !(packet instanceof SetTimePacket) && !(packet instanceof SetDifficultyPacket)
&& !(packet instanceof LoginStatusPacket) && !(packet instanceof ServerHandshakePacket) && this.sentAndReceivedChunks != -1) {
this.queueAfterChunkSent.add(packet);
return;
}
Expand Down Expand Up @@ -725,7 +729,7 @@ public void disconnect(String reason, boolean overrideKick) {
GlowServer.logger.info("[" + this.remoteIP + ":" + this.remotePort + "] kicked: " + reason);
}

this.send(new KickMessage(reason));
this.send(new DisconnectPacket());
this.statusActive = false;
this.dServer.getNetworkHandler().removeSession(this);
this.getServer().getSessionRegistry().remove((GlowSession) this);
Expand All @@ -735,6 +739,33 @@ public void disconnect(String reason, boolean overrideKick) {
this.player = null;
}

public void sendInventory() {
if(this.getPlayer() == null) return;
if(this.getPlayer().getGameMode().equals(GameMode.CREATIVE)) return;
WindowItemsPacket pkItems = new WindowItemsPacket();
pkItems.windowID = PEWindowConstantID.PLAYER_INVENTORY;
pkItems.slots = new PEInventorySlot[InventoryType.SlotSize.PLAYER];
pkItems.hotbar = new int[9];
for (int i = 9; i <= 35; i++) {
if (this.getPlayer().getInventory().getContents()[i] != null) {
pkItems.slots[i - 9] = new PEInventorySlot((short) (this.getPlayer().getInventory().getContents()[i].getTypeId() & 0xFFFF), (byte) (this.getPlayer().getInventory().getContents()[i].getAmount() & 0xFF), this.getPlayer().getInventory().getContents()[i].getDurability());
} else {
pkItems.slots[i - 9] = new PEInventorySlot();
}
}
for (int i = 0; i <= 8; i++) {
if (this.getPlayer().getInventory().getContents()[i] != null) {
pkItems.slots[i + 27] = new PEInventorySlot((short) (this.getPlayer().getInventory().getContents()[i].getTypeId() & 0xFFFF), (byte) (this.getPlayer().getInventory().getContents()[i].getAmount() & 0xFF), this.getPlayer().getInventory().getContents()[i].getDurability());
} else {
pkItems.slots[i + 27] = new PEInventorySlot();
}
}
for (int i = 0; i <= 8; i++) {
pkItems.hotbar[i] = 44 - 8 + i;
}
this.send(pkItems);
}

@Override
public InetSocketAddress getAddress() {
return this.remoteInetSocketAddress;
Expand All @@ -758,4 +789,9 @@ public void setProtocol(ProtocolType protocol) {
//GlowProtocol proto = protocol.getProtocol();
//super.setProtocol(proto);
}

public SocketAddress getRemoteAddress() {
return remoteAddress;
}

}
14 changes: 9 additions & 5 deletions src/main/java/org/dragonet/net/NetworkHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,15 @@ public NetworkHandler(DragonetServer server, InetSocketAddress address) {
this.udp.start();
}

public synchronized void onTick() {
public void onTick() {
DatagramPacket packet = null;
while ((packet = this.udp.receive()) != null) {
this.processPacket(packet);
}
for (DragonetSession session : this.sessions.values()) {
session.onTick();
synchronized (this.sessions) {
for (DragonetSession session : this.sessions.values()) {
session.onTick();
}
}
}

Expand Down Expand Up @@ -141,7 +143,9 @@ public void send(byte[] buffer, SocketAddress remoteAddr) {
this.udp.send(buffer, remoteAddr);
}

public synchronized void removeSession(DragonetSession session) {
this.sessions.remove(session.getAddress().toString());
public void removeSession(DragonetSession session) {
synchronized (this.sessions) {
this.sessions.remove(session.getRemoteAddress().toString());
}
}
}
1 change: 0 additions & 1 deletion src/main/java/org/dragonet/net/NonBlockUDPSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public boolean send(byte[] buffer, SocketAddress addr) {
socket.send(new DatagramPacket(buffer, buffer.length, addr));
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/org/dragonet/net/packet/EncapsulatedPacket.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,13 @@ public static byte[] toBinary(EncapsulatedPacket packet) {
*
* @param session The DragonetSession context
* @param packet The PEPacket you want to encapsulate.
* @param reliability Packet Reliability
* @return Wrapped EncapsulatedPacket
*/
public static EncapsulatedPacket[] fromPEPacket(DragonetSession session, PEPacket packet, int reliability) {
if(session == null) return null;
if(packet == null) return null;
if(packet.getData() == null) return null;
packet.encode();
byte[] data = packet.getData();
if (data.length + 34 < session.getClientMTU()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* GNU LESSER GENERAL PUBLIC LICENSE
* Version 3, 29 June 2007
*
* Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*
* You can view LICENCE file for details.
*
* @author The Dragonet Team
*/

package org.dragonet.net.packet.minecraft;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.dragonet.inventory.PEInventorySlot;
import org.dragonet.utilities.io.PEBinaryWriter;

public class AddItemEntityPacket extends PEPacket {

public int eid;
public PEInventorySlot item;
public float x;
public float y;
public float z;
public byte yaw;
public byte pitch;
public byte roll;

@Override
public int pid() {
return PEPacketIDs.ADD_ITEM_ENTITY_PACKET;
}

@Override
public void encode() {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PEBinaryWriter writer = new PEBinaryWriter(bos);
writer.writeByte((byte) (this.pid() & 0xFF));
writer.writeInt(eid);
PEInventorySlot.writeSlot(writer, item);
writer.writeFloat(x);
writer.writeFloat(y);
writer.writeFloat(z);
writer.writeByte(yaw);
writer.writeByte(pitch);
writer.writeByte(roll);
this.setData(bos.toByteArray());
} catch (IOException e) {
}
}

@Override
public void decode() {
}

}
Loading

0 comments on commit 6e0edf6

Please sign in to comment.