-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
297 additions
and
689 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
181 changes: 181 additions & 0 deletions
181
server/nukkit-core/src/main/java/cn/nukkit/command/defaults/ParticleCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
server/nukkit-core/src/main/java/cn/nukkit/level/particle/ExplodeParticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
server/nukkit-core/src/main/java/cn/nukkit/level/particle/GenericParticle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
server/nukkit-core/src/main/java/cn/nukkit/level/particle/Particle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters