Skip to content

Commit

Permalink
v1.2.0 update
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyBlack233 authored Apr 25, 2023
1 parent 37ce2ce commit 219f01f
Show file tree
Hide file tree
Showing 13 changed files with 123 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ minecraft_version=1.16.5
yarn_mappings=1.16.5+build.10
loader_version=0.14.9
# Mod Properties
mod_version=1.1.1
mod_version=1.2.0
maven_group=heyblack
archives_base_name=RepeaterSound
# Dependencies
Expand Down
34 changes: 26 additions & 8 deletions src/main/java/heyblack/repeatersound/RepeaterSound.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,54 @@ public class RepeaterSound implements ClientModInitializer
public static final Identifier REDSTONE_WIRE_CLICK = new Identifier("repeatersound:redstone_wire_click");
public static SoundEvent BLOCK_REDSTONE_WIRE_CLICK = new SoundEvent(REDSTONE_WIRE_CLICK);

public static final Identifier DAYLIGHT_DETECTOR_CLICK = new Identifier("repeatersound:daylight_detector_click");
public static SoundEvent BLOCK_DAYLIGHT_DETECTOR_CLICK = new SoundEvent(DAYLIGHT_DETECTOR_CLICK);

ConfigManager cfgManager = ConfigManager.getInstance();
static Config config;

@Override
public void onInitializeClient()
{
cfgManager.loadConfig();
config = cfgManager.loadConfig();

ClientCommandManager.DISPATCHER.register(ClientCommandManager.literal("repeatersound")
.then(ClientCommandManager.literal("setBasePitch")
.then(ClientCommandManager.argument("pitch", FloatArgumentType.floatArg())
.executes(ctx -> saveConfigPitch(FloatArgumentType.getFloat(ctx, "pitch")))))
.then(ClientCommandManager.literal("useRandomPitch")
.then(ClientCommandManager.argument("useRandom", BoolArgumentType.bool())
.executes(ctx -> saveConfigRandom(BoolArgumentType.getBool(ctx, "useRandom"))))));
.executes(ctx -> saveConfigRandom(BoolArgumentType.getBool(ctx, "useRandom")))))
.then(ClientCommandManager.literal("setVolume")
.then(ClientCommandManager.argument("volume", FloatArgumentType.floatArg())
.executes(ctx -> saveConfigVolume(FloatArgumentType.getFloat(ctx, "volume"))))));
}

public static Config getConfig()
{
return config;
}

public int saveConfigPitch(float pitch)
{
Config cfg = cfgManager.getConfigFromFile();
cfg.setBasePitch(pitch);
cfgManager.save(cfg);
config.basePitch = pitch;
cfgManager.save(config);

return 1;
}

public int saveConfigRandom(boolean random)
{
Config cfg = cfgManager.getConfigFromFile();
cfg.setRandomPitch(random);
cfgManager.save(cfg);
config.useRandomPitch = random;
cfgManager.save(config);

return 1;
}

public int saveConfigVolume(float volume)
{
config.volume = volume;
cfgManager.save(config);

return 1;
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/heyblack/repeatersound/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

public class Config
{
public float basePitch;
public boolean useRandomPitch;
public float basePitch = 0.5f;
public boolean useRandomPitch = false;
public float volume = 0.3f;

public void setDefault()
{
basePitch = 0.5f;
useRandomPitch = false;
volume = 0.3f;
}

public float getBasePitch()
Expand All @@ -30,4 +32,14 @@ public void setRandomPitch(boolean bl)
{
useRandomPitch = bl;
}

public float getVolume()
{
return volume;
}

public void setVolume(float v)
{
volume = v;
}
}
14 changes: 8 additions & 6 deletions src/main/java/heyblack/repeatersound/config/ConfigManager.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package heyblack.repeatersound.config;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import net.fabricmc.loader.api.FabricLoader;
import org.apache.logging.log4j.LogManager;
Expand All @@ -11,7 +12,7 @@
public class ConfigManager
{
Logger logger = LogManager.getLogger();
Config cfg = new Config();
Config config = new Config();
static Gson gson = new Gson();
public File cfgFile = FabricLoader.getInstance().getConfigDir().resolve("repeatersound.json5").toFile();

Expand All @@ -21,25 +22,26 @@ public static ConfigManager getInstance()
return instance;
}

public void loadConfig()
public Config loadConfig()
{
if(!cfgFile.exists())
{
try
{
cfgFile.createNewFile();
cfg.setDefault();
save(cfg);
config.setDefault();
save(config);

return;
return config;
}
catch (IOException e)
{
logger.error("[RepeaterSound] " + "error while creating config!");
throw new RuntimeException(e);
}
}
cfg = getConfigFromFile();
config = getConfigFromFile();
return config;
}

public void save(Config cfg)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package heyblack.repeatersound.mixin;

import heyblack.repeatersound.RepeaterSound;
import heyblack.repeatersound.config.Config;
import heyblack.repeatersound.config.ConfigManager;
import net.fabricmc.api.EnvType;
Expand All @@ -17,7 +18,9 @@
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.ModifyArgs;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.spongepowered.asm.mixin.injection.invoke.arg.Args;

import static net.minecraft.block.ComparatorBlock.MODE;

Expand All @@ -32,13 +35,16 @@ public void getState(BlockState s, World world, BlockPos pos, PlayerEntity playe
state = s;
}

@ModifyArg(method = "onUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;playSound(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/sound/SoundEvent;Lnet/minecraft/sound/SoundCategory;FF)V"), index = 5)
public float pitch(float f)
@ModifyArgs(method = "onUse", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;playSound(Lnet/minecraft/entity/player/PlayerEntity;Lnet/minecraft/util/math/BlockPos;Lnet/minecraft/sound/SoundEvent;Lnet/minecraft/sound/SoundCategory;FF)V"))
public void pitch(Args args)
{
Config config = ConfigManager.getInstance().getConfigFromFile();
Config config = RepeaterSound.getConfig();
float basePitch = config.getBasePitch();
return config.getRandomPitch() ?
float pitch = config.getRandomPitch() ?
(float) (basePitch + (Math.random() - 0.5) * 0.25) :
(state = state.cycle(MODE)).get(MODE) == ComparatorMode.SUBTRACT ? basePitch + 0.05f : basePitch;
float volume = config.getVolume();
args.set(5, pitch);
args.set(4, volume);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package heyblack.repeatersound.mixin;

import heyblack.repeatersound.RepeaterSound;
import heyblack.repeatersound.config.Config;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.BlockState;
import net.minecraft.block.DaylightDetectorBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Environment(value= EnvType.CLIENT)
@Mixin(DaylightDetectorBlock.class)
public class DaylightDetectorBlockMixin
{
@Shadow @Final public static BooleanProperty INVERTED;

@Inject(method = "onUse", at = @At("HEAD"))
public void playSound(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir)
{
Config config = RepeaterSound.getConfig();
float basePitch = config.getBasePitch();
float pitch = config.getRandomPitch() ?
(float) (basePitch + (Math.random() - 0.5) * 0.25) :
state.cycle(INVERTED).get(INVERTED) ? basePitch : basePitch + 0.05f;
float volume = config.getVolume();
world.playSound(player, pos, RepeaterSound.BLOCK_DAYLIGHT_DETECTOR_CLICK, SoundCategory.BLOCKS, volume, pitch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ protected static boolean isFullyConnected(BlockState state)
{
return false;
}
@Inject(at = @At(value = "RETURN", ordinal = 1), method = "onUse")
@Inject( method = "onUse", at = @At(value = "RETURN", ordinal = 1))
public void playSound(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir)
{
Config config = ConfigManager.getInstance().getConfigFromFile();
Config config = RepeaterSound.getConfig();
float basePitch = config.getBasePitch();
float pitch = config.getRandomPitch() ?
(float) (basePitch + (Math.random() - 0.5) * 0.25) :
this.isFullyConnected(state) ? basePitch : basePitch + 0.05f;
world.playSound(player, pos, RepeaterSound.BLOCK_REDSTONE_WIRE_CLICK, SoundCategory.BLOCKS, 0.3f, pitch);
float volume = config.getVolume();
world.playSound(player, pos, RepeaterSound.BLOCK_REDSTONE_WIRE_CLICK, SoundCategory.BLOCKS, volume, pitch);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
public class RepeaterBlockMixin
{
@Shadow @Final public static IntProperty DELAY;
@Inject(at = @At("TAIL"), method = "onUse")
@Inject(method = "onUse", at = @At("TAIL"))
public void playSound(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit, CallbackInfoReturnable<ActionResult> cir)
{
Config config = ConfigManager.getInstance().getConfigFromFile();
Config config = RepeaterSound.getConfig();
float basePitch = config.getBasePitch();
float pitch = config.getRandomPitch() ?
(float) (basePitch + (Math.random() - 0.5) * 0.25) :
(basePitch - 0.02f) + state.cycle(DELAY).get(DELAY) * 0.02f;
world.playSound(player, pos, RepeaterSound.BLOCK_REPEATER_CLICK, SoundCategory.BLOCKS, 0.3f, pitch);
float volume = config.getVolume();
world.playSound(player, pos, RepeaterSound.BLOCK_REPEATER_CLICK, SoundCategory.BLOCKS, volume, pitch);
}
}
5 changes: 3 additions & 2 deletions src/main/resources/RepeaterClickingSound.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
"package": "heyblack.repeatersound.mixin",
"compatibilityLevel": "JAVA_8",
"client": [
"ComparatorBlockMixin",
"DaylightDetectorBlockMixin",
"RedstoneWireBlockMixin",
"RepeaterBlockMixin",
"ComparatorBlockMixin"
"RepeaterBlockMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/repeatersound/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"subtitles.repeatersound.repeater_click": "Repeater clicks",
"subtitles.repeatersound.redstone_wire_click": "Redstone Wire clicks"
"subtitles.repeatersound.redstone_wire_click": "Redstone Wire clicks",
"subtitles.repeatersound.daylight_detector_click": "Daylight Detector clicks"
}
3 changes: 2 additions & 1 deletion src/main/resources/assets/repeatersound/lang/zh_cn.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"subtitles.repeatersound.repeater_click": "中继器:调节档位",
"subtitles.repeatersound.redstone_wire_click": "红石线:改变形状"
"subtitles.repeatersound.redstone_wire_click": "红石线:改变形状",
"subtitles.repeatersound.daylight_detector_click": "阳光探测器:转换模式"
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/repeatersound/sounds.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@
"sounds": [
"random/click"
]
},
"daylight_detector_click": {
"subtitle": "subtitles.repeatersound.daylight_detector_click",
"sounds": [
"random/click"
]
}
}
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"RepeaterClickingSound.mixins.json"
],
"depends": {
"fabricloader": ">=0.14.9",
"fabricloader": "*",
"fabric": "*",
"minecraft": ">=1.16"
}
}

0 comments on commit 219f01f

Please sign in to comment.