diff --git a/src/main/java/com/cleanroommc/bogosorter/common/config/BogoSorterConfig.java b/src/main/java/com/cleanroommc/bogosorter/common/config/BogoSorterConfig.java index 1a029d2a..44b23088 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/config/BogoSorterConfig.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/config/BogoSorterConfig.java @@ -41,7 +41,7 @@ public static void save(JsonObject json) { general.addProperty("enableAutoRefill", playerConfig.enableAutoRefill); general.addProperty("refillDmgThreshold", playerConfig.autoRefillDamageThreshold); general.addProperty("enableHotbarSwap", HotbarSwap.isEnabled()); - general.addProperty("sortSound", SortHandler.sortSound.getSoundName().toString()); + general.addProperty("sortSound", SortHandler.getSortSoundName()); general.addProperty("buttonColor", "#" + Integer.toHexString(buttonColor)); general.addProperty("_comment", "By setting the chance below to 0 you agree to have no humor and that you are boring."); @@ -78,11 +78,11 @@ public static void load(JsonObject json) { playerConfig.enableAutoRefill = JsonHelper.getBoolean(general, true, "enableAutoRefill"); playerConfig.autoRefillDamageThreshold = (short) JsonHelper.getInt(general, 1, "refillDmgThreshold"); HotbarSwap.setEnabled(JsonHelper.getBoolean(general, true, "enableHotbarSwap")); - String sortSound = JsonHelper.getString(general, "ui.button.click", "sortSound"); - SortHandler.sortSound = SoundEvent.REGISTRY.getObject(new ResourceLocation(sortSound)); - if (SortHandler.sortSound == null) { - SortHandler.sortSound = SoundEvents.UI_BUTTON_CLICK; - } + SortHandler.sortSound = JsonHelper.getElement(general, SoundEvents.UI_BUTTON_CLICK, element -> { + if (element.isJsonNull()) return null; + SoundEvent soundEvent = SoundEvent.REGISTRY.getObject(new ResourceLocation(element.getAsString())); + return soundEvent != null ? soundEvent : SoundEvents.UI_BUTTON_CLICK; + }, "sortSound"); buttonColor = JsonHelper.getColor(general, 0xFFFFFFFF, "buttonColor"); } sortRules.clear(); diff --git a/src/main/java/com/cleanroommc/bogosorter/common/sort/SortHandler.java b/src/main/java/com/cleanroommc/bogosorter/common/sort/SortHandler.java index 4d86b7df..4c66cb31 100644 --- a/src/main/java/com/cleanroommc/bogosorter/common/sort/SortHandler.java +++ b/src/main/java/com/cleanroommc/bogosorter/common/sort/SortHandler.java @@ -20,9 +20,11 @@ import net.minecraft.item.ItemStack; import net.minecraft.util.SoundEvent; import net.minecraft.util.text.TextComponentString; +import net.minecraftforge.fml.common.registry.ForgeRegistries; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.concurrent.atomic.AtomicReference; @@ -33,12 +35,37 @@ public class SortHandler { public static final Map> cacheNbtSortRules = new Object2ObjectOpenHashMap<>(); public static final AtomicReference> currentNbtSortRules = new AtomicReference<>(Collections.emptyList()); - public static SoundEvent sortSound = SoundEvents.UI_BUTTON_CLICK; + @Nullable public static SoundEvent sortSound = SoundEvents.UI_BUTTON_CLICK; + private static List foolsSounds = null; + private static long foolsBuildTime = 0; + + public static String getSortSoundName() { + return sortSound == null ? "null" : sortSound.getSoundName().toString(); + } @SideOnly(Side.CLIENT) public static void playSortSound() { - SoundEvent sound = BogoSorter.isAprilFools() ? SoundEvent.REGISTRY.getObjectById(BogoSorter.RND.nextInt(SoundEvent.REGISTRY.getKeys().size())) : sortSound; - Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(sound, 1f)); + SoundEvent sound; + if (BogoSorter.isAprilFools()) { + if (foolsSounds == null || foolsBuildTime - Minecraft.getSystemTime() > 300000) { + List sounds = new ArrayList<>(256); + for (SoundEvent soundEvent : ForgeRegistries.SOUND_EVENTS) { + if (soundEvent != null && + !soundEvent.getSoundName().getPath().contains("music.") && + !soundEvent.getSoundName().getPath().contains("record.")) { + sounds.add(soundEvent); + } + } + foolsSounds = sounds; + foolsBuildTime = Minecraft.getSystemTime(); + } + sound = foolsSounds.get(BogoSorter.RND.nextInt(foolsSounds.size())); + } else { + sound = sortSound; + } + if (sound != null) { + Minecraft.getMinecraft().getSoundHandler().playSound(PositionedSoundRecord.getMasterRecord(sound, 1f)); + } } private final EntityPlayer player; @@ -52,7 +79,8 @@ public SortHandler(EntityPlayer entityPlayer, Container container, Int2ObjectMap this(entityPlayer, container, GuiSortingContext.getOrCreate(container), clientSortData); } - public SortHandler(EntityPlayer player, Container container, GuiSortingContext sortingContext, Int2ObjectMap clientSortData) { + public SortHandler(EntityPlayer player, Container container, GuiSortingContext sortingContext, + Int2ObjectMap clientSortData) { this.player = player; this.container = container; this.context = sortingContext; @@ -61,7 +89,7 @@ public SortHandler(EntityPlayer player, Container container, GuiSortingContext s int result; for (SortRule sortRule : itemSortRules) { result = sortRule instanceof ClientItemSortRule ? ((ClientItemSortRule) sortRule).compareServer(container1, container2) : - sortRule.compare(container1.getItemStack(), container2.getItemStack()); + sortRule.compare(container1.getItemStack(), container2.getItemStack()); if (result != 0) return result; } result = ItemCompareHelper.compareRegistryOrder(container1.getItemStack(), container2.getItemStack()); @@ -111,7 +139,8 @@ public void sortHorizontal(SlotGroup slotGroup) { continue; } - int max = Math.min(slot.bogo$getItemStackLimit(itemSortContainer.getItemStack()), slot.bogo$getMaxStackSize(itemSortContainer.getItemStack())); + int max = Math.min(slot.bogo$getItemStackLimit(itemSortContainer.getItemStack()), + slot.bogo$getMaxStackSize(itemSortContainer.getItemStack())); if (max <= 0) continue; slot.bogo$putStack(itemSortContainer.makeStack(max)); @@ -174,7 +203,8 @@ public void sortBogo(SlotGroup slotGroup) { public LinkedList gatherItems(SlotGroup slotGroup) { LinkedList list = new LinkedList<>(); - Object2ObjectOpenCustomHashMap items = new Object2ObjectOpenCustomHashMap<>(BogoSortAPI.ITEM_META_NBT_HASH_STRATEGY); + Object2ObjectOpenCustomHashMap items = new Object2ObjectOpenCustomHashMap<>( + BogoSortAPI.ITEM_META_NBT_HASH_STRATEGY); for (ISlot slot : getSortableSlots(slotGroup)) { ItemStack stack = slot.bogo$getStack(); if (!stack.isEmpty()) {