Sonickat

DebugQueueEvent.JAVA

Feb 1st, 2025
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | Gaming | 0 0
  1. package net.sonickat.sklib.common;
  2.  
  3. import java.time.Instant;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. public class DebugQueueEvent {
  8.     private final String message;
  9.     private final String timestamp;
  10.     private final CRef affectedBlock;
  11.     private final List<CRef> callStack; // Tracks full chain of triggers
  12.  
  13.     public DebugQueueEvent(String message, CRef affectedBlock, List<CRef> callStack) {
  14.         this.timestamp = Instant.now().toString();
  15.         this.message = message;
  16.         this.affectedBlock = affectedBlock;
  17.  
  18.         // Copy call stack + add this block
  19.         this.callStack = new ArrayList<>(callStack);
  20.         this.callStack.add(affectedBlock);
  21.     }
  22.  
  23.     public String getMessage() {
  24.         return message;
  25.     }
  26.  
  27.     public String getTimestamp() {
  28.         return timestamp;
  29.     }
  30.  
  31.     public CRef getAffectedBlock() {
  32.         return affectedBlock;
  33.     }
  34.  
  35.     public List<CRef> getCallStack() {
  36.         return callStack;
  37.     }
  38.  
  39.     /**
  40.      * Formats the call stack for console output, limiting excessive length.
  41.      */
  42.     private String formatCallStack() {
  43.         int size = callStack.size();
  44.         int maxLength = DebugQueue.MAX_CONSOLE_CHAIN_LENGTH; // Controlled by DebugQueue
  45.  
  46.         if (size <= maxLength) {
  47.             return callStack.toString(); // Show full list if small
  48.         }
  49.  
  50.         // Show first, '...', last few elements (e.g., [..., B, C, D])
  51.         List<CRef> reducedStack = new ArrayList<>();
  52.         reducedStack.add(callStack.get(0)); // Always include the origin
  53.         reducedStack.add(new CRef(0, 0, 0)); // Placeholder for "..."
  54.         reducedStack.addAll(callStack.subList(size - (maxLength - 1), size));
  55.  
  56.         return reducedStack.toString().replace(new CRef(0, 0, 0).toString(), "...");
  57.     }
  58.  
  59.     @Override
  60.     public String toString() {
  61.         return "[" + timestamp + "] (" + affectedBlock + ") <- " + formatCallStack() + " " + message;
  62.     }
  63. }
  64.  
Advertisement