Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More anchor fixes & unmuffle sound when slider is set to 100% #4

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class SoundMuffler {

public static final String MODID = "extremesoundmuffler";
public static final String MODNAME = "Extreme Sound Muffler Legacy";
private static final Logger LOGGER = LogManager.getLogger();
public static final Logger LOGGER = LogManager.getLogger();

@SidedProxy(
serverSide = "com.leobeliik.extremesoundmuffler.CommonProxy",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public void initGui() {
11,
11,
() -> editTitle(Objects.requireNonNull(getAnchorByName(screenTitle))))
.setVisible(() -> !isMain() && anchor.getAnchorPos() != null)
.setVisible(() -> !isMain() && anchor != null && anchor.getAnchorPos() != null)
.setIcon(EDIT_ANCHOR));

addEditAnchorButtons();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public ESMButton setVisible(boolean state) {
}

public ESMButton setVisible(BooleanSupplier supplier) {
this.visible = supplier.getAsBoolean();
this.visibilitySupplier = supplier;
return this;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public ESMButton setIcon(@Nullable Icon icon, int xOffset, int yOffset, int widt
}

public boolean isVisible() {
return visible || (visibilitySupplier != null && visibilitySupplier.getAsBoolean());
return visible && (visibilitySupplier == null || visibilitySupplier.getAsBoolean());
}

public boolean isMouseOver(int mouseX, int mouseY) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,20 @@ private void toggleSound() {
} else {
anchor.removeSound(sound);
}
setMuffled(false);
} else {
boolean didMuffle = false;
if (MainScreen.isMain()) {
setSliderValue(Config.getDefaultMuteVolume());
muffledSounds.put(sound, sliderValue);
didMuffle = true;
} else if (anchor.getAnchorPos() != null) {
setSliderValue(Config.getDefaultMuteVolume());
anchor.addSound(sound, sliderValue);
didMuffle = true;
}
setMuffled(didMuffle);
}
setMuffled(!muffled);
}

private void changeSliderValue(float mouseX) {
Expand All @@ -154,21 +158,25 @@ private void changeSliderValue(float mouseX) {

private void setSliderValue(float value) {
sliderValue = MathHelper.clamp_float(value, 0.0F, 1F);
updateVolume();
if (sliderValue == 1F) {
toggleSound();
} else {
updateVolume();
}
}

@Override
protected void mouseDragged(Minecraft mc, int mouseX, int mouseY) {
if (isDragging) {
if (!muffled) toggleSound();
if (!muffled && canMuffle()) toggleSound();
changeSliderValue((float) mouseX);
}
super.mouseDragged(mc, mouseX, mouseY);
}

@Override
public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {
if (!visible || !enabled) return false;
if (!isVisible() || !enabled) return false;
for (ESMButton button : subButtons) {
if (!button.isMouseOver(mouseX, mouseY)) continue;
button.mousePressed(mc, mouseX, mouseY);
Expand All @@ -186,7 +194,7 @@ public boolean mousePressed(Minecraft mc, int mouseX, int mouseY) {

@Override
public void mouseReleased(int mouseX, int mouseY) {
if (!visible || !enabled) return;
if (!isVisible() || !enabled) return;

for (ESMButton button : subButtons) {
if (!button.isMouseOver(mouseX, mouseY)) continue;
Expand All @@ -210,4 +218,8 @@ public MuffledSlider setMuffled(boolean muffled) {
this.muffled = muffled;
return this;
}

private boolean canMuffle() {
return MainScreen.isMain() || anchor.getAnchorPos() != null;
}
}
28 changes: 12 additions & 16 deletions src/main/java/com/leobeliik/extremesoundmuffler/utils/Anchor.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,29 @@ public class Anchor {
private String dimension;
private int radius;
private SortedMap<String, Float> muffledSounds = new TreeMap<>();
private int x, y, z;
private Vec3 anchorPos;

public Anchor(int id, String name) {
this.id = id;
this.name = name;
}

public Anchor(int id, String name, int x, int y, int z, String dimension, int radius,
public Anchor(int id, String name, Vec3 anchorPos, String dimension, int radius,
SortedMap<String, Float> muffledSounds) {
this.id = id;
this.name = name;
this.x = x;
this.y = y;
this.z = z;
this.anchorPos = anchorPos;
this.dimension = dimension;
this.radius = radius;
this.muffledSounds = muffledSounds;
}

public Vec3 getAnchorPos() {
return Vec3.createVectorHelper(x, y, z);
return anchorPos;
}

private void setAnchorPos(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
anchorPos = Vec3.createVectorHelper(x, y, z);
}

public int getAnchorId() {
Expand Down Expand Up @@ -88,16 +84,16 @@ public void replaceSound(ResourceLocation sound, float volume) {
muffledSounds.replace(sound.toString(), volume);
}

public String getX() {
return String.valueOf(x);
public int getX() {
return anchorPos == null ? 0 : (int) anchorPos.xCoord;
}

public String getY() {
return String.valueOf(y);
public int getY() {
return anchorPos == null ? 0 : (int) anchorPos.yCoord;
}

public String getZ() {
return String.valueOf(z);
public int getZ() {
return anchorPos == null ? 0 : (int) anchorPos.zCoord;
}

public String getDimension() {
Expand All @@ -123,7 +119,7 @@ public void setAnchor() {

public void deleteAnchor() {
setName("Anchor: " + this.getAnchorId());
setAnchorPos(0, 0, 0);
anchorPos = null;
setDimension(null);
setRadius(0);
muffledSounds.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -16,15 +18,23 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.annotation.Nullable;

import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.Vec3;
import net.minecraft.world.storage.ThreadedFileIOBase;

import org.apache.commons.io.FileUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.leobeliik.extremesoundmuffler.Config;
import com.leobeliik.extremesoundmuffler.SoundMuffler;
import com.leobeliik.extremesoundmuffler.interfaces.ISoundLists;

import cpw.mods.fml.common.FMLCommonHandler;
Expand Down Expand Up @@ -99,15 +109,13 @@ public static Anchor deserializeAnchor(NBTTagCompound nbt) {
muffledSounds.put(key, muffledNBT.getFloat(key));
}

if (!nbt.hasKey("POS")) {
if (!nbt.hasKey("X") || !nbt.hasKey("Y") || !nbt.hasKey("Z")) {
return new Anchor(nbt.getInteger("ID"), nbt.getString("NAME"));
} else {
return new Anchor(
nbt.getInteger("ID"),
nbt.getString("NAME"),
nbt.getInteger("X"),
nbt.getInteger("Y"),
nbt.getInteger("Z"),
Vec3.createVectorHelper(nbt.getInteger("X"), nbt.getInteger("Y"), nbt.getInteger("Z")),
nbt.getString("DIM"),
nbt.getInteger("RAD"),
muffledSounds);
Expand All @@ -134,23 +142,55 @@ private static Map<String, Float> loadMuffledMap() {
}

private static void saveAnchors() {
new File("ESM/", identifier).mkdirs();
try (Writer writer = new OutputStreamWriter(
new FileOutputStream("ESM/" + identifier + "/anchors.dat"),
StandardCharsets.UTF_8)) {
writer.write(gson.toJson(anchorList));
} catch (IOException ignored) {}
File file = new File("ESM/" + identifier, "anchor.dat");
NBTTagCompound anchorsNBT = new NBTTagCompound();
for (Anchor anchor : anchorList) {
anchorsNBT.setTag("Anchor" + anchor.getAnchorId(), serializeAnchor(anchor));
}
writeNBT(file, anchorsNBT);
}

private static List<Anchor> loadAnchors() {
try (InputStreamReader reader = new InputStreamReader(
new FileInputStream("ESM/" + identifier + "/anchors.dat"),
StandardCharsets.UTF_8)) {
return gson.fromJson(new JsonReader(reader), new TypeToken<List<Anchor>>() {}.getType());
} catch (JsonSyntaxException | IOException ignored) {
File file = new File("ESM/" + identifier, "anchor.dat");
NBTTagCompound anchorsNBT = readNBT(file);
if (anchorsNBT == null) {
return IntStream.range(0, 10)
.mapToObj(i -> new Anchor(i, "Anchor " + i))
.collect(Collectors.toList());
}

List<Anchor> temp = new ArrayList<>();
for (int i = 0; i < anchorsNBT.func_150296_c()
.size(); i++) {
temp.add(deserializeAnchor(anchorsNBT.getCompoundTag("Anchor" + i)));
}
return temp;
}

public static void writeNBT(File file, NBTTagCompound tag) {
ThreadedFileIOBase.threadedIOInstance.queueIO(() -> {
try (FileOutputStream stream = FileUtils.openOutputStream(file)) {
CompressedStreamTools.writeCompressed(tag, stream);
} catch (Exception ex) {
SoundMuffler.LOGGER.warn("Failed to save file: {}", file.getName(), ex);
}
return false;
});
}

@Nullable
public static NBTTagCompound readNBT(File file) {
if (!file.exists() || !file.isFile()) {
return null;
}
try (InputStream stream = FileUtils.openInputStream(file)) {
return CompressedStreamTools.readCompressed(stream);
} catch (Exception ex) {
try {
return CompressedStreamTools.read(file);
} catch (Exception ex1) {
return null;
}
}
}
}
4 changes: 2 additions & 2 deletions src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
{
"modid": "${modId}",
"name": "${modName}",
"description": "Muffle those nasty sounds",
"description": "Clientside mod that allows you to muffle (almost) any sound selectively, allowing you to choose the volume of the sound you want between 0% and 100%.",
"version": "${modVersion}",
"mcversion": "${minecraftVersion}",
"url": "",
"url": "https://www.curseforge.com/minecraft/mc-mods/extreme-sound-muffler-legacy",
"updateUrl": "",
"authorList": [
"LeoBeliik",
Expand Down