Skip to content

Commit

Permalink
feat: Clean up number functions in BUtil (potentially breaking)
Browse files Browse the repository at this point in the history
* Rename 'parseInt' to 'getRandomIntInRange', change the format of defining ranges from (number)-(number) to (number)..(number) and support negative values

* Replace some unnecessary calls to BUtil.getRandomIntInRange witth appropriate alternatives

* Add some debug logs to BUtil's number functions

* Replace 'map(Integer::parseInt)' with 'mapToInt(Integer::parseInt)' in some places
  • Loading branch information
Nadwey committed Jan 10, 2025
1 parent d14c350 commit 73ab75b
Show file tree
Hide file tree
Showing 15 changed files with 66 additions and 107 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/dre/brewery/Barrel.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.dre.brewery.integration.Hook;
import com.dre.brewery.integration.barrel.LogBlockBarrel;
import com.dre.brewery.lore.BrewLore;
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.BoundingBox;
import com.dre.brewery.utility.Logging;
import com.dre.brewery.utility.MinecraftVersion;
Expand Down Expand Up @@ -98,7 +97,7 @@ public Barrel(Block spigot, byte sign, BoundingBox bounds, @Nullable Map<String,
if (items != null) {
for (String slot : items.keySet()) {
if (items.get(slot) instanceof ItemStack) {
this.inventory.setItem(BUtil.parseInt(slot), (ItemStack) items.get(slot));
this.inventory.setItem(Integer.parseInt(slot), (ItemStack) items.get(slot));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/dre/brewery/commands/CommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void cmdHelp(CommandSender sender, String[] args) {

int page = 1;
if (args.length > 1) {
page = BUtil.parseInt(args[1]);
page = BUtil.parseIntOrZero(args[1]);
}

ArrayList<String> commands = getCommands(sender);
Expand All @@ -96,12 +96,12 @@ public static Tuple<Brew, Player> getFromCommand(CommandSender sender, String[]
boolean hasQuality = false;
String pName = null;
if (args.length > 2) {
quality = BUtil.parseInt(args[args.length - 1]);
quality = BUtil.getRandomIntInRange(args[args.length - 1]);

if (quality <= 0 || quality > 10) {
pName = args[args.length - 1];
if (args.length > 3) {
quality = BUtil.parseInt(args[args.length - 2]);
quality = BUtil.getRandomIntInRange(args[args.length - 2]);
}
}
if (quality > 0 && quality <= 10) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CopyCommand implements SubCommand {
@Override
public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender, String label, String[] args) {
if (args.length > 1) {
cmdCopy(sender, BUtil.parseInt(args[1]), lang);
cmdCopy(sender, BUtil.getRandomIntInRange(args[1]), lang);
} else {
cmdCopy(sender, 1, lang);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class HelpCommand implements SubCommand {
public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender, String label, String[] args) {
int page = 1;
if (args.length > 1) {
page = BUtil.parseInt(args[1]);
page = BUtil.parseIntOrZero(args[1]);
}

ArrayList<String> commands = CommandUtil.getCommands(sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender
}
int count = 0;
if (args.length > 2) {
count = BUtil.parseInt(args[2]);
count = BUtil.getRandomIntInRange(args[2]);
}
if (count <= 0) {
count = 20 + (int) (Math.random() * 40);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender
int page = 1;
String world = null;
if (args.length > 2) {
page = BUtil.parseInt(args[2]);
page = BUtil.parseIntOrZero(args[2]);
}
if (args.length > 3) {
world = args[3];
Expand All @@ -57,7 +57,7 @@ public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender
} else if (args[1].equalsIgnoreCase("remove")) {

if (args.length > 2) {
int id = BUtil.parseInt(args[2]);
int id = BUtil.parseIntOrZero(args[2]);
Wakeup.remove(sender, id);
} else {
lang.sendEntry(sender, "Etc_Usage");
Expand All @@ -68,7 +68,7 @@ public void execute(BreweryPlugin breweryPlugin, Lang lang, CommandSender sender

int id = -1;
if (args.length > 2) {
id = BUtil.parseInt(args[2]);
id = BUtil.parseIntOrZero(args[2]);
if (id < 0) {
id = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,7 @@
package com.dre.brewery.configuration.sector.capsule;

import eu.okaeri.configs.OkaeriConfig;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.*;

import java.util.List;

Expand Down
13 changes: 2 additions & 11 deletions src/main/java/com/dre/brewery/listeners/BlockListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@

package com.dre.brewery.listeners;

import com.dre.brewery.BPlayer;
import com.dre.brewery.BSealer;
import com.dre.brewery.Barrel;
import com.dre.brewery.BreweryPlugin;
import com.dre.brewery.DistortChat;
import com.dre.brewery.*;
import com.dre.brewery.api.events.barrel.BarrelDestroyEvent;
import com.dre.brewery.configuration.ConfigManager;
import com.dre.brewery.configuration.files.Config;
Expand All @@ -38,12 +34,7 @@
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockBurnEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.block.*;

public class BlockListener implements Listener {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dre/brewery/recipe/BCauldronRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public static BCauldronRecipe fromConfig(String id, ConfigCauldronIngredient cfg
if (split.length == 1) {
minute = 10;
} else if (split.length == 2) {
minute = BUtil.parseInt(split[1]);
minute = BUtil.parseIntOrZero(split[1]);
} else {
Logging.errorLog("cookParticle: '" + entry + "' in: " + recipe.name);
return null;
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/com/dre/brewery/recipe/BEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,21 @@ public BEffect(String effectString) {

private void setLvl(String[] range) {
if (range.length == 1) {
maxlvl = (short) BUtil.parseInt(range[0]);
maxlvl = (short) BUtil.getRandomIntInRange(range[0]);
minlvl = 1;
} else {
maxlvl = (short) BUtil.parseInt(range[1]);
minlvl = (short) BUtil.parseInt(range[0]);
maxlvl = (short) BUtil.getRandomIntInRange(range[1]);
minlvl = (short) BUtil.getRandomIntInRange(range[0]);
}
}

private void setDuration(String[] range) {
if (range.length == 1) {
maxduration = (short) BUtil.parseInt(range[0]);
maxduration = (short) BUtil.getRandomIntInRange(range[0]);
minduration = (short) (maxduration / 8);
} else {
maxduration = (short) BUtil.parseInt(range[1]);
minduration = (short) BUtil.parseInt(range[0]);
maxduration = (short) BUtil.getRandomIntInRange(range[1]);
minduration = (short) BUtil.getRandomIntInRange(range[0]);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/dre/brewery/recipe/BRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public static BRecipe fromConfig(String recipeId, ConfigRecipe configRecipe) {
int[] cmData = new int[3];
for (int i = 0; i < 3; i++) {
if (cmdParts.length > i) {
cmData[i] = BUtil.parseInt(cmdParts[i]);
cmData[i] = BUtil.getRandomIntInRange(cmdParts[i]);
} else {
cmData[i] = i == 0 ? 0 : cmData[i - 1];
}
Expand Down Expand Up @@ -222,7 +222,7 @@ public static List<RecipeItem> loadIngredients(List<String> stringList, String r
String[] ingredParts = item.split("/");
int amount = 1;
if (ingredParts.length == 2) {
amount = BUtil.parseInt(ingredParts[1]);
amount = BUtil.getRandomIntInRange(ingredParts[1]);
if (amount < 1) {
Logging.errorLog(recipeId + ": Invalid Item Amount: " + ingredParts[1]);
return null;
Expand Down Expand Up @@ -280,7 +280,7 @@ public static List<RecipeItem> loadIngredients(List<String> stringList, String r
Material mat = MaterialUtil.getMaterialSafely(matParts[0]);
short durability = -1;
if (matParts.length == 2) {
durability = (short) BUtil.parseInt(matParts[1]);
durability = (short) BUtil.getRandomIntInRange(matParts[1]);
}
if (mat == null && Hook.VAULT.isEnabled()) {
try {
Expand Down
25 changes: 13 additions & 12 deletions src/main/java/com/dre/brewery/storage/BData.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public static void readData() {
boolean stat = section.getBoolean(uid + ".stat", false);
int lastUpdate = section.getInt(uid + ".lastUpdate", 0);

Brew.loadLegacy(ingredients, BUtil.parseInt(uid), quality, alc, distillRuns, ageTime, BarrelWoodType.fromAny(wood), recipe, unlabeled, persistent, stat, lastUpdate);
Brew.loadLegacy(ingredients, Integer.parseInt(uid), quality, alc, distillRuns, ageTime, BarrelWoodType.fromAny(wood), recipe, unlabeled, persistent, stat, lastUpdate);
}
}

Expand Down Expand Up @@ -265,7 +265,7 @@ public static List<Ingredient> oldDeserializeIngredients(ConfigurationSection ma
if (m == null) continue;
SimpleItem item;
if (matSplit.length == 2) {
item = new SimpleItem(m, (short) BUtil.parseInt(matSplit[1]));
item = new SimpleItem(m, (short) BUtil.parseIntOrZero(matSplit[1]));
} else {
item = new SimpleItem(m);
}
Expand Down Expand Up @@ -357,8 +357,7 @@ public static void loadWorldData(String uuid, World world) {
if (block != null) {
String[] splitted = block.split("/");
if (splitted.length == 3) {

Block worldBlock = world.getBlockAt(BUtil.parseInt(splitted[0]), BUtil.parseInt(splitted[1]), BUtil.parseInt(splitted[2]));
Block worldBlock = world.getBlockAt(Integer.parseInt(splitted[0]), Integer.parseInt(splitted[1]), Integer.parseInt(splitted[2]));
BIngredients ingredients = loadCauldronIng(section, cauldron + ".ingredients");
int state = section.getInt(cauldron + ".state", 0);

Expand All @@ -385,15 +384,17 @@ public static void loadWorldData(String uuid, World world) {

// load itemStacks from invSection
ConfigurationSection invSection = section.getConfigurationSection(barrel + ".inv");
Block block = world.getBlockAt(BUtil.parseInt(splitted[0]), BUtil.parseInt(splitted[1]), BUtil.parseInt(splitted[2]));
Block block = world.getBlockAt(Integer.parseInt(splitted[0]), Integer.parseInt(splitted[1]), Integer.parseInt(splitted[2]));
float time = (float) section.getDouble(barrel + ".time", 0.0);
byte sign = (byte) section.getInt(barrel + ".sign", 0);

BoundingBox box = null;
if (section.contains(barrel + ".bounds")) {
String[] bds = section.getString(barrel + ".bounds", "").split(",");
if (bds.length == 6) {
box = new BoundingBox(BUtil.parseInt(bds[0]), BUtil.parseInt(bds[1]), BUtil.parseInt(bds[2]), BUtil.parseInt(bds[3]), BUtil.parseInt(bds[4]), BUtil.parseInt(bds[5]));
box = BoundingBox.fromPoints(
Arrays.stream(bds).mapToInt(Integer::parseInt).toArray()
);
}
} else if (section.contains(barrel + ".st")) {
// Convert from Stair and Wood Locations to BoundingBox
Expand All @@ -408,7 +409,7 @@ public static void loadWorldData(String uuid, World world) {
if (woLength > 1) {
System.arraycopy(wo, 0, points, st.length, woLength);
}
int[] locs = Arrays.stream(points).mapToInt(s -> BUtil.parseInt(s)).toArray();
int[] locs = Arrays.stream(points).mapToInt(Integer::parseInt).toArray();
try {
box = BoundingBox.fromPoints(locs);
} catch (Exception e) {
Expand Down Expand Up @@ -447,12 +448,12 @@ public static void loadWorldData(String uuid, World world) {
if (loc != null) {
String[] splitted = loc.split("/");
if (splitted.length == 5) {
double x = Double.parseDouble(splitted[0]);
double y = Double.parseDouble(splitted[1]);
double z = Double.parseDouble(splitted[2]);
float pitch = Float.parseFloat(splitted[3]);
float yaw = Float.parseFloat(splitted[4]);

double x = BUtil.parseDouble(splitted[0]);
double y = BUtil.parseDouble(splitted[1]);
double z = BUtil.parseDouble(splitted[2]);
float pitch = BUtil.parseFloat(splitted[3]);
float yaw = BUtil.parseFloat(splitted[4]);
Location location = new Location(world, x, y, z, yaw, pitch);

initWakeups.add(new Wakeup(location));
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/dre/brewery/storage/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import com.dre.brewery.storage.interfaces.ExternallyAutoSavable;
import com.dre.brewery.storage.interfaces.SerializableThing;
import com.dre.brewery.storage.records.BreweryMiscData;
import com.dre.brewery.utility.BUtil;
import com.dre.brewery.utility.Logging;
import lombok.Getter;
import org.bukkit.Bukkit;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ public Barrel getBarrel(UUID id) {
return null;
}

List<Integer> bounds = Arrays.stream(
int[] bounds = Arrays.stream(
dataFile.getString(path + ".bounds").split(",")
)
.map(Integer::parseInt).toList();
.mapToInt(Integer::parseInt).toArray();

BoundingBox boundingBox = BoundingBox.fromPoints(bounds);
float time = (float) dataFile.getDouble(path + ".time", 0.0);
Expand Down
Loading

0 comments on commit 73ab75b

Please sign in to comment.