Skip to content

Commit

Permalink
feat: time command
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Mar 23, 2024
1 parent 3d4466b commit 503dff4
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ default CommandNode intNum(String name) {
return intNum(name, 0);
}

default CommandNode longNum(String name, long defaultValue) {
return addLeaf(getFactory().longNum(name, this, defaultValue));
}

default CommandNode longNum(String name) {
return longNum(name, 0);
}

default CommandNode floatNum(String name, float defaultValue) {
return addLeaf(getFactory().floatNum(name, this, defaultValue));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static CommandNodeFactory getFactory() {

CommandNode intNum(String name, CommandNode parent, int defaultValue);

CommandNode longNum(String name, CommandNode parent, long defaultValue);

CommandNode floatNum(String name, CommandNode parent, float defaultValue);

CommandNode doubleNum(String name, CommandNode parent, double defaultValue);
Expand Down
9 changes: 6 additions & 3 deletions Allay-API/src/main/java/org/allaymc/api/world/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,14 @@ default void sendTime(Collection<EntityPlayer> players) {
}
}

@ApiStatus.Internal
void sendTime(EntityPlayer player);
void setTime(long time);

default void addTime(long amount) {
setTime(getWorldData().getTime() + amount);
}

@ApiStatus.Internal
void tickTime(long tickNumber);
void sendTime(EntityPlayer player);

void setDimension(Dimension dimension);

Expand Down
19 changes: 18 additions & 1 deletion Allay-API/src/main/java/org/allaymc/api/world/WorldData.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,33 @@ public synchronized long getTime() {

public synchronized void setTime(long time) {
this.time = time;
rollbackTime();
}

public synchronized void addTime() {
this.time++;
rollbackTime();
}

public synchronized void addTime(int value) {
public synchronized void addTime(long value) {
this.time += value;
rollbackTime();
}

protected void rollbackTime() {
if (this.time > TIME_FULL) {
this.time = 0;
}
}

public static final long TIME_DAY = 0;
public static final long TIME_NOON = 6000;
public static final long TIME_SUNSET = 12000;
public static final long TIME_NIGHT = 14000;
public static final long TIME_MIDNIGHT = 18000;
public static final long TIME_SUNRISE = 23000;
public static final long TIME_FULL = 24000;

public synchronized void setCurrentTick(long currentTick) {
this.currentTick = currentTick;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public void registerDefaultCommands() {
register(new UnbanIPCommand());
register(new WhitelistCommand());
register(new ScoreboardCommand());
register(new TimeCommand());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package org.allaymc.server.command.defaults;

import org.allaymc.api.command.SimpleCommand;
import org.allaymc.api.command.tree.CommandTree;
import org.allaymc.api.i18n.TrKeys;
import org.allaymc.api.world.WorldData;

/**
* Allay Project 2024/3/23
*
* @author daoge_cmd
*/
public class TimeCommand extends SimpleCommand {
public TimeCommand() {
super("time", TrKeys.M_COMMANDS_TIME_DESCRIPTION);
}

@Override
public void prepareCommandTree(CommandTree tree) {
tree.getRoot()
.key("set")
.enums("time", "day", "night", "midnight", "noon", "sunrise", "sunset")
.exec(context -> {
var world = context.getSender().getCmdExecuteLocation().dimension().getWorld();
long time = 0L;
switch ((String) context.getSecondResult()) {
case "day" -> {
time = WorldData.TIME_DAY;
}
case "night" -> {
time = WorldData.TIME_NIGHT;
}
case "midnight" -> {
time = WorldData.TIME_MIDNIGHT;
}
case "noon" -> {
time = WorldData.TIME_NOON;
}
case "sunrise" -> {
time = WorldData.TIME_SUNRISE;
}
case "sunset" -> {
time = WorldData.TIME_SUNSET;
}
}
world.setTime(time);
context.addOutput(TrKeys.M_COMMANDS_TIME_SET, time);
return context.success();
})
.up()
.longNum("amount")
.exec(context -> {
var world = context.getSender().getCmdExecuteLocation().dimension().getWorld();
long time = context.getSecondResult();
world.setTime(time);
context.addOutput(TrKeys.M_COMMANDS_TIME_SET, time);
return context.success();
})
.root()
.key("add")
.longNum("amount")
.exec(context -> {
var world = context.getSender().getCmdExecuteLocation().dimension().getWorld();
long amount = context.getSecondResult();
world.addTime(amount);
context.addOutput(TrKeys.M_COMMANDS_TIME_ADDED, amount);
return context.success();
});

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public CommandNode intNum(String name, CommandNode parent, int defaultValue) {
return new IntNode(name, parent, defaultValue);
}

@Override
public CommandNode longNum(String name, CommandNode parent, long defaultValue) {
return new LongNode(name, parent, defaultValue);
}

@Override
public CommandNode floatNum(String name, CommandNode parent, float defaultValue) {
return new FloatNode(name, parent, defaultValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package org.allaymc.server.command.tree.node;

import org.allaymc.api.command.tree.BaseNode;
import org.allaymc.api.command.tree.CommandContext;
import org.allaymc.api.command.tree.CommandNode;
import org.cloudburstmc.protocol.bedrock.data.command.CommandParam;
import org.cloudburstmc.protocol.bedrock.data.command.CommandParamData;

/**
* Allay Project 2024/3/23
*
* @author daoge_cmd
*/
public class LongNode extends BaseNode {
public LongNode(String name, CommandNode parent, long defaultValue) {
super(name, parent, defaultValue);
}

@Override
public boolean match(CommandContext context) {
var arg = context.queryArg();
var number = 0L;
try {
number = Long.parseLong(arg);
} catch (NumberFormatException e) {
return false;
}
context.putResult(number);
context.popArg();
return true;
}

@Override
public CommandParamData toNetworkData() {
var data = super.toNetworkData();
data.setType(CommandParam.INT);
return data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected record PacketQueueEntry(EntityPlayer player, BedrockPacket packet) {
protected final Thread thread;
protected final Thread networkThread;
protected long nextTimeSendTick;
public static final int TIME_SENDING_INTERVAL = 12 * 20;
public static final long TIME_SENDING_INTERVAL = 12 * 20;
public static final int MAX_PACKETS_HANDLE_COUNT_AT_ONCE = Server.SETTINGS.networkSettings().maxSyncedPacketsHandleCountAtOnce();

public AllayWorld(WorldStorage worldStorage) {
Expand Down Expand Up @@ -183,8 +183,7 @@ public void startTick() {
}
}

@Override
public void tickTime(long tickNumber) {
protected void tickTime(long tickNumber) {
if (worldData.getGameRule(GameRule.DO_DAYLIGHT_CYCLE)) {
if (tickNumber >= nextTimeSendTick) {
worldData.addTime(TIME_SENDING_INTERVAL);
Expand All @@ -194,6 +193,12 @@ public void tickTime(long tickNumber) {
}
}

@Override
public void setTime(long time) {
worldData.setTime(time);
sendTime(getPlayers());
}

@Override
public void sendTime(EntityPlayer player) {
var setTimePk = new SetTimePacket();
Expand Down

0 comments on commit 503dff4

Please sign in to comment.