Skip to content

Commit

Permalink
allow null sort sound
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Aug 21, 2024
1 parent 9e52e6a commit ca22842
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.");

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,12 +35,37 @@ public class SortHandler {
public static final Map<EntityPlayer, List<NbtSortRule>> cacheNbtSortRules = new Object2ObjectOpenHashMap<>();
public static final AtomicReference<List<NbtSortRule>> 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<SoundEvent> 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<SoundEvent> 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;
Expand All @@ -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> clientSortData) {
public SortHandler(EntityPlayer player, Container container, GuiSortingContext sortingContext,
Int2ObjectMap<ClientSortData> clientSortData) {
this.player = player;
this.container = container;
this.context = sortingContext;
Expand All @@ -61,7 +89,7 @@ public SortHandler(EntityPlayer player, Container container, GuiSortingContext s
int result;
for (SortRule<ItemStack> 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());
Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -174,7 +203,8 @@ public void sortBogo(SlotGroup slotGroup) {

public LinkedList<ItemSortContainer> gatherItems(SlotGroup slotGroup) {
LinkedList<ItemSortContainer> list = new LinkedList<>();
Object2ObjectOpenCustomHashMap<ItemStack, ItemSortContainer> items = new Object2ObjectOpenCustomHashMap<>(BogoSortAPI.ITEM_META_NBT_HASH_STRATEGY);
Object2ObjectOpenCustomHashMap<ItemStack, ItemSortContainer> items = new Object2ObjectOpenCustomHashMap<>(
BogoSortAPI.ITEM_META_NBT_HASH_STRATEGY);
for (ISlot slot : getSortableSlots(slotGroup)) {
ItemStack stack = slot.bogo$getStack();
if (!stack.isEmpty()) {
Expand Down

0 comments on commit ca22842

Please sign in to comment.