Skip to content

Commit

Permalink
Limit attenuation on earmuffs to 95%
Browse files Browse the repository at this point in the history
  • Loading branch information
malte0811 committed Feb 4, 2024
1 parent cc25d2f commit 456323a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,21 @@ public ToolConfigBoolean(String name, int x, int y, boolean value)

public static class ToolConfigFloat extends ToolConfig
{
public float value;
public final float value;
public final float min;
public final float max;

public ToolConfigFloat(String name, int x, int y, float value)
{
this(name, x, y, value, 0, 1);
}

public ToolConfigFloat(String name, int x, int y, float value, float min, float max)
{
super(name, x, y);
this.value = value;
this.min = min;
this.max = max;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public void init()
ToolConfigFloat[] floatArray = tool.getFloatOptions(stack);
if(floatArray!=null)
for(ToolConfigFloat f : floatArray)
this.addRenderableWidget(new GuiSliderIE(leftPos+f.x, topPos+f.y, 80, tool.fomatConfigName(stack, f), f.value,
value -> sendChange(value, f.name, FloatTag::valueOf)));
this.addRenderableWidget(new GuiSliderIE(
leftPos+f.x, topPos+f.y, 80,
tool.fomatConfigName(stack, f), f.min, f.max,
f.value, value -> sendChange(value, f.name, FloatTag::valueOf)
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public class GuiSliderIE extends ExtendedSlider
{
private final FloatConsumer handler;

public GuiSliderIE(int x, int y, int width, String name, float value, FloatConsumer handler)
public GuiSliderIE(int x, int y, int width, String name, float min, float max, float value, FloatConsumer handler)
{
super(
x, y, width, 8,
Component.nullToEmpty(name+" "),
Component.nullToEmpty("%"),
0, 100, 100*value,
100*min, 100*max, 100*value,
1, 0, true
);
this.handler = handler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@

public class EarmuffsItem extends IEBaseItem implements DyeableLeatherItem, IConfigurableTool, IColouredItem
{
/**
* The minimum allowed volume multiplier, i.e. the strongest attenuation. Note that this has to be strictly
* positive: otherwise the vanilla sound system will not start any sounds while earmuffs are worn, so long-playing
* sounds will not return after the earmuffs are removed.
*/
public static final float MIN_MULTIPLIER = 0.05f;
public static final float MAX_REDUCTION = 1-MIN_MULTIPLIER;

public static ItemGetterList EARMUFF_GETTERS = new ItemGetterList(
entity -> {
ItemStack head = entity.getItemBySlot(EquipmentSlot.HEAD);
Expand Down Expand Up @@ -141,7 +149,9 @@ public ToolConfigBoolean[] getBooleanOptions(ItemStack stack)
@Override
public ToolConfigFloat[] getFloatOptions(ItemStack stack)
{
return new ToolConfigFloat[]{new ToolConfigFloat("reductionValue", 60, 20, 1-getVolumeMod(stack))};
return new ToolConfigFloat[]{
new ToolConfigFloat("reductionValue", 60, 20, MAX_REDUCTION-getVolumeMod(stack), 0, MAX_REDUCTION)
};
}

@Override
Expand All @@ -161,9 +171,9 @@ public String fomatConfigDescription(ItemStack stack, ToolConfig config)
@Override
public void applyConfigOption(ItemStack stack, String key, Object value)
{
if(value instanceof Boolean)
ItemNBTHelper.putBoolean(stack, "IE:Earmuffs:Cat_"+key, !(Boolean)value);
else if(value instanceof Float)
ItemNBTHelper.putFloat(stack, "IE:Earmuffs:Volume", 1-(Float)value);
if(value instanceof Boolean bool)
ItemNBTHelper.putBoolean(stack, "IE:Earmuffs:Cat_"+key, !bool);
else if(value instanceof Float flt)
ItemNBTHelper.putFloat(stack, "IE:Earmuffs:Volume", MAX_REDUCTION-flt);
}
}

0 comments on commit 456323a

Please sign in to comment.