|
1 | 1 | package com.minecrafttas.tasmod.commands; |
2 | 2 |
|
| 3 | +import java.text.SimpleDateFormat; |
3 | 4 | import java.util.ArrayList; |
4 | 5 | import java.util.Arrays; |
5 | 6 | import java.util.List; |
| 7 | +import java.util.function.UnaryOperator; |
6 | 8 | import java.util.regex.Pattern; |
7 | 9 |
|
8 | 10 | import com.minecrafttas.tasmod.TASmod; |
| 11 | +import com.minecrafttas.tasmod.TASmodClient; |
9 | 12 | import com.minecrafttas.tasmod.networking.TASmodBufferBuilder; |
| 13 | +import com.minecrafttas.tasmod.registries.TASmodConfig; |
10 | 14 | import com.minecrafttas.tasmod.registries.TASmodPackets; |
11 | 15 | import com.minecrafttas.tasmod.savestates.SavestateHandlerServer.SavestateCallback; |
| 16 | +import com.minecrafttas.tasmod.savestates.SavestateIndexer.FailedSavestate; |
12 | 17 | import com.minecrafttas.tasmod.savestates.SavestateIndexer.Savestate; |
13 | 18 | import com.minecrafttas.tasmod.savestates.exceptions.LoadstateException; |
14 | 19 | import com.minecrafttas.tasmod.savestates.exceptions.SavestateDeleteException; |
15 | 20 | import com.minecrafttas.tasmod.savestates.exceptions.SavestateException; |
| 21 | +import com.minecrafttas.tasmod.util.Component; |
| 22 | +import com.minecrafttas.tasmod.util.Component.CClickEvent; |
| 23 | +import com.minecrafttas.tasmod.util.Component.CHoverEvent; |
| 24 | +import com.minecrafttas.tasmod.util.I18n; |
16 | 25 | import com.minecrafttas.tasmod.util.LoggerMarkers; |
17 | 26 |
|
18 | 27 | import net.minecraft.command.CommandBase; |
|
22 | 31 | import net.minecraft.entity.player.EntityPlayerMP; |
23 | 32 | import net.minecraft.server.MinecraftServer; |
24 | 33 | import net.minecraft.util.math.BlockPos; |
| 34 | +import net.minecraft.util.text.Style; |
25 | 35 | import net.minecraft.util.text.TextComponentString; |
| 36 | +import net.minecraft.util.text.TextFormatting; |
| 37 | +import net.minecraft.util.text.event.ClickEvent; |
| 38 | +import net.minecraft.util.text.event.HoverEvent; |
26 | 39 |
|
27 | 40 | public class CommandSavestate extends CommandBase { |
28 | 41 |
|
| 42 | + public static boolean once = true; |
| 43 | + |
29 | 44 | @Override |
30 | 45 | public String getName() { |
31 | 46 | return "savestate"; |
@@ -264,15 +279,129 @@ else if ("info".equals(first)) { |
264 | 279 |
|
265 | 280 | private void info(ICommandSender sender) { |
266 | 281 | TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command Info"); |
267 | | - |
| 282 | + infoIndexAmount(sender, null, null); |
268 | 283 | } |
269 | 284 |
|
270 | | - private void infoIndex(ICommandSender sender, int index) { |
| 285 | + private void infoIndex(ICommandSender sender, Integer index) { |
271 | 286 | TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command InfoIndex {}", index); |
| 287 | + infoIndexAmount(sender, index, null); |
272 | 288 | } |
273 | 289 |
|
274 | | - private void infoIndexAmount(ICommandSender sender, int index, int amount) { |
275 | | - TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command InfoIndexAmount {}|{}", index, amount); |
| 290 | + private void infoIndexAmount(ICommandSender sender, Integer indexToDisplay, Integer amount) { |
| 291 | + TASmod.LOGGER.trace(LoggerMarkers.Savestate, "Command InfoIndexAmount {}|{}", indexToDisplay, amount); |
| 292 | + |
| 293 | + int currentIndex = TASmod.savestateHandlerServer.getCurrentIndex(); |
| 294 | + int size = TASmod.savestateHandlerServer.size(); |
| 295 | + if (indexToDisplay == null) { |
| 296 | + indexToDisplay = currentIndex; |
| 297 | + } |
| 298 | + if (amount == null) { |
| 299 | + amount = 10; |
| 300 | + } |
| 301 | + |
| 302 | + sender.sendMessage(Component.literal("").build()); // Print an empty line |
| 303 | + |
| 304 | + String format = I18n.format("msg.tasmod.savestate.dateformat"); |
| 305 | + SimpleDateFormat dateFormat = new SimpleDateFormat(format); |
| 306 | + |
| 307 | + List<Savestate> savestateList = TASmod.savestateHandlerServer.getSavestateInfo(indexToDisplay, amount); |
| 308 | + |
| 309 | + if (savestateList.size() < size && once) { |
| 310 | + sender.sendMessage(Component.translatable("gui.tasmod.savestate.omitted", "/savestate info all").withStyle(TextFormatting.RED, TextFormatting.ITALIC).build()); |
| 311 | + once = false; |
| 312 | + } |
| 313 | + |
| 314 | + for (Savestate savestate : savestateList) { |
| 315 | + |
| 316 | + String index = savestate.getIndex() == null ? "" : Integer.toString(savestate.getIndex()); |
| 317 | + boolean isCurrentIndex = savestate.getIndex() == currentIndex; |
| 318 | + String name = savestate.getName() == null ? "" : savestate.getName(); |
| 319 | + String date = savestate.getDate() == null ? "" : dateFormat.format(savestate.getDate()); |
| 320 | + |
| 321 | + TextFormatting indexColor = isCurrentIndex ? TextFormatting.AQUA : TextFormatting.BLUE; |
| 322 | + TextFormatting nameColor = isCurrentIndex ? TextFormatting.WHITE : TextFormatting.GRAY; |
| 323 | + TextFormatting dateColor = isCurrentIndex ? TextFormatting.AQUA : TextFormatting.DARK_AQUA; |
| 324 | + TextFormatting saveColor = isCurrentIndex ? TextFormatting.LIGHT_PURPLE : TextFormatting.DARK_PURPLE; |
| 325 | + TextFormatting deleteColor = isCurrentIndex ? TextFormatting.RED : TextFormatting.DARK_RED; |
| 326 | + TextFormatting renameColor = isCurrentIndex ? TextFormatting.YELLOW : TextFormatting.GOLD; |
| 327 | + TextFormatting loadColor = isCurrentIndex ? TextFormatting.GREEN : TextFormatting.DARK_GREEN; |
| 328 | + |
| 329 | + //@formatter:off |
| 330 | + UnaryOperator<Style> hover = t -> |
| 331 | + t.setHoverEvent ( |
| 332 | + CHoverEvent.create(HoverEvent.Action.SHOW_TEXT, Component.literal(date).withStyle(dateColor)) |
| 333 | + ); |
| 334 | + |
| 335 | + Component msg = null; |
| 336 | + |
| 337 | + if(savestate instanceof FailedSavestate) { |
| 338 | + FailedSavestate failedSavestate = (FailedSavestate) savestate; |
| 339 | + msg = Component.translatable("%s: %s%s", |
| 340 | + Component.literal(index).withStyle(indexColor), |
| 341 | + Component.literal(name).withStyle(nameColor), |
| 342 | + Component.translatable("msg.tasmod.savestate.info.error", failedSavestate.getError().getMessage()) |
| 343 | + .withStyle(TextFormatting.RED)) |
| 344 | + .withStyle(t -> |
| 345 | + t.setHoverEvent( |
| 346 | + CHoverEvent.create(HoverEvent.Action.SHOW_TEXT, Component.literal(date).withStyle(TextFormatting.GOLD) |
| 347 | + ))); |
| 348 | + } else { |
| 349 | + if(!TASmodClient.config.getBoolean(TASmodConfig.SAVESTATE_SHOW_CONTROLS)) { |
| 350 | + msg = Component.translatable("%s: %s", |
| 351 | + Component.literal(index).withStyle(indexColor), |
| 352 | + Component.literal(name).withStyle(nameColor)) |
| 353 | + .withStyle(hover); |
| 354 | + |
| 355 | + } |
| 356 | + else { |
| 357 | + Component saveComponent = Component.translatable("msg.tasmod.savestate.save.clickable").withStyle(saveColor) |
| 358 | + .withStyle(t-> |
| 359 | + t.setHoverEvent( |
| 360 | + CHoverEvent.create(HoverEvent.Action.SHOW_TEXT, Component.translatable("msg.tasmod.savestate.save.hover", name).withStyle(saveColor))) |
| 361 | + ) |
| 362 | + .withStyle(t-> |
| 363 | + t.setClickEvent( |
| 364 | + CClickEvent.create(ClickEvent.Action.SUGGEST_COMMAND, String.format("/savestate save %s", index))) |
| 365 | + ); |
| 366 | + |
| 367 | + Component deleteComponent = Component.translatable("msg.tasmod.savestate.delete.clickable").withStyle(deleteColor) |
| 368 | + .withStyle(t-> |
| 369 | + t.setClickEvent(CClickEvent.create(ClickEvent.Action.SUGGEST_COMMAND, String.format("/savestate delete %s", index))) |
| 370 | + ) |
| 371 | + .withStyle(t-> |
| 372 | + t.setHoverEvent(CHoverEvent.create(HoverEvent.Action.SHOW_TEXT, Component.translatable("msg.tasmod.savestate.delete.hover", name).withStyle(deleteColor))) |
| 373 | + ); |
| 374 | + |
| 375 | + Component renameComponent = Component.translatable("msg.tasmod.savestate.rename.clickable").withStyle(renameColor) |
| 376 | + .withStyle(t-> |
| 377 | + t.setClickEvent(CClickEvent.create(ClickEvent.Action.SUGGEST_COMMAND, String.format("/savestate rename %s", index))) |
| 378 | + ) |
| 379 | + .withStyle(t-> |
| 380 | + t.setHoverEvent(CHoverEvent.create(HoverEvent.Action.SHOW_TEXT, Component.translatable("msg.tasmod.savestate.rename.hover", name).withStyle(renameColor))) |
| 381 | + ); |
| 382 | + |
| 383 | + Component loadComponent = Component.translatable("msg.tasmod.savestate.load.clickable").withStyle(loadColor) |
| 384 | + .withStyle(t-> |
| 385 | + t.setClickEvent(CClickEvent.create(ClickEvent.Action.SUGGEST_COMMAND, String.format("/savestate load %s", index))) |
| 386 | + ) |
| 387 | + .withStyle(t-> |
| 388 | + t.setHoverEvent(CHoverEvent.create(HoverEvent.Action.SHOW_TEXT, Component.translatable("msg.tasmod.savestate.load.hover", name).withStyle(loadColor))) |
| 389 | + ); |
| 390 | + |
| 391 | + msg = Component.translatable("%s: %s %s %s %s %s", |
| 392 | + Component.literal(index).withStyle(indexColor), |
| 393 | + Component.literal(name).withStyle(nameColor), |
| 394 | + Component.wrap(saveComponent, nameColor), |
| 395 | + Component.wrap(deleteComponent, nameColor), |
| 396 | + Component.wrap(renameComponent, nameColor), |
| 397 | + Component.wrap(loadComponent, nameColor) |
| 398 | + ).withStyle(hover); |
| 399 | + } |
| 400 | + } |
| 401 | + |
| 402 | + //@formatter:on |
| 403 | + sender.sendMessage(msg.build()); |
| 404 | + } |
276 | 405 | } |
277 | 406 |
|
278 | 407 | private void infoAll(ICommandSender sender) { |
|
0 commit comments