Skip to content

Commit

Permalink
weather control, time contrl, and refactored parametre settings
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadron67 committed Aug 3, 2020
1 parent 3600e2d commit 4f22284
Show file tree
Hide file tree
Showing 20 changed files with 477 additions and 162 deletions.

This file was deleted.

63 changes: 1 addition & 62 deletions src/main/java/com/hadroncfy/sreplay/command/SReplayCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import com.google.gson.JsonParseException;
import com.hadroncfy.sreplay.SReplayMod;
import com.hadroncfy.sreplay.config.TextRenderer;
import com.hadroncfy.sreplay.recording.Photographer;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.BoolArgumentType;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
Expand Down Expand Up @@ -44,7 +42,6 @@ public class SReplayCommand {
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

private final ConfirmationManager cm = new ConfirmationManager(20000, 999);
private final Random rand = new Random();

public SReplayCommand(){
}
Expand All @@ -59,7 +56,7 @@ public void register(CommandDispatcher<ServerCommandSource> d) {
.then(literal("tp").executes(this::playerTp))
.then(literal("name").executes(this::getName)
.then(argument("fileName", StringArgumentType.word()).executes(this::setName)))
.then(buildPlayerParameterCommand())
.then(Photographer.PARAM_MANAGER.buildCommand())
.then(literal("pause").executes(this::pause))
.then(literal("resume").executes(this::resume))
.then(literal("marker").executes(this::getMarkers)
Expand Down Expand Up @@ -200,63 +197,6 @@ private int setName(CommandContext<ServerCommandSource> ctx){
return 0;
}

private static LiteralArgumentBuilder<ServerCommandSource> buildPlayerParameterCommand(){
return literal("set")
.then(new RecordParameterExecutor<Integer>("sizeLimit", IntegerArgumentType.integer(-1), int.class) {
@Override
protected int run(CommandContext<ServerCommandSource> ctx, Photographer p, Integer val) {
if (val == -1){
p.getRecordingParam().sizeLimit = -1;
}
else if (val < 10){
ctx.getSource().sendFeedback(SReplayMod.getFormats().sizeLimitTooSmall, true);
return 1;
}
else {
p.getRecordingParam().sizeLimit = ((long)val) << 20;
}
return 0;
}
}.build())
.then(new RecordParameterExecutor<Integer>("timeLimit", IntegerArgumentType.integer(-1), int.class) {
@Override
protected int run(CommandContext<ServerCommandSource> ctx, Photographer p, Integer val) {
if (val == -1){
p.getRecordingParam().timeLimit = -1;
}
else if (val < 10){
ctx.getSource().sendFeedback(SReplayMod.getFormats().timeLimitTooSmall, true);
}
else {
p.getRecordingParam().timeLimit = val * 1000;
}
return 0;
}

}.build())
.then(new RecordParameterExecutor<Boolean>("autoRestart", BoolArgumentType.bool(), boolean.class) {
@Override
protected int run(CommandContext<ServerCommandSource> ctx, Photographer p, Boolean val) {
p.getRecordingParam().autoReconnect = val;
return 0;
}
}.build())
.then(new RecordParameterExecutor<Boolean>("autoPause", BoolArgumentType.bool(), boolean.class) {
@Override
protected int run(CommandContext<ServerCommandSource> ctx, Photographer p, Boolean val) {
p.setAutoPause(val);
return 0;
}
}.build())
.then(new RecordParameterExecutor<Integer>("watchDistance", IntegerArgumentType.integer(0), int.class){
@Override
protected int run(CommandContext<ServerCommandSource> context, Photographer p, Integer val) {
p.setWatchDistance(val);
return 0;
}
}.build());
}

static Photographer requirePlayer(CommandContext<ServerCommandSource> ctx){
String name = StringArgumentType.getString(ctx, "player");
Photographer p = SReplayMod.getFake(ctx.getSource().getMinecraftServer(), name);
Expand Down Expand Up @@ -427,7 +367,6 @@ public int playerKill(CommandContext<ServerCommandSource> ctx) {
}

public int playerRespawn(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
final ServerCommandSource src = ctx.getSource();
final Photographer p = requirePlayer(ctx);
if (p != null) {
p.reconnect();
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/hadroncfy/sreplay/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public class Config {
public String serverHostName = "localhost";
public int serverPort = 12346;
public long downloadTimeout = 60000;
public long sizeLimit = -1;
public long timeLimit = -1;
public int sizeLimit = -1;
public int timeLimit = -1;
public boolean autoReconnect = true;
public Pattern playerNamePattern = Pattern.compile("^cam_.*$");

Expand Down
26 changes: 24 additions & 2 deletions src/main/java/com/hadroncfy/sreplay/config/Formats.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public class Formats {
private static Text red(String s){
return new LiteralText(s).setStyle(new Style().setColor(Formatting.RED));
}
private static Text green(String s){
return new LiteralText(s).setStyle(new Style().setColor(Formatting.GREEN));
}
private static Text white(String s){
return new LiteralText(s).setStyle(new Style().setColor(Formatting.WHITE));
}

public Text playerNotFound = red("[SReplay] 玩家$1未找到(或不是录像机器人)"),
recordFileExists = red("[SReplay] 录像文件$1已存在"),
reloadedConfig = new LiteralText("[SReplay] 已加载配置"),
Expand Down Expand Up @@ -89,7 +96,7 @@ private static Text red(String s){
serverStopped = new LiteralText("[SReplay] 下载服务器已停止"),
serverStopFailed = new LiteralText("[SReplay] 下载服务器停止失败:$1"),
downloadUrl = new LiteralText("[SReplay] 下载链接:")
.append(new LiteralText("$1").setStyle(new Style().setBold(true).setUnderline(true)
.append(new LiteralText("$1").setStyle(new Style().setUnderline(true)
.setClickEvent(new ClickEvent(Action.OPEN_URL, "$1"))
.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new LiteralText("点击以下载").setStyle(new Style().setColor(Formatting.GRAY).setItalic(true))
Expand All @@ -101,5 +108,20 @@ private static Text red(String s){
.append(new LiteralText("的"))
.append(new LiteralText("$3").setStyle(new Style().setColor(Formatting.GREEN)))
.append(new LiteralText("值设置为"))
.append(new LiteralText("$4").setStyle(new Style().setColor(Formatting.GREEN)));
.append(new LiteralText("$4").setStyle(new Style().setColor(Formatting.GREEN))),
getParam = new LiteralText("[SReplay]").append(
new LiteralText("$1").setStyle(new Style().setColor(Formatting.GREEN))
).append(new LiteralText("的")).append(
new LiteralText("$2").setStyle(new Style().setColor(Formatting.GREEN))
).append(new LiteralText("值为")).append(
new LiteralText("$3").setStyle(new Style().setColor(Formatting.GREEN))
),
positiveParam = red("[SReplay] 此参数须为正整数"),
nonNegativeOrMinusOne = red("[SReplay] 此参数须为-1或非负数"),
invalidEnum = red("[SReplay] 无效的值");

public Text[] help = new Text[]{
new LiteralText("====== SReplay 用法 ======").setStyle(new Style().setColor(Formatting.YELLOW)),
green("/sreplay ")
};
}
14 changes: 3 additions & 11 deletions src/main/java/com/hadroncfy/sreplay/mixin/MixinEntityTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,16 @@

import static com.hadroncfy.sreplay.recording.Photographer.getRealViewDistance;

import com.hadroncfy.sreplay.recording.Photographer;

@Mixin(targets = { "net.minecraft.server.world.ThreadedAnvilChunkStorage$EntityTracker" })
public class MixinEntityTracker {
@Shadow @Final
private Entity entity;

@Redirect(method = "updateCameraPosition(Lnet/minecraft/server/network/ServerPlayerEntity;)V", at = @At(
value = "INVOKE",
// XXX: AAAAAAhhhhhh!!! Why cannot I redirect ThreadedAnvilChunkStorage.watchDistance????!!!!!
target = "Ljava/lang/Math;min(II)I"
target = "Lnet/minecraft/server/world/ThreadedAnvilChunkStorage;method_18725(Lnet/minecraft/server/world/ThreadedAnvilChunkStorage;)I"
))
private int getViewDistance(int a, int b, ServerPlayerEntity player){
if (entity instanceof Photographer){
return Math.min(a, (((Photographer)entity).getRecordingParam().getWatchDistance() - 1) * 16);
}
else {
return Math.min(a, b);
}
private int getViewDistance(ThreadedAnvilChunkStorage cela, ServerPlayerEntity player){
return getRealViewDistance(player, ((ThreadedAnvilChunkStorageAccessor)cela).getWatchDistance());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.hadroncfy.sreplay.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.network.packet.s2c.play.WorldTimeUpdateS2CPacket;

@Mixin(WorldTimeUpdateS2CPacket.class)
public interface WorldTimeUpdateS2CPacketAccessor {
@Accessor("time")
long getTime();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.hadroncfy.sreplay.recording;

public enum ForcedWeather {
NONE, CLEAR, RAIN, THUNDER;
}
33 changes: 20 additions & 13 deletions src/main/java/com/hadroncfy/sreplay/recording/Photographer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.hadroncfy.sreplay.config.TextRenderer;
import com.hadroncfy.sreplay.interfaces.IChunkSender;
import com.hadroncfy.sreplay.mixin.PlayerManagerAccessor;
import com.hadroncfy.sreplay.recording.param.ParamManager;
import com.mojang.authlib.GameProfile;

import net.minecraft.entity.Entity;
Expand Down Expand Up @@ -44,6 +45,7 @@

public class Photographer extends ServerPlayerEntity implements ISizeLimitExceededListener {
public static final String MCPR = ".mcpr";
public static final ParamManager PARAM_MANAGER = new ParamManager(RecordingParam.class);
private static final String RAW_SUBDIR = "raw";
private static final GameMode MODE = GameMode.SPECTATOR;
private static final Logger LOGGER = LogManager.getLogger();
Expand All @@ -61,11 +63,13 @@ public class Photographer extends ServerPlayerEntity implements ISizeLimitExceed
private Recorder recorder;
private final File outputDir;
private final List<Entity> trackedPlayers = new ArrayList<>();
private int currentWatchDistance;

private String recordingFileName, saveFileName;

public Photographer(MinecraftServer server, ServerWorld world, GameProfile profile, ServerPlayerInteractionManager im, File outputDir, RecordingParam param){
super(server, world, profile, im);
currentWatchDistance = server.getPlayerManager().getViewDistance();
rparam = param;
this.outputDir = outputDir;
}
Expand Down Expand Up @@ -117,8 +121,7 @@ public void setSaveName(String name){
saveFileName = name;
}

public void setWatchDistance(int distance){
rparam.setWatchDistance(distance);
private void setWatchDistance(int distance){
recorder.onPacket(new ChunkLoadDistanceS2CPacket(distance));
int cx = MathHelper.floor(x) >> 4, cz = MathHelper.floor(z) >> 4;
ServerChunkManager chunkManager = (ServerChunkManager) world.getChunkManager();
Expand Down Expand Up @@ -150,13 +153,22 @@ private void connect() throws IOException{
removed = false;
trackedPlayers.clear();
server.getPlayerManager().onPlayerConnect(connection, this);
if (rparam.getWatchDistance() != server.getPlayerManager().getViewDistance()){
recorder.onPacket(new ChunkLoadDistanceS2CPacket(rparam.getWatchDistance()));
}
syncParams();
interactionManager.setGameMode(MODE);
getServerWorld().getChunkManager().updateCameraPosition(this);
}

public void syncParams(){
if (recorder != null){
recorder.syncParam();
updatePause();
if (currentWatchDistance != rparam.watchDistance){
setWatchDistance(rparam.watchDistance);
currentWatchDistance = rparam.watchDistance;
}
}
}

public void connect(String saveName) throws IOException {
reconnectCount = 0;
saveFileName = saveName;
Expand Down Expand Up @@ -207,11 +219,6 @@ public void onStoppedTracking(Entity entity) {
}
}

public void setAutoPause(boolean b){
rparam.autoPause = b;
updatePause();
}

private void updatePause(){
if (recorder != null && rparam.autoPause){
final String name = getGameProfile().getName();
Expand Down Expand Up @@ -257,8 +264,8 @@ public Text method_14206() {
size += "/" + String.format("%.2f", rparam.sizeLimit / 1024F / 1024F) + "M";
}
Text ret = new LiteralText(getGameProfile().getName()).setStyle(new Style().setItalic(true).setColor(Formatting.AQUA));
if (rparam.getWatchDistance() != server.getPlayerManager().getViewDistance()){
ret.append(new LiteralText(" (" + rparam.getWatchDistance() + ")").setStyle(new Style().setColor(Formatting.GRAY)));
if (rparam.watchDistance != server.getPlayerManager().getViewDistance()){
ret.append(new LiteralText(" (" + rparam.watchDistance + ")").setStyle(new Style().setColor(Formatting.GRAY)));
}
if (rparam.autoReconnect){
ret.append(new LiteralText(" [R]").setStyle(new Style().setItalic(false).setColor(Formatting.DARK_PURPLE)));
Expand Down Expand Up @@ -411,7 +418,7 @@ public static boolean checkForSaveFileDupe(MinecraftServer server, File outputDi

public static int getRealViewDistance(ServerPlayerEntity player, int watchDistance){
if (player instanceof Photographer){
return ((Photographer)player).getRecordingParam().getWatchDistance();
return ((Photographer)player).currentWatchDistance;
}
else {
return watchDistance;
Expand Down
Loading

0 comments on commit 4f22284

Please sign in to comment.