|
| 1 | +package me.anatoliy57.chunit.functions; |
| 2 | + |
| 3 | +import com.laytonsmith.PureUtilities.TermColors; |
| 4 | +import com.laytonsmith.annotations.api; |
| 5 | +import com.laytonsmith.core.MSVersion; |
| 6 | +import com.laytonsmith.core.Static; |
| 7 | +import com.laytonsmith.core.constructs.CVoid; |
| 8 | +import com.laytonsmith.core.constructs.Target; |
| 9 | +import com.laytonsmith.core.environments.Environment; |
| 10 | +import com.laytonsmith.core.exceptions.CRE.CRECastException; |
| 11 | +import com.laytonsmith.core.exceptions.CRE.CREThrowable; |
| 12 | +import com.laytonsmith.core.exceptions.ConfigRuntimeException; |
| 13 | +import com.laytonsmith.core.functions.AbstractFunction; |
| 14 | +import com.laytonsmith.core.natives.interfaces.Mixed; |
| 15 | + |
| 16 | +import java.io.*; |
| 17 | +import java.nio.charset.StandardCharsets; |
| 18 | + |
| 19 | +public class Echo { |
| 20 | + public static String docs() { |
| 21 | + return "A set of functions for logs"; |
| 22 | + } |
| 23 | + |
| 24 | + public static BufferedWriter out; |
| 25 | + |
| 26 | + static { |
| 27 | + out = new BufferedWriter(new OutputStreamWriter(new |
| 28 | + FileOutputStream(java.io.FileDescriptor.out), StandardCharsets.US_ASCII), 512); |
| 29 | + } |
| 30 | + |
| 31 | + @api |
| 32 | + public static class print extends AbstractFunction { |
| 33 | + |
| 34 | + public print() { |
| 35 | + } |
| 36 | + |
| 37 | + public String getName() { |
| 38 | + return "print"; |
| 39 | + } |
| 40 | + |
| 41 | + public Integer[] numArgs() { |
| 42 | + return new Integer[]{1}; |
| 43 | + } |
| 44 | + |
| 45 | + public String docs() { |
| 46 | + return "void {message} Prints a message"; |
| 47 | + } |
| 48 | + |
| 49 | + public Class<? extends CREThrowable>[] thrown() { |
| 50 | + return new Class[]{CRECastException.class}; |
| 51 | + } |
| 52 | + |
| 53 | + public boolean isRestricted() { |
| 54 | + return true; |
| 55 | + } |
| 56 | + |
| 57 | + public MSVersion since() { |
| 58 | + return MSVersion.V3_0_2; |
| 59 | + } |
| 60 | + |
| 61 | + public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { |
| 62 | + String mes = Static.MCToANSIColors(args[0].val()); |
| 63 | + |
| 64 | + if (mes.contains("\u001b")) { |
| 65 | + mes = mes + TermColors.reset(); |
| 66 | + } |
| 67 | + |
| 68 | + try { |
| 69 | + Echo.out.write(mes); |
| 70 | + out.flush(); |
| 71 | + } catch (IOException ignored) {} |
| 72 | + |
| 73 | + return CVoid.VOID; |
| 74 | + } |
| 75 | + |
| 76 | + public Boolean runAsync() { |
| 77 | + return null; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @api |
| 82 | + public static class println extends AbstractFunction { |
| 83 | + |
| 84 | + public println() { |
| 85 | + } |
| 86 | + |
| 87 | + public String getName() { |
| 88 | + return "println"; |
| 89 | + } |
| 90 | + |
| 91 | + public Integer[] numArgs() { |
| 92 | + return new Integer[]{0, 1}; |
| 93 | + } |
| 94 | + |
| 95 | + public String docs() { |
| 96 | + return "void {message} Prints a message and then terminate the line."; |
| 97 | + } |
| 98 | + |
| 99 | + public Class<? extends CREThrowable>[] thrown() { |
| 100 | + return new Class[]{CRECastException.class}; |
| 101 | + } |
| 102 | + |
| 103 | + public boolean isRestricted() { |
| 104 | + return true; |
| 105 | + } |
| 106 | + |
| 107 | + public MSVersion since() { |
| 108 | + return MSVersion.V3_0_2; |
| 109 | + } |
| 110 | + |
| 111 | + public Mixed exec(Target t, Environment env, Mixed... args) throws ConfigRuntimeException { |
| 112 | + String mes; |
| 113 | + if (args.length != 0) { |
| 114 | + mes = Static.MCToANSIColors(args[0].val()); |
| 115 | + } else { |
| 116 | + mes = ""; |
| 117 | + } |
| 118 | + |
| 119 | + if (mes.contains("\u001b")) { |
| 120 | + mes = mes + TermColors.reset(); |
| 121 | + } |
| 122 | + |
| 123 | + try { |
| 124 | + Echo.out.write(mes); |
| 125 | + Echo.out.write('\n'); |
| 126 | + out.flush(); |
| 127 | + } catch (IOException ignored) {} |
| 128 | + return CVoid.VOID; |
| 129 | + } |
| 130 | + |
| 131 | + public Boolean runAsync() { |
| 132 | + return null; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments