Skip to content

Commit

Permalink
Fix Replenish and BlockLeveler states
Browse files Browse the repository at this point in the history
  • Loading branch information
Archy-X committed Aug 12, 2023
1 parent d30e03c commit 0a22f6f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ public void onEnable() {
// Load skills, stats
loadSkills();

// Register default traits
traitManager.registerTraitImplementations();

xpRequirements = new XpRequirements(this);
userManager = new BukkitUserManager(this);
hookManager = new HookManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void applyReplenish(BlockBreakEvent event) {
}

private boolean canBeReplenished(Material mat) {
return mat == Material.WHEAT || mat == Material.CARROT || mat == Material.POTATO || mat == Material.BEETROOT || mat == Material.NETHER_WART;
return mat == Material.WHEAT || mat == Material.CARROTS || mat == Material.POTATOES || mat == Material.BEETROOTS || mat == Material.NETHER_WART;
}

private boolean isFullyGrown(Block block) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package dev.aurelium.auraskills.bukkit.source;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import dev.aurelium.auraskills.api.ability.Abilities;
import dev.aurelium.auraskills.api.skill.Skill;
import dev.aurelium.auraskills.api.source.type.BlockXpSource;
Expand Down Expand Up @@ -123,20 +121,19 @@ public Pair<BlockXpSource, Skill> getSource(Block block, BlockXpSource.BlockTrig
anyStateMatches = false;
// Convert block data to json
String blockDataString = block.getBlockData().getAsString(true);
Gson gson = new Gson();
JsonObject jsonObject = gson.fromJson(blockDataString, JsonObject.class);
Map<String, Object> blockDataMap = parseFromBlockData(blockDataString);
// Check if block data matches defined states
for (BlockXpSource.BlockXpSourceState state : source.getStates()) {
if (state == null) continue;
boolean stateMatches = true;
for (Map.Entry<String, Object> stateEntry : state.getStateMap().entrySet()) {
String key = stateEntry.getKey();
Object value = stateEntry.getValue();
if (!jsonObject.has(key)) {
if (!blockDataMap.containsKey(key)) {
stateMatches = false;
break;
}
if (!jsonObject.get(key).getAsString().equals(String.valueOf(value))) {
if (!blockDataMap.get(key).equals(value)) {
stateMatches = false;
break;
}
Expand Down Expand Up @@ -174,4 +171,80 @@ private Map<BlockXpSource, Skill> filterByTrigger(Map<BlockXpSource, Skill> sour
return filtered;
}

private Map<String, Object> parseFromBlockData(String input) {
Map<String, Object> result = new HashMap<>();
// Check if the input is valid
if (input == null || input.isEmpty()) {
return result;
}
// Find the index of the first bracket
int bracketIndex = input.indexOf("[");
// Check if the bracket exists
if (bracketIndex == -1) {
return result;
}
// Get the part of the input after the bracket and remove the closing bracket
String data = input.substring(bracketIndex + 1).replace("]", "");
// Find the index of the first comma
int commaIndex = data.indexOf(",");
// Loop until there are no more commas
while (commaIndex != -1) {
// Get the pair before the comma
String pair = data.substring(0, commaIndex);
// Find the index of the equal sign
int equalIndex = pair.indexOf("=");
// Check if the equal sign exists
if (equalIndex != -1) {
// Get the key and value and trim any whitespace
String key = pair.substring(0, equalIndex).trim();
String value = pair.substring(equalIndex + 1).trim();
// Parse the value and put it in the result map with the key
result.put(key, parseValue(value));
}
// Remove the pair and the comma from the data
data = data.substring(commaIndex + 1);
// Find the next comma index
commaIndex = data.indexOf(",");
}
// Check if there is any remaining data
if (!data.isEmpty()) {
// Find the index of the equal sign
int equalIndex = data.indexOf("=");
// Check if the equal sign exists
if (equalIndex != -1) {
// Get the key and value and trim any whitespace
String key = data.substring(0, equalIndex).trim();
String value = data.substring(equalIndex + 1).trim();
// Parse the value and put it in the result map with the key
result.put(key, parseValue(value));
}
}
// Return the result map
return result;
}

private Object parseValue(String value) {
// Try to parse as an int
try {
return Integer.parseInt(value);
} catch (NumberFormatException ignored) {
// Ignore and continue
}
// Try to parse as a double
try {
return Double.parseDouble(value);
} catch (NumberFormatException ignored) {
// Ignore and continue
}
// Try to parse as a boolean
if (value.equals("true")) {
return true;
}
if (value.equals("false")) {
return false;
}
// Return as a String otherwise
return value;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public class BukkitTraitManager extends TraitManager {
public BukkitTraitManager(AuraSkills plugin) {
super(plugin);
this.plugin = plugin;
registerTraitImplementations();
}

public void registerTraitImplementations() {
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/resources/sources/farming.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ sources:
menu_item:
material: beetroot
nether_wart:
block: nether_warts
block: nether_wart
xp: 3.0
check_replace: false
state:
Expand Down

0 comments on commit 0a22f6f

Please sign in to comment.