Skip to content

Commit

Permalink
Add section macros
Browse files Browse the repository at this point in the history
  • Loading branch information
virustotalop committed Dec 16, 2024
1 parent 39d9d2a commit d30712c
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 18 deletions.
1 change: 1 addition & 0 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ plugins {
dependencies {
compileOnly("com.github.ClubObsidian:FuzzUtil:1.1.0")
compileOnly("javax.inject:javax.inject:1")
compileOnly("com.github.clubobsidian:wrappy:$wrappyVersion")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.clubobsidian.dynamicgui.api.parser.macro;

import com.clubobsidian.wrappy.ConfigurationSection;

import java.io.Serializable;
import java.util.List;

Expand All @@ -27,4 +29,5 @@ public interface MacroParser extends Serializable {

List<String> parseListMacros(final List<String> replaceIn);

ConfigurationSection parseSectionMacros(ConfigurationSection copyInto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class SimpleGui implements Gui {
private final List<LocationWrapper<?>> locations;
private final Map<String, List<Integer>> npcIds;
private transient InventoryWrapper<?> inventoryWrapper;
private final FunctionTree functions;
private transient FunctionTree functions;
private Gui back;
private final Map<String, String> metadata;
private final boolean isStatic;
Expand Down Expand Up @@ -221,6 +221,15 @@ public boolean isStatic() {
}

public Gui clone() {
return SerializationUtils.clone(this);
SimpleGui cloned = SerializationUtils.clone(this);
cloned.functions = this.functions;
for (Slot slot : this.slots) {
Slot clonedSlot = cloned.slots.get(slot.getIndex());
if (clonedSlot instanceof SimpleSlot) {
SimpleSlot clonedSimpleSlot = (SimpleSlot) clonedSlot;
clonedSimpleSlot.setFunctions(slot.getFunctions());
}
}
return cloned;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class SimpleSlot implements Slot {
private final int amount;
private transient ItemStackWrapper<?> itemStack;
private Gui owner;
private final FunctionTree functions;
private transient FunctionTree functions;
private final int updateInterval;
private int tick;
private int frame;
Expand Down Expand Up @@ -180,6 +180,10 @@ public FunctionTree getFunctions() {
return this.functions;
}

public void setFunctions(FunctionTree functions) {
this.functions = functions;
}

@Override
@Nullable
public ItemStackWrapper<?> buildItemStack(@NotNull PlayerWrapper<?> playerWrapper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public SimpleGuiToken(ConfigurationSection section, List<MacroToken> macroTokens
copyMacroTokens.add(new SimpleMacroToken(macrosSection));

this.macroParser = new SimpleMacroParser(copyMacroTokens);
this.macroParser.parseSectionMacros(section);

this.title = this.macroParser.parseStringMacros(section.getString("title"));
this.type = this.parseType(section.getString("type"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.clubobsidian.dynamicgui.api.parser.macro.MacroParser;
import com.clubobsidian.dynamicgui.api.parser.macro.MacroToken;
import com.clubobsidian.wrappy.ConfigurationSection;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -50,10 +51,12 @@ public String parseStringMacros(final String replaceIn) {
}
String replace = replaceIn;
for (MacroToken token : this.tokens) {
for (Map.Entry<String, Object> next : token.getMacros().entrySet()) {
String key = next.getKey();
Object value = next.getValue();

for (Map.Entry<String, Object> entry : token.getMacros().entrySet()) {
if (entry.getValue() instanceof ConfigurationSection) {
continue;
}
String key = entry.getKey();
Object value = entry.getValue();
if (replace.contains(key)) {
replace = StringUtils.replace(replace, key, value.toString());
}
Expand All @@ -62,6 +65,9 @@ public String parseStringMacros(final String replaceIn) {

for (MacroToken token : this.tokens) {
for (Map.Entry<String, Object> entry : token.getMacros().entrySet()) {
if (entry.getValue() instanceof ConfigurationSection) {
continue;
}
String key = entry.getKey();
if (replace.contains(key)) {
return this.parseStringMacros(replace);
Expand All @@ -78,7 +84,9 @@ public List<String> parseListMacros(final List<String> replaceIn) {
for (MacroToken token : this.tokens) {
for (Map.Entry<String, Object> next : token.getMacros().entrySet()) {
String key = next.getKey();

if (next.getValue() instanceof ConfigurationSection) {
continue;
}
for (int i = 0; i < newList.size(); i++) {
String line = newList.get(i);
if (line.contains(key)) {
Expand Down Expand Up @@ -137,4 +145,38 @@ public List<String> parseListMacros(final List<String> replaceIn) {

return newList;
}

@Override
public ConfigurationSection parseSectionMacros(ConfigurationSection copyInto) {
for (MacroToken token : this.tokens) {
for (Map.Entry<String, Object> entry : token.getMacros().entrySet()) {
if (entry.getValue() instanceof ConfigurationSection) {
String macroName = entry.getKey();
ConfigurationSection tokenSection = (ConfigurationSection) entry.getValue();
this.recurReplaceSection(macroName, tokenSection, copyInto);
}
}
}
return copyInto;
}

private void recurReplaceSection(String macroName,
ConfigurationSection tokenSection,
ConfigurationSection copyInto) {
for (Object key : copyInto.getKeys()) {
Object value = copyInto.get(key);
if (value instanceof String) {
String strValue = (String) value;
if (strValue.equals(macroName)) {
copyInto.set(key, null);
ConfigurationSection valueSection = copyInto.getConfigurationSection(key);
valueSection.combine(tokenSection);
}
}
ConfigurationSection childSection = copyInto.getConfigurationSection(key);
if (!childSection.getKeys().isEmpty()) {
this.recurReplaceSection(macroName, tokenSection, childSection);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ private void parse(ConfigurationSection section) {
for (Object key : section.getKeys()) {
if (key instanceof String) {
String keyStr = (String) key;
this.macros.put(keyStr, section.get(key));
ConfigurationSection keySection = section.getConfigurationSection(key);
if (!keySection.getKeys().isEmpty()) {
this.macros.put(keyStr, keySection);
} else {
this.macros.put(keyStr, section.get(key));
}
} else {
DynamicGui.get().getLogger().error("Macros do not support non-string keys: " + key);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public SimpleSlotToken(int index, ConfigurationSection section, List<MacroToken>
ConfigurationSection macrosSection = section.getConfigurationSection("macros");
copyMacroTokens.add(new SimpleMacroToken(macrosSection));
this.macroParser = new SimpleMacroParser(copyMacroTokens);
this.macroParser.parseSectionMacros(section);
this.index = index;
this.amount = this.parseAmount(section.getInteger("amount"));
this.icon = this.macroParser.parseStringMacros(section.getString("icon"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.*;

public class MacroParserTest {

Expand Down Expand Up @@ -168,4 +169,20 @@ public void testMacroChaining() {
assertEquals("with some other text", parsedLore.get(3));
assertEquals("still-not-a-macro", parsedLore.get(4));
}

@Test
public void testSectionMacroToken() {
File file = new File("test.yml");
Configuration config = Configuration.load(file);
ConfigurationSection macroSection = config.getConfigurationSection("macros");

MacroToken token = new SimpleMacroToken(macroSection);
MacroParser parser = new SimpleMacroParser(List.of(token));
ConfigurationSection parentFunctionSection = config.getConfigurationSection("functions");
parser.parseSectionMacros(parentFunctionSection);
ConfigurationSection childFunctionSection = parentFunctionSection.getConfigurationSection("function");
assertEquals(2, childFunctionSection.getKeys().size());
assertEquals("load", childFunctionSection.getString("type"));
assertEquals("function-2", childFunctionSection.getStringList("functions").get(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,10 @@ public class MacrosTokenTest {
public void testGuiMacroToken() {
File file = new File("test.yml");
Configuration config = Configuration.load(file);
ConfigurationSection section = config
.getConfigurationSection("macros");
ConfigurationSection section = config.getConfigurationSection("macros");
MacroToken token = new SimpleMacroToken(section);
Map<String, Object> macros = token.getMacros();

assertEquals(2, macros.size());
assertEquals(3, macros.size());
}

@Test
Expand Down
9 changes: 5 additions & 4 deletions parser/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ macros:
- "not-a-macro"
- "%multiline-test%"
- "still-not-a-macro"

functions:
function:
'%function-macro%':
type: "load"
functions:
- "function-2"
- "function-2"

functions:
function: "%function-macro%"

'0':
name: "Some name"
Expand Down

0 comments on commit d30712c

Please sign in to comment.