Skip to content

Commit

Permalink
Merge pull request #19 from NGSpace/functions
Browse files Browse the repository at this point in the history
[5.0.0] Add functions, some refactoring
  • Loading branch information
NGSpace authored Oct 15, 2024
2 parents 32e026e + 3e7f1e0 commit ff667ea
Show file tree
Hide file tree
Showing 45 changed files with 557 additions and 146 deletions.
11 changes: 6 additions & 5 deletions .settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#
#Tue Sep 10 00:49:57 IDT 2024
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=17
org.eclipse.jdt.core.compiler.codegen.targetPlatform=21
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=17
org.eclipse.jdt.core.compiler.compliance=21
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=17
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=21
3 changes: 3 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ If you do know here are some rules:
3. Below 10k lines additions and deletions (combined)
4. Do not add dependencies.
5. Do not add any precompiled code.

# License
By contributing to this repository you agree to give me (NGSpace/NGS/NoGravitySpace/Tomer akiva) full control over your work.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ loader_version=0.15.11

# Mod Properties
# REMEMBER TO CHANGE THE VALUE IN FABRIC.MOD.JSON
mod_version=4.0.5
mod_version=5.0.0
maven_group=hudder
archives_base_name=hudder

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/github/ngspace/hudder/Hudder.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,13 @@ public class Hudder implements ModInitializer {
))
);
}

public static void showToast(MinecraftClient CLIENT, Text title, Text content) {
CLIENT.getToastManager().add(new SystemToast(SystemToast.Type.PERIODIC_NOTIFICATION,title,content));
}
public static void showWarningToast(MinecraftClient CLIENT, Text title, Text content) {
CLIENT.getToastManager().add(new SystemToast(new SystemToast.Type(10000L),title,content));
}
public void showToast(MinecraftClient CLIENT, Text title) {showToast(CLIENT, title, Text.of(""));}

public void compile(RenderTickCounter f) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public Object getSystemVariable(String key) {

public boolean isDynamicVariable(String key) {
return getSystemVariable(key)==null&&getOperator(key)==null&&!key.contains("+")&&!key.contains("-")
&&!key.contains("/")&&!key.contains("*")&&!key.contains("%")&&!key.contains("=");
&&!key.contains("/")&&!key.contains("*")&&!key.contains("%")&&!key.contains("=")&&!key.contains("(")
&&!key.contains(")");
}
public Object getDynamicVariable(String key) {
Object obj = get(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import io.github.ngspace.hudder.compilers.utils.CompileException;
import io.github.ngspace.hudder.config.ConfigInfo;
import io.github.ngspace.hudder.config.ConfigManager;
import io.github.ngspace.hudder.util.HudderUtils;
import io.github.ngspace.hudder.v2runtime.AV2Compiler;
import io.github.ngspace.hudder.v2runtime.V2Runtime;
import io.github.ngspace.hudder.v2runtime.runtime_elements.BasicConditionV2RuntimeElement;
Expand All @@ -30,8 +31,9 @@ public class HudderV2Compiler extends AV2Compiler {
int bracketscount = 0;

String[] builder = {};

boolean quotesafe = false;
boolean backslashsafe = false;
boolean condSafe = false;
boolean safeappend = false;

Expand Down Expand Up @@ -94,7 +96,7 @@ public class HudderV2Compiler extends AV2Compiler {
} else if (c=='}') {
bracketscount--;
if (bracketscount==0) {
runtime.addRuntimeElement(new VariableV2RuntimeElement(elemBuilder.toString(), this));
runtime.addRuntimeElement(new VariableV2RuntimeElement(elemBuilder.toString(), this, runtime));
elemBuilder.setLength(0);
compileState = TEXT_STATE;
} else elemBuilder.append(c);
Expand All @@ -114,7 +116,7 @@ public class HudderV2Compiler extends AV2Compiler {
case '%':
compileState = TEXT_STATE;
builder = addToArray(builder,elemBuilder.toString().trim());
runtime.addRuntimeElement(new BasicConditionV2RuntimeElement(builder, this, info));
runtime.addRuntimeElement(new BasicConditionV2RuntimeElement(builder, this, info, runtime));
elemBuilder.setLength(0);
break;
case '"':
Expand All @@ -131,18 +133,28 @@ public class HudderV2Compiler extends AV2Compiler {
break;
}
case METHOD_STATE: {
if (backslashsafe) {
backslashsafe = false;
elemBuilder.append(c);
continue;
}
switch (c) {
case ';':
compileState = TEXT_STATE;
case '\\':
backslashsafe = true;
elemBuilder.append(c);
break;
case ',':
builder = addToArray(builder,elemBuilder.toString().trim());
elemBuilder.setLength(0);
case '"':
quotesafe = !quotesafe;
elemBuilder.append(c);
break;
case ';':
if (!quotesafe) compileState = TEXT_STATE;
else elemBuilder.append(c);
break;
default: elemBuilder.append(c);break;
}
if (compileState!=METHOD_STATE) {
builder = addToArray(builder,elemBuilder.toString().trim());
builder = HudderUtils.processParemeters(elemBuilder.toString());
runtime.addRuntimeElement(new MethodV2RuntimeElement(builder, this, info, runtime));
elemBuilder.setLength(0);
builder = new String[0];
Expand Down Expand Up @@ -201,10 +213,10 @@ public class HudderV2Compiler extends AV2Compiler {
break;
}
if (isWhile) {
runtime.addRuntimeElement(new WhileV2RuntimeElement(info, cond, cmds, this));
runtime.addRuntimeElement(new WhileV2RuntimeElement(info, cond, cmds, this, runtime));
break;
}
runtime.addRuntimeElement(new IfV2RuntimeElement(info, cond, cmds, this));
runtime.addRuntimeElement(new IfV2RuntimeElement(info, cond, cmds, this, runtime));
break;
}
default: throw new CompileException("Unknown compile state: " + compileState);
Expand Down
38 changes: 19 additions & 19 deletions src/main/java/io/github/ngspace/hudder/methods/MethodHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
import java.util.function.Consumer;

import io.github.ngspace.hudder.Hudder;
import io.github.ngspace.hudder.compilers.ATextCompiler;
import io.github.ngspace.hudder.compilers.utils.CompileException;
import io.github.ngspace.hudder.compilers.utils.CompileState;
import io.github.ngspace.hudder.config.ConfigInfo;
import io.github.ngspace.hudder.methods.methods.DecimalMethods;
import io.github.ngspace.hudder.methods.methods.GUIMethods;
import io.github.ngspace.hudder.methods.methods.IMethod;
Expand All @@ -23,7 +26,6 @@
import io.github.ngspace.hudder.methods.methods.StringMethods;
import io.github.ngspace.hudder.methods.methods.TextMethod;
import io.github.ngspace.hudder.methods.methods.TexturesMethods;
import io.github.ngspace.hudder.util.HudFileUtils;
import net.minecraft.text.Text;

public class MethodHandler {
Expand All @@ -42,8 +44,16 @@ public MethodHandler() {
register((i,m,c,type,args)->m.setTextLocation(type,(float) (args.length>0?args[0].asDouble():i.scale)),
BOTTOMRIGHT, TOPLEFT, TOPRIGHT, BOTTOMLEFT, MUTE);
register(new TextMethod(), "text");
register((c,m,a,t,s)->a.put(s[1].getAbsoluteValue(), Hudder.ins.textRenderer.getWidth(s[0].asString())),
2, new String[] {"[Text]",Var[0]}, "strwidth");

// TODO remove this clusterfuck of a method (After 5.0.0)

register(new IMethod() {
@Override public boolean isDeprecated(String name) {return true;}
@Override public void invoke(ConfigInfo info, CompileState m, ATextCompiler a, String t, MethodValue... s)
throws CompileException {
a.put(s[1].getAbsoluteValue(), Hudder.ins.textRenderer.getWidth(s[0].asString()));
}
}, 2, new String[] {"[Text]",Var[0]}, "strwidth");



Expand All @@ -59,11 +69,11 @@ public MethodHandler() {


//Logging and errors
register((c,m,a,t,s)->Hudder.ins.player.sendMessage(Text.of(s[0].asStringSafe())),1, TextArg, "alert");
register((c,m,a,t,s)->Hudder.log(s[0].asStringSafe()),1, TextArg, "log");
register((c,m,a,t,s)->Hudder.warn(s[0].asStringSafe()),1, TextArg, "warn");
register((c,m,a,t,s)->Hudder.error(s[0].asStringSafe()),1, TextArg, "error");
register((c,m,a,t,s)->{throw new CompileException(s[0].asStringSafe());},1, TextArg, "throw");
register((c,m,a,t,s)->Hudder.ins.player.sendMessage(Text.of(s[0].asString())),1, TextArg, "alert");
register((c,m,a,t,s)->Hudder.log(s[0].asString()),1, TextArg, "log");
register((c,m,a,t,s)->Hudder.warn(s[0].asString()),1, TextArg, "warn");
register((c,m,a,t,s)->Hudder.error(s[0].asString()),1, TextArg, "error");
register((c,m,a,t,s)->{throw new CompileException(s[0].asString());},1, TextArg, "throw");



Expand All @@ -75,22 +85,11 @@ public MethodHandler() {

//Mathematical operations
register(new DecimalMethods(), "decimalpoint", "float");
/**This looks kinda confusing so I'll explain:
* getAbsoluteValue() is the name the variable (aka the raw text supplied)
* asInt() is the value of the variable as an int (aka the result of getVariable() converted to int)
*/
register((ci,meta,comp,type,args)->comp.put(args[0].getAbsoluteValue(),args[0].asInt()),1,Var,"int","fullnumber");



//String Manipulation
register(new StringMethods(), "concat", "multiplystring", "substring");



//File-IO(THIS IS ALL YOU'RE GETTING, NO MORE FILE-IO! YOU'RE NOT WRITING OR READING FILES ON MY WATCH FUCKERS!)
String[] ar = new String[] {"[Filename]",Var[0]};
register((c,m,a,t,s)->a.put(s[1].getAbsoluteValue(), HudFileUtils.exists(s[0].asStringSafe())), 2, ar, "exists");
}


Expand All @@ -110,6 +109,7 @@ public void register(IMethod method, int length, String[] args, String... names)
for (String name : names) methods.put(name,newmethod);
}

@SuppressWarnings("removal")
public void register(String method, String[] argtypes, String name) {
int[] parameters = new int[argtypes.length];
for (int i = 0;i<argtypes.length;i++) {
Expand Down
26 changes: 17 additions & 9 deletions src/main/java/io/github/ngspace/hudder/methods/MethodValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ protected MethodValue() {}
public abstract int asInt() throws CompileException;
public abstract double asDouble() throws CompileException;
public abstract boolean asBoolean() throws CompileException;


/**
* @deprecated to encourage type safety, will be removed in the future.
*/
@Deprecated(since = "5.0.0", forRemoval = true)
public String asStringSafe() {
try {
return asString();
} catch (Exception e) {
try {
return cleanDouble(asDouble());//String.valueOf(asDouble());
return cleanDouble(asDouble());
} catch (Exception ex) {
try {
return String.valueOf(asBoolean());
Expand All @@ -36,18 +40,22 @@ public String asStringSafe() {
}
}
}
public int asIntSafe() {try {return asInt();} catch (Exception e) {return 0;}}
/**
* @deprecated to encourage type safety, will be removed in the future.
*/
@Deprecated(since = "5.0.0", forRemoval = true)
public double asDoubleSafe() {try {return asDouble();} catch (Exception e) {return 0;}}
/**
* @deprecated to encourage type safety, will be removed in the future.
*/
@Deprecated(since = "5.0.0", forRemoval = true)
public boolean asBooleanSafe() {try {return asBoolean();} catch (Exception e) {return false;}}

@Override public String toString() {return getAbsoluteValue();}

public static String cleanDouble(double d)
{
if(d == (long) d)
return Long.toString((long)d);
else
return Double.toString((long)d);
public static String cleanDouble(double d) {
if(d == (long) d) return Long.toString((long)d);
else return Double.toString((long)d);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
import io.github.ngspace.hudder.methods.MethodValue;

public class DecimalMethods implements IMethod {

@Override
public boolean isDeprecated(String name) {
return true;
}
@Override
public String getDeprecationWarning(String name) {
return "Use truncate function";
}
@Override
public void invoke(ConfigInfo ci, CompileState meta, ATextCompiler comp, String type, MethodValue... args)
throws CompileException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@

@FunctionalInterface
public interface IMethod {

public default boolean isDeprecated(String name) {
return false;
}
public default String getDeprecationWarning(String name) {
return name + " is Deprecated";
}

/**
* Called when the first parameter passed to MetaCompiler has been a command registered with this object.
* @param config - The config used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import net.minecraft.item.ItemStack;

public class InventoryInformationMethods implements IMethod {

// TODO replace this with a proper function (after version 5.0.0)

@Override
public void invoke(ConfigInfo config, CompileState m, ATextCompiler c, String type, MethodValue... s) throws CompileException {
ItemStack stack = Hudder.ins.player.getInventory().getStack(s[0].asInt());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
import io.github.ngspace.hudder.util.HudFileUtils;

public class LoadMethod implements IMethod {
@SuppressWarnings("removal")
@Override
public void invoke(ConfigInfo ci, CompileState meta, ATextCompiler comp, String type, MethodValue... args) throws CompileException {
if (args.length<1)
throw new CompileException("\""+type+"\" only accepts ;"+type+",[file],<text>,<compiler>;");
try {
boolean AddText = (args.length<2 || args[1].asBoolean()) || type.equals("add");
ATextCompiler ecompiler=(args.length>2?Compilers.getCompilerFromName(args[2].asStringSafe()):comp);
ATextCompiler ecompiler=(args.length>2?Compilers.getCompilerFromName(args[2].asString()):comp);
for (var i : Hudder.precomplistners) i.accept(ecompiler);
meta.combineWithResult(ecompiler.compile(ci, HudFileUtils.getFile(args[0].asStringSafe())), AddText);
for (var i : Hudder.postcomplistners) i.accept(ecompiler);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@

public class StringMethods implements IMethod {

@Override
public boolean isDeprecated(String name) {
return true;
}
@Override
public String getDeprecationWarning(String name) {
return "Use \"" + (name.equalsIgnoreCase("multiplystring") ? "repeat" : name) + "\" function";
}

@Override
public void invoke(ConfigInfo ci, CompileState meta, ATextCompiler comp, String type, MethodValue... args) throws CompileException {
try {
switch (type) {
case "concat": {
String str1 = args[0].asStringSafe();
String str2 = args[1].asStringSafe();
String str1 = args[0].asString();
String str2 = args[1].asString();
comp.put(args[2].getAbsoluteValue(),str1+str2);
break;
}
case "multiplystring": {
String str1 = args[0].asStringSafe();
String str1 = args[0].asString();
int times = args[1].asInt();
comp.put(args[2].getAbsoluteValue(),str1.repeat(times));
break;
}
case "substring": {
String str1 = args[0].asStringSafe();
String str1 = args[0].asString();
int start = args[1].asInt();
int end = args[2].asInt();
try {
Expand All @@ -43,7 +52,6 @@ public void invoke(ConfigInfo ci, CompileState meta, ATextCompiler comp, String
throw new CompileException("\""+type+"\" only accepts ;"+type
+(type.equals("concat") ?",[string],[string],[result variable name]" :"")
+(type.equals("multiplystring") ?",[string],[amount],[result variable name]" :"")
+(type.equals("string") ?",[string],[result variable name]" :"")
+(type.equals("substring") ?",[string],[start],[end],[result variable name]" :"")
+ ";");
} catch (Exception e) {throw new CompileException(e.getLocalizedMessage());}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class TexturesMethods implements IMethod {
switch (type) {
case "image","png":
try {
boolean ex = HudFileUtils.exists(HudFileUtils.FOLDER + s[0].asStringSafe().trim());
boolean ex = HudFileUtils.exists(HudFileUtils.FOLDER + s[0].asString().trim());
if (!ex) return;
Identifier id = Identifier.of(s[0].asStringSafe().trim().toLowerCase());
HudFileUtils.getAndRegisterImage(s[0].asStringSafe(),id);
Identifier id = Identifier.of(s[0].asString().trim().toLowerCase());
HudFileUtils.getAndRegisterImage(s[0].asString(),id);
m.elements.add(new TextureElement(id,s[1].asInt(),s[2].asInt(),s[3].asInt(),s[4].asInt()));
} catch (IOException e) {
throw new CompileException(e.getLocalizedMessage());
Expand Down
Loading

0 comments on commit ff667ea

Please sign in to comment.