Caching Some Paths for Efficiency

This commit is contained in:
Kam committed 2025-02-18 04:57:17 -05:00
1 parent 79955f549e
commit efcdeecbf2
3 files changed
+328 -311

No files matched your search

@@ -7,84 +7,101 @@ import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.audio.SoundRegistry;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.StatCollector;
import noppes.npcs.client.controllers.MusicController;
import noppes.npcs.client.gui.util.*;
import noppes.npcs.entity.EntityNPCInterface;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollListener{
public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollListener {
private GuiCustomScroll scrollCategories;
private GuiCustomScroll scrollSounds;
private GuiCustomScroll scrollCategories;
private GuiCustomScroll scrollSounds;
private String selectedDomain;
public ResourceLocation selectedResource;
private String selectedDomain;
public ResourceLocation selectedResource;
private String catSearch = "";
private String soundSearch = "";
private HashMap<String,List<String>> domains = new HashMap<String,List<String>>();
// Map: domain -> list of sound paths
private HashMap<String, List<String>> domains = new HashMap<>();
public GuiSoundSelection(String sound){
drawDefaultBackground = false;
title = "";
setBackground("menubg.png");
xSize = 366;
ySize = 226;
// --- Cache static fields ---
private static final long CACHE_DURATION = 180000L; // 3 minutes in ms
private static long lastCacheTime = 0;
public static HashMap<String, List<String>> cachedDomains = new HashMap<>();
SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();
SoundRegistry registry = ReflectionHelper.getPrivateValue(SoundHandler.class, handler, 4);
public GuiSoundSelection(String sound) {
drawDefaultBackground = false;
title = "";
setBackground("menubg.png");
xSize = 366;
ySize = 226;
Set<ResourceLocation> set = registry.getKeys();
for(ResourceLocation location : set){
List<String> list = domains.get(location.getResourceDomain());
if(list == null)
domains.put(location.getResourceDomain(), list = new ArrayList<String>());
list.add(location.getResourcePath());
domains.put(location.getResourceDomain(), list);
}
if(sound != null && !sound.isEmpty()){
selectedResource = new ResourceLocation(sound);
selectedDomain = selectedResource.getResourceDomain();
if(!domains.containsKey(selectedDomain)) {
selectedDomain = null;
}
}
long now = System.currentTimeMillis();
// Use cached data if it's fresh
if (now - lastCacheTime < CACHE_DURATION && !cachedDomains.isEmpty()) {
domains.putAll(cachedDomains);
} else {
// Otherwise, load domains from the SoundRegistry
SoundHandler handler = Minecraft.getMinecraft().getSoundHandler();
SoundRegistry registry = ReflectionHelper.getPrivateValue(SoundHandler.class, handler, 4);
Set<ResourceLocation> set = registry.getKeys();
for (ResourceLocation location : set) {
List<String> list = domains.get(location.getResourceDomain());
if (list == null) {
domains.put(location.getResourceDomain(), list = new ArrayList<String>());
}
list.add(location.getResourcePath());
}
// Update the static cache
cachedDomains.clear();
cachedDomains.putAll(domains);
lastCacheTime = now;
}
if (sound != null && !sound.isEmpty()) {
selectedResource = new ResourceLocation(sound);
selectedDomain = selectedResource.getResourceDomain();
if (!domains.containsKey(selectedDomain)) {
selectedDomain = null;
}
}
}
@Override
public void initGui(){
super.initGui();
this.addButton(new GuiNpcButton(2, guiLeft + xSize - 45, guiTop + ySize - 35, 40, 20, "gui.done"));
this.addButton(new GuiNpcButton(1, guiLeft + 4, guiTop + ySize - 35, 70, 20, "gui.play"));
this.addButton(new GuiNpcButton(2, guiLeft + xSize - 45, guiTop + ySize - 35, 40, 20, "gui.done"));
this.addButton(new GuiNpcButton(1, guiLeft + 4, guiTop + ySize - 35, 70, 20, "gui.play"));
getButton(1).enabled = selectedResource != null;
if(scrollCategories == null){
scrollCategories = new GuiCustomScroll(this,0, 0);
scrollCategories.setSize(90, 163);
if (scrollCategories == null) {
scrollCategories = new GuiCustomScroll(this, 0);
scrollCategories.setSize(90, 163);
}
scrollCategories.setList(Lists.newArrayList(domains.keySet()));
if(selectedDomain != null) {
scrollCategories.setSelected(selectedDomain);
if (selectedDomain != null) {
scrollCategories.setSelected(selectedDomain);
}
scrollCategories.guiLeft = guiLeft + 4;
scrollCategories.guiTop = guiTop + 4;
this.addScroll(scrollCategories);
addTextField(new GuiNpcTextField(55, this, fontRendererObj, guiLeft + 4, guiTop + 169, 90, 20, catSearch));
if(scrollSounds == null){
scrollSounds = new GuiCustomScroll(this,1, 0);
scrollSounds.setSize(265, 163);
if (scrollSounds == null) {
scrollSounds = new GuiCustomScroll(this, 1);
scrollSounds.setSize(265, 163);
}
if(selectedDomain != null) {
scrollSounds.setList(domains.get(selectedDomain));
if (selectedDomain != null) {
scrollSounds.setList(domains.get(selectedDomain));
}
if(selectedResource != null) {
scrollSounds.setSelected(selectedResource.getResourcePath());
if (selectedResource != null) {
scrollSounds.setSelected(selectedResource.getResourcePath());
}
scrollSounds.guiLeft = guiLeft + 95;
scrollSounds.guiTop = guiTop + 4;
@@ -93,25 +110,24 @@ public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollL
addTextField(new GuiNpcTextField(66, this, fontRendererObj, guiLeft + 95, guiTop + 169, 265, 20, soundSearch));
}
@Override
protected void actionPerformed(GuiButton guibutton){
super.actionPerformed(guibutton);
if(guibutton.id == 1){
MusicController.Instance.stopMusic();
@Override
protected void actionPerformed(GuiButton guibutton){
super.actionPerformed(guibutton);
if (guibutton.id == 1) {
MusicController.Instance.stopMusic();
MusicController.Instance.playSound(selectedResource.toString(), (float)player.posX, (float)player.posY, (float)player.posZ);
}
if(guibutton.id == 2){
close();
if (guibutton.id == 2) {
close();
}
}
@Override
public void customScrollDoubleClicked(String selection, GuiCustomScroll scroll) {
if(selectedResource == null)
return;
close();
}
@Override
public void customScrollDoubleClicked(String selection, GuiCustomScroll scroll) {
if (selectedResource == null)
return;
close();
}
@Override
public void close() {
@@ -121,7 +137,7 @@ public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollL
@Override
public void customScrollClicked(int i, int j, int k, GuiCustomScroll guiCustomScroll) {
if(guiCustomScroll.id == 0){
if (guiCustomScroll.id == 0) {
selectedDomain = guiCustomScroll.getSelected();
selectedResource = null;
scrollSounds.selected = -1;
@@ -129,15 +145,14 @@ public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollL
getTextField(66).setText("");
soundSearch = "";
}
if(guiCustomScroll.id == 1){
if (guiCustomScroll.id == 1) {
selectedResource = new ResourceLocation(selectedDomain, guiCustomScroll.getSelected());
}
initGui();
}
@Override
public void keyTyped(char c, int i)
{
public void keyTyped(char c, int i) {
super.keyTyped(c, i);
if(getTextField(55) != null){
if(getTextField(55).isFocused()){
@@ -175,7 +190,6 @@ public class GuiSoundSelection extends SubGuiInterface implements ICustomScrollL
if(selectedDomain == null){
return new ArrayList<String>();
}
if(soundSearch.isEmpty()){
return new ArrayList<String>(this.domains.get(selectedDomain));
}