Skip to content

Commit

Permalink
Particles WORKS ! OMG!!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Snake1999 committed Nov 21, 2015
1 parent c03816a commit 85cef24
Show file tree
Hide file tree
Showing 11 changed files with 297 additions and 689 deletions.
675 changes: 0 additions & 675 deletions server/LICENSE

This file was deleted.

8 changes: 4 additions & 4 deletions server/nukkit-core/src/main/java/cn/nukkit/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -1789,10 +1789,10 @@ public void handleDataPacket(DataPacket packet) {
break;
}

if (loginPacket.skin.length != 64 * 32 * 4 && loginPacket.skin.length != 64 * 64 * 4) {
this.close("", "disconnectionScreen.invalidSkin");
break;
}
//if (loginPacket.skin.length != 64 * 32 * 4 && loginPacket.skin.length != 64 * 64 * 4) {
// this.close("", "disconnectionScreen.invalidSkin");
// break;
//}

this.setSkin(loginPacket.skin, loginPacket.slim);

Expand Down
12 changes: 10 additions & 2 deletions server/nukkit-core/src/main/java/cn/nukkit/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ public void batchPackets(Player[] players, DataPacket[] packets) {
}

public void batchPackets(Player[] players, DataPacket[] packets, boolean forceSync) {
this.batchPackets(players, packets, forceSync, 0);
}

public void batchPackets(Player[] players, DataPacket[] packets, boolean forceSync, int channel) {
byte[][] payload = new byte[packets.length * 2][];
for (int i = 0; i < packets.length; i++) {
DataPacket p = packets[i];
Expand All @@ -501,14 +505,18 @@ public void batchPackets(Player[] players, DataPacket[] packets, boolean forceSy
payload[i * 2] = Binary.writeInt(buf.length);
payload[i * 2 + 1] = buf;
}
this.batchPackets(players, payload, forceSync);
this.batchPackets(players, payload, forceSync, channel);
}

public void batchPackets(Player[] players, byte[][] payload) {
this.batchPackets(players, payload, false);
}

public void batchPackets(Player[] players, byte[][] payload, boolean forceSync) {
this.batchPackets(players, payload, forceSync, 0);
}

public void batchPackets(Player[] players, byte[][] payload, boolean forceSync, int channel) {
byte[] data = new byte[0];
data = Binary.appendBytes(data, payload);
List<String> targets = new ArrayList<>();
Expand All @@ -519,7 +527,7 @@ public void batchPackets(Player[] players, byte[][] payload, boolean forceSync)
}

if (!forceSync && this.networkCompressionAsync) {
this.getScheduler().scheduleAsyncTask(new CompressBatchedTask(data, targets, this.networkCompressionLevel));
this.getScheduler().scheduleAsyncTask(new CompressBatchedTask(data, targets, this.networkCompressionLevel, channel));
} else {
try {
this.broadcastPacketsCallback(Zlib.deflate(data, this.networkCompressionLevel), targets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void setDefaultCommands() {
//this.register("nukkit", new GiveCommand("give"));
//this.register("nukkit", new EffectCommand("effect"));
//this.register("nukkit", new EnchantCommand("enchant"));
//this.register("nukkit", new ParticleCommand("particle"));
this.register("nukkit", new ParticleCommand("particle"));
this.register("nukkit", new GamemodeCommand("gamemode"));
//this.register("nukkit", new KillCommand("kill"));
//this.register("nukkit", new SpawnpointCommand("spawnpoint"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package cn.nukkit.command.defaults;

import cn.nukkit.Player;
import cn.nukkit.command.CommandSender;
import cn.nukkit.event.TranslationContainer;
import cn.nukkit.level.Level;
import cn.nukkit.level.particle.ExplodeParticle;
import cn.nukkit.level.particle.Particle;
import cn.nukkit.math.Vector3;
import cn.nukkit.utils.TextFormat;

import java.util.regex.Pattern;

/**
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.command.defaults in project Nukkit .
*/
public class ParticleCommand extends VanillaCommand {
public ParticleCommand(String name) {
super(name, "%nukkit.command.particle.description", "%nukkit.command.particle.usage");
this.setPermission("nukkit.command.particle");
}

private static class ParticleCommandRequest{

private enum RequestType{OK, ILLEGAL_ARGUMENTS, UNKNOWN_PARTICLE, INVALID_POSITION}

private RequestType requestType;

private Particle particle;

private double xd;
private double yd;
private double zd;

private int count;

public RequestType getRequestType() {
return requestType;
}

public Particle getParticle() {
return particle;
}

public double getXd() {
return xd;
}

public double getYd() {
return yd;
}

public double getZd() {
return zd;
}

public int getCount() {
return count;
}

private ParticleCommandRequest(RequestType requestType){
this(requestType, null, 0, 0, 0, 0);
}

private ParticleCommandRequest(RequestType requestType, Particle particle, double xd, double yd, double zd, int count){
this.requestType = requestType;
this.particle = particle;
this.xd = xd;
this.yd = yd;
this.zd = zd;
this.count = count;
}

private static Particle getParticleFromString(String string, Vector3 pos, int data){
string = string.toLowerCase().trim();
switch (string){
case "explode":
return new ExplodeParticle(pos);
//todo a lot
default:
return null;
}
}

private static boolean isParticleExist(String string){
return getParticleFromString(string, new Vector3(0, 0, 0), 0) != null;
}

//nukkit.command.particle.usage=/particle <粒子名称> <x> <y> <z> <xd> <yd> <zd> [数量] [数据值]
static ParticleCommandRequest of(String[] args){
if (args.length < 7 || args.length > 9){
return new ParticleCommandRequest(RequestType.ILLEGAL_ARGUMENTS);
} else if (!isAllNumeric(args[1], args[2], args[3], args[4], args[5], args[6])){
return new ParticleCommandRequest(RequestType.ILLEGAL_ARGUMENTS);
} else if (!isParticleExist(args[0])){
return new ParticleCommandRequest(RequestType.UNKNOWN_PARTICLE);
} else {
Vector3 pos = new Vector3(
Double.parseDouble(args[1]),
Double.parseDouble(args[2]),
Double.parseDouble(args[3])
);
double xd = Double.parseDouble(args[4]);
double yd = Double.parseDouble(args[5]);
double zd = Double.parseDouble(args[6]);

int count = 1;
int data = 0;
if (args.length == 8){
if(isAllInteger(args[7])) count = Integer.parseInt(args[7]);
} else if (args.length == 9){
if(isAllInteger(args[7], args[8])) {
count = Integer.parseInt(args[7]);
data = Integer.parseInt(args[8]);
}
} else if(args.length != 7){
return new ParticleCommandRequest(RequestType.ILLEGAL_ARGUMENTS);
}
Particle particle = getParticleFromString(args[0], pos, data);
return new ParticleCommandRequest(RequestType.OK, particle, xd, yd, zd, count);
}
}

static boolean isAllNumeric(String... strings){
final String regDouble = "^[-\\+]?\\d+(\\.\\d+)?$";
Pattern p = Pattern.compile(regDouble);
boolean result = true;
for (String string : strings) {
if (!p.matcher(string).matches()) result = false;
}
return result;
}

static boolean isAllInteger(String... strings){
final String regDouble = "^[-\\+]?[\\d]*$";
Pattern p = Pattern.compile(regDouble);
boolean result = true;
for (String string : strings) {
if (!p.matcher(string).matches()) result = false;
}
return result;
}
}

@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) {
return true;
}
ParticleCommandRequest request = ParticleCommandRequest.of(args);
switch (request.getRequestType()){
case INVALID_POSITION:
case ILLEGAL_ARGUMENTS:
sender.sendMessage(new TranslationContainer("commands.generic.usage", this.usageMessage));
break;
case UNKNOWN_PARTICLE:
sender.sendMessage(new TranslationContainer(TextFormat.RED + "%commands.particle.notFound", args[0]));
break;
case OK:
Particle p = request.getParticle();
Level level = sender.getServer().getDefaultLevel();
if(sender instanceof Player){
level = ((Player) sender).getLevel();
}
for (int i = 0; i < request.getCount(); i++) {
p.setComponents(
p.getX() + Math.random() * request.getXd(),
p.getY() + Math.random() * request.getYd(),
p.getZ() + Math.random() * request.getZd()
);

level.addParticle(p);
}
String countString = String.valueOf(request.getCount());
sender.sendMessage(new TranslationContainer("commands.particle.success", new String[]{args[0], countString}));
break;
}
return true;
}
}
24 changes: 21 additions & 3 deletions server/nukkit-core/src/main/java/cn/nukkit/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import cn.nukkit.level.format.generic.BaseLevelProvider;
import cn.nukkit.level.format.generic.EmptyChunkSection;
import cn.nukkit.level.generator.*;
import cn.nukkit.level.particle.Particle;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.NukkitMath;
import cn.nukkit.math.Vector2;
Expand Down Expand Up @@ -315,8 +316,25 @@ public void addSound() {
//todo
}

public void addParticle() {
//todo
public void addParticle(Particle particle) {
Objects.requireNonNull(particle);
List<DataPacket> pks = particle.encode();
Objects.requireNonNull(pks);
pks.forEach((pk)->{
this.addChunkPacket(particle.getFloorX() >> 4, particle.getFloorZ() >> 4, pk);
});
}

public void addParticle(Particle particle, Player player) {
this.addParticle(particle, new Player[]{player});
}

public void addParticle(Particle particle, Player[] players) {
Objects.requireNonNull(particle);
List<DataPacket> pks = particle.encode();
Objects.requireNonNull(pks);

this.server.batchPackets(players, pks.toArray(new DataPacket[]{}));
}

public boolean getAutoSave() {
Expand Down Expand Up @@ -1308,7 +1326,7 @@ public Item useBreakOn(Vector3 vector, Item item, Player player, boolean createP
}

//todo particle
this.addParticle();
//this.addParticle();
}

target.onBreak(item);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cn.nukkit.level.particle;

import cn.nukkit.math.Vector3;

/**
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.particle in project Nukkit .
*/
public class ExplodeParticle extends GenericParticle {
public ExplodeParticle(Vector3 pos) {
super(pos, 4);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cn.nukkit.level.particle;

import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.DataPacket;
import cn.nukkit.network.protocol.LevelEventPacket;

import java.util.ArrayList;
import java.util.List;

/**
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.particle in project Nukkit .
*/
public class GenericParticle extends Particle {

protected int id = 0;

protected int data;

public GenericParticle(Vector3 pos, int id){
this(pos, id, 0);
}

public GenericParticle(Vector3 pos, int id, int data){
super(pos);
this.id = id;
this.data = data;
}

@Override
public List<DataPacket> encode() {
LevelEventPacket packet = new LevelEventPacket();
packet.evid = 0x4000 | this.id;
packet.x = this.getFloorX();
packet.y = this.getFloorY();
packet.z = this.getFloorZ();
packet.data = this.data;
packet.encode();

List<DataPacket> pks = new ArrayList<>();
pks.add(packet);
return pks;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cn.nukkit.level.particle;

import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.DataPacket;

import java.util.List;

/**
* Created on 2015/11/21 by xtypr.
* Package cn.nukkit.level.particle in project Nukkit .
*/
public abstract class Particle extends Vector3 {

public Particle(Vector3 pos){
super(pos.getX(), pos.getY(), pos.getZ());
}

public abstract List<DataPacket> encode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface ProtocolInfo {
/**
* Actual Minecraft: PE protocol version
*/
byte CURRENT_PROTOCOL = 34;
byte CURRENT_PROTOCOL = 38;
byte LOGIN_PACKET = (byte) 0x8f;
byte PLAY_STATUS_PACKET = (byte) 0x90;
byte DISCONNECT_PACKET = (byte) 0x91;
Expand Down
6 changes: 3 additions & 3 deletions server/nukkit-core/src/main/resources/lang/chs.ini
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ commands.enchant.notFound=ID为 {%0} 的附魔不存在
commands.enchant.success=附魔完成
commands.enchant.usage=/enchant <玩家名称> <附魔ID> [物品等级]

commands.particle.success=正在使用 {%0} 效果 {%1}
commands.particle.notFound=未知的效果名称 {%0}
commands.particle.success=已释放 {%0} 粒子 {%1}
commands.particle.notFound=未知的粒子名称 {%0}

commands.players.usage=/list
commands.players.list=目前有 {%0}/{%1} 个玩家在线:
Expand Down Expand Up @@ -254,7 +254,7 @@ nukkit.command.kill.description=自杀或杀死其他玩家
nukkit.command.kill.usage=/kill [玩家名称]

nukkit.command.particle.description=加入粒子效果至世界
nukkit.command.particle.usage=/particle <玩家名称> <x> <y> <z> <xd> <yd> <zd> [数量] [数据值]
nukkit.command.particle.usage=/particle <粒子名称> <x> <y> <z> <xd> <yd> <zd> [数量] [数据值]

nukkit.command.time.description=更改每个世界的时间
nukkit.command.time.usage=/time <set|add> <数值> 或 /time <start|stop|query>
Expand Down

0 comments on commit 85cef24

Please sign in to comment.