Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/patch'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/org/lins/mmmjjkx/rykenslimefuncustomizer/objects/script/ScriptEval.java
  • Loading branch information
lijinhong11 committed Sep 15, 2024
2 parents d0ba4c4 + aeda6c8 commit 9e4c9ef
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@ public void unregister() {
ScriptableListeners.removeScriptableListener(addonId);

if (config != null) {
config.onReloadHandler().close();
if (config.onReloadHandler() != null) {
config.onReloadHandler().close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import io.github.thebusybiscuit.slimefun4.implementation.operations.CraftingOperation;
import io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import lombok.Getter;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.AContainer;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe;
Expand All @@ -39,6 +41,7 @@ public class CustomRecipeMachine extends AContainer implements RecipeDisplayItem
private final List<CustomMachineRecipe> recipes;
private final int energyPerCraft;
private final int capacity;
private final boolean hideAllRecipes;

public static final ItemStack RECIPE_INPUT =
new CustomItemStack(Material.GREEN_STAINED_GLASS_PANE, "&aMulti-Item Input", "", "&2> &aClick to view");
Expand All @@ -59,7 +62,8 @@ public CustomRecipeMachine(
int energyPerCraft,
int capacity,
@Nullable CustomMenu menu,
int speed) {
int speed,
boolean hideAllRecipes) {
super(itemGroup, item, recipeType, recipe);

this.processor = new MachineProcessor<>(this);
Expand All @@ -71,6 +75,7 @@ public CustomRecipeMachine(
this.energyPerCraft = energyPerCraft;
this.capacity = capacity;
this.menu = menu;
this.hideAllRecipes = hideAllRecipes;

if (menu == null) {
this.createPreset(this, this.getInventoryTitle(), super::constructMenu);
Expand Down Expand Up @@ -134,8 +139,16 @@ public ItemStack getProgressBar() {
@NotNull public List<ItemStack> getDisplayRecipes() {
List<ItemStack> displayRecipes = new ArrayList<>();

if (hideAllRecipes) {
return displayRecipes;
}

int i = 0;
for (CustomMachineRecipe recipe : raw_recipes) {
if (recipe.isHide()) {
continue;
}

ItemStack[] input = recipe.getInput();
ItemStack[] output = recipe.getOutput();

Expand Down Expand Up @@ -214,7 +227,7 @@ protected void tick(Block b) {
}
} else {
if (outputs.length > 0) {
int index = new Random().nextInt(outputs.length);
int index = new SecureRandom().nextInt(outputs.length);
ItemStack is = outputs[index];
if (is != null) {
inv.pushItem(is.clone(), this.getOutputSlots());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public class CustomTemplateMachine extends AbstractEmptyMachine<CustomTemplateCr
private final int capacity;
private final boolean fasterIfMoreTemplates;
private final boolean moreOutputIfMoreTemplates;
private final boolean hideAllRecipes;

public CustomTemplateMachine(
ItemGroup itemGroup,
Expand All @@ -75,7 +76,8 @@ public CustomTemplateMachine(
int consumption,
int capacity,
boolean fasterIfMoreTemplates,
boolean moreOutputIfMoreTemplates) {
boolean moreOutputIfMoreTemplates,
boolean hideAllRecipes) {
super(itemGroup, item, recipeType, recipe);

this.processor = new MachineProcessor<>(this);
Expand All @@ -88,6 +90,7 @@ public CustomTemplateMachine(
this.capacity = capacity;
this.fasterIfMoreTemplates = fasterIfMoreTemplates;
this.moreOutputIfMoreTemplates = moreOutputIfMoreTemplates;
this.hideAllRecipes = hideAllRecipes;

createPreset(this, bmp -> {
menu.apply(bmp);
Expand Down Expand Up @@ -133,9 +136,17 @@ public void tick(Block b, SlimefunItem item, Config data) {
@NotNull public List<ItemStack> getDisplayRecipes() {
List<ItemStack> displayRecipes = new ArrayList<>();

if (hideAllRecipes) {
return displayRecipes;
}

int templateIndex = 0, recipeIndex = 0;
for (MachineTemplate template : templates) {
for (CustomMachineRecipe recipe : template.recipes()) {
if (recipe.isHide()) {
continue;
}

if (recipe.getInput().length == 0) {
ItemStack templateItem = template.template().clone();
CommonUtils.addLore(templateItem, true, "&d&l&o*模板物品不消耗*");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.bukkit.Bukkit;
import org.jetbrains.annotations.Nullable;

public class RecipeTypeMap {
private static final Map<String, RecipeType> recipeTypes;

static {
recipeTypes = new HashMap<>();

RecipeTypeExpandIntegration.registerRecipeTypes();
}

public static void removeRecipeTypes(String... keys) {
Expand All @@ -34,4 +38,40 @@ public static void clearRecipeTypes() {
@Nullable public static RecipeType getRecipeType(String s) {
return recipeTypes.get(s);
}

private enum RecipeTypeExpandIntegration {
INFINITY_EXPANSION("io.github.mooy1.infinityexpansion.items.blocks.InfinityWorkbench", "TYPE", true),
SLIME_TINKER("io.github.sefiraat.slimetinker.items.workstations.workbench.Workbench", "TYPE", true);

private final String clazz;
private final String fieldName;
private final boolean isStatic;

RecipeTypeExpandIntegration(String clazz, String fieldName, boolean isStatic) {
this.clazz = clazz;
this.fieldName = fieldName;
this.isStatic = isStatic;
}

static void registerRecipeTypes() {
for (RecipeTypeExpandIntegration integration : values()) {
String className = integration.clazz;
String fieldName = integration.fieldName;
try {
Class<?> clazz = Class.forName(className);
Object instance;
if (integration.isStatic) {
instance = clazz.getField(fieldName).get(null);
} else {
instance = clazz.newInstance(); //or something sus
}
if (instance instanceof RecipeType) {
RecipeTypeMap.pushRecipeType((RecipeType) instance);
}
} catch (Exception e) {
Bukkit.getLogger().warning("Failed to get external recipe type from " + className + "#" + fieldName + ": " + e.getMessage());
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package org.lins.mmmjjkx.rykenslimefuncustomizer.objects.machine;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import lombok.Getter;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.abstractItems.MachineRecipe;
import org.bukkit.inventory.ItemStack;

@Getter
public class CustomMachineRecipe extends MachineRecipe {
@Getter
private final List<Integer> chances;

@Getter
private final boolean chooseOneIfHas;

@Getter
private final boolean forDisplay;

private final Random RNG = new Random();
private final boolean hide;

public CustomMachineRecipe(
int seconds,
ItemStack[] input,
ItemStack[] output,
List<Integer> chances,
boolean chooseOneIfHas,
boolean forDisplay) {
boolean forDisplay,
boolean hide) {
super(seconds, input.clone(), output.clone());

this.chances = chances;
this.chooseOneIfHas = chooseOneIfHas;
this.forDisplay = forDisplay;
this.hide = hide;
}

public List<ItemStack> getMatchChanceResult() {
Expand All @@ -52,7 +51,7 @@ private boolean matchChance(Integer chance) {
if (chance >= 100) return true;
if (chance < 1) return false;

int result = RNG.nextInt(100);
int result = new SecureRandom().nextInt(100);
return result < chance;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.security.Permission;
import java.security.Permissions;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.*;
import java.util.stream.IntStream;
import lombok.AccessLevel;
Expand All @@ -21,6 +22,7 @@
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitTask;
import org.graalvm.polyglot.HostAccess;
import org.jetbrains.annotations.Nullable;
import org.lins.mmmjjkx.rykenslimefuncustomizer.RykenSlimefunCustomizer;
Expand Down Expand Up @@ -137,15 +139,50 @@ protected final void setup() {
return arr[random.nextInt(arr.length)];
});

addThing("runLater", (BiConsumer<Function<?, ?>, Integer>) (r, l) -> Bukkit.getScheduler().runTaskLater(RykenSlimefunCustomizer.INSTANCE, () -> r.apply(null), l));
addThing("runRepeating", (CiConsumer<Function<?, ?>, Integer, Integer>) (r, l, t) -> Bukkit.getScheduler().runTaskTimer(RykenSlimefunCustomizer.INSTANCE, () -> r.apply(null), l, t));
addThing("runAsync", (Consumer<Function<?, ?>>) r -> Bukkit.getScheduler().runTaskAsynchronously(RykenSlimefunCustomizer.INSTANCE, () -> r.apply(null)));
addThing("runLaterAsync", (BiConsumer<Function<?, ?>, Integer>) (r, l) -> Bukkit.getScheduler().runTaskLaterAsynchronously(RykenSlimefunCustomizer.INSTANCE, () -> r.apply(null), l));
addThing("runRepeatingAsync", (CiConsumer<Function<?, ?>, Integer, Integer>) (r, l, t) -> Bukkit.getScheduler().runTaskTimerAsynchronously(RykenSlimefunCustomizer.INSTANCE, () -> r.apply(null), l, t));
addThing("runLater", (BiFunction<Function<Object[], ?>, Integer, BukkitTask>) (r, l) -> {
AtomicReference<BukkitTask> task = new AtomicReference<>();
Bukkit.getScheduler().runTaskLater(RykenSlimefunCustomizer.INSTANCE, t -> {
r.apply(new Object[]{t});
task.set(t);
}, l);
return task.get();
});
addThing("runRepeating", (CiFunction<Function<Object[], ?>, Integer, Integer, BukkitTask>) (r, l, t) -> {
AtomicReference<BukkitTask> task = new AtomicReference<>();
Bukkit.getScheduler().runTaskTimer(RykenSlimefunCustomizer.INSTANCE, ta -> {
r.apply(new Object[]{ta});
task.set(ta);
}, l, t);
return task.get();
});
addThing("runAsync", (Function<Function<Object[], ?>, BukkitTask>) r -> {
AtomicReference<BukkitTask> task = new AtomicReference<>();
Bukkit.getScheduler().runTaskAsynchronously(RykenSlimefunCustomizer.INSTANCE, t -> {
r.apply(new Object[]{t});
task.set(t);
});
return task.get();
});
addThing("runLaterAsync", (BiFunction<Function<Object[], ?>, Integer, BukkitTask>) (r, l) -> {
AtomicReference<BukkitTask> task = new AtomicReference<>();
Bukkit.getScheduler().runTaskLaterAsynchronously(RykenSlimefunCustomizer.INSTANCE, t -> {
r.apply(null);
task.set(t);
}, l);
return task.get();
});
addThing("runRepeatingAsync", (CiFunction<Function<Object[], ?>, Integer, Integer, BukkitTask>) (r, l, t) -> {
AtomicReference<BukkitTask> task = new AtomicReference<>();
Bukkit.getScheduler().runTaskTimerAsynchronously(RykenSlimefunCustomizer.INSTANCE, ta -> {
r.apply(null);
task.set(ta);
}, l, t);
return task.get();
});

addThing("getAddonConfig", (Supplier<YamlConfiguration>) () -> {
if (addon.getConfig() == null) {
throw new RuntimeException("The addon hasn't config file!");
throw new RuntimeException("The addon does not have a config file!");
}

return addon.getConfig().config();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public CustomRecipeMachine readEach(String s) {
return null;
}

boolean hideAllRecipes = section.getBoolean("hideAllRecipes", false);

List<CustomMachineRecipe> mr = readRecipes(s, input.size(), output.size(), recipes, addon);

return new CustomRecipeMachine(
Expand All @@ -105,7 +107,8 @@ public CustomRecipeMachine readEach(String s) {
energy,
capacity,
menu,
speed);
speed,
hideAllRecipes);
}

@Override
Expand Down Expand Up @@ -183,11 +186,12 @@ private List<CustomMachineRecipe> readRecipes(

boolean chooseOne = recipes.getBoolean("chooseOne", false);
boolean forDisplay = recipes.getBoolean("forDisplay", false);
boolean hide = recipes.getBoolean("hide", false);

input = CommonUtils.removeNulls(input);
output = CommonUtils.removeNulls(output);

list.add(new CustomMachineRecipe(seconds, input, output, chances, chooseOne, forDisplay));
list.add(new CustomMachineRecipe(seconds, input, output, chances, chooseOne, forDisplay, hide));
}
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ public CustomTemplateMachine readEach(String s) {
return null;
}

boolean hideAllRecipes = section.getBoolean("hideAllRecipes", false);

return new CustomTemplateMachine(
group.getSecondValue(),
sfis,
Expand All @@ -115,7 +117,8 @@ public CustomTemplateMachine readEach(String s) {
energy,
capacity,
fasterIfMoreTemplates,
moreOutputIfMoreTemplates);
moreOutputIfMoreTemplates,
hideAllRecipes);
}

private List<MachineTemplate> readTemplates(
Expand Down Expand Up @@ -192,11 +195,12 @@ private List<CustomMachineRecipe> readRecipes(

boolean chooseOne = recipes.getBoolean("chooseOne", false);
boolean forDisplay = recipes.getBoolean("forDisplay", false);
boolean hide = recipes.getBoolean("hide", false);

input = CommonUtils.removeNulls(input);
output = CommonUtils.removeNulls(output);

list.add(new CustomMachineRecipe(seconds, input, output, chances, chooseOne, forDisplay));
list.add(new CustomMachineRecipe(seconds, input, output, chances, chooseOne, forDisplay, hide));
}
return list;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ public static Class<?> generateClass(

builder = builder.implement(interfaces).name(finalClassName);

DynamicType.Unloaded<?> unloaded = builder.make();
Class<?> clazz = unloaded.load(extendClass.getClassLoader()).getLoaded();
Class<?> clazz;
try (DynamicType.Unloaded<?> unloaded = builder.make()) {
clazz = unloaded.load(extendClass.getClassLoader()).getLoaded();
}
cache.put(finalClassName, clazz);
return clazz;
}
Expand Down

0 comments on commit 9e4c9ef

Please sign in to comment.