Skip to content

Commit

Permalink
Merge pull request #8 from RappyLabyAddons/feat/sounds
Browse files Browse the repository at this point in the history
Implement sound system
  • Loading branch information
RappyTV committed Jul 7, 2023
2 parents b96e130 + b3df98b commit 9b4acad
Show file tree
Hide file tree
Showing 23 changed files with 484 additions and 63 deletions.
7 changes: 4 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ labyMod {
addonInfo {
namespace = "toolwarn"
displayName = "Toolbreak Warning"
author = "RappyTV#6969"
author = "RappyTV"
description = "Stops you from using your currently used tool when its almost destroyed."
version = System.getenv().getOrDefault("VERSION", "1.1.0")
version = System.getenv().getOrDefault("VERSION", "1.2.0")
}

minecraft {
Expand All @@ -32,7 +32,8 @@ labyMod {
"1.18.2",
"1.19.2",
"1.19.3",
"1.19.4"
"1.19.4",
"1.20.1"
) { version, provider ->
configureRun(provider, version)
}
Expand Down
11 changes: 7 additions & 4 deletions core/src/main/java/com/rappytv/toolwarn/TbwAddon.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package com.rappytv.toolwarn;

import com.rappytv.toolwarn.commands.TbwCommand;
import com.rappytv.toolwarn.config.TbwConfiguration;
import com.rappytv.toolwarn.core.generated.DefaultReferenceStorage;
import com.rappytv.toolwarn.listener.GameTickListener;
import com.rappytv.toolwarn.util.ITbwSounds;
import net.labymod.api.addon.LabyAddon;
import net.labymod.api.models.addon.annotation.AddonMain;

@AddonMain
public class TbwAddon extends LabyAddon<TbwConfiguration> {

public static final String prefix = "§c§lTBW §8» §7";
private static TbwAddon instance;
private static ITbwSounds sounds;

@Override
protected void enable() {
instance = this;
sounds = ((DefaultReferenceStorage) this.referenceStorageAccessor()).iTbwSounds();
registerSettingCategory();

registerCommand(new TbwCommand(this));
registerListener(new GameTickListener(this));
}

public static TbwAddon get() {
return instance;
public static ITbwSounds getSounds() {
return sounds;
}

@Override
Expand Down
54 changes: 27 additions & 27 deletions core/src/main/java/com/rappytv/toolwarn/commands/TbwCommand.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.rappytv.toolwarn.commands;

import com.rappytv.toolwarn.TbwAddon;
import com.rappytv.toolwarn.TbwConfiguration;
import com.rappytv.toolwarn.config.TbwConfiguration;
import com.rappytv.toolwarn.util.ToolType;
import com.rappytv.toolwarn.util.Util;
import net.labymod.api.Laby;
import net.labymod.api.client.chat.command.Command;
import net.labymod.api.client.entity.player.ClientPlayer;
import net.labymod.api.client.world.item.ItemStack;
import net.labymod.api.util.I18n;

public class TbwCommand extends Command {

Expand All @@ -21,85 +22,84 @@ public TbwCommand(TbwAddon addon) {
@Override
public boolean execute(String prefix, String[] arguments) {
if(arguments.length < 1) {
Util.msg(Util.getTranslation("toolwarn.command.invalidSubcommand"), true);
return true;
Util.msg(I18n.translate("toolwarn.command.invalidSubcommand"), true);
} else if(arguments[0].equalsIgnoreCase("disable")) {
config.enabled().set(false);

Util.msg(Util.getTranslation("toolwarn.command.addonDisabled"), true);
Util.msg(I18n.translate("toolwarn.command.addonDisabled"), true);
} else if(arguments[0].equalsIgnoreCase("format")) {
if(arguments.length < 2) {
Util.msg(Util.getTranslation("toolwarn.command.onOff"), true);
Util.msg(I18n.translate("toolwarn.command.onOff"), true);
return true;
}

if(arguments[1].equalsIgnoreCase("on")) {
config.format().set(true);

Util.msg(Util.getTranslation("toolwarn.command.formattingEnabled"), true);
Util.msg(I18n.translate("toolwarn.command.formattingEnabled"), true);
} else if(arguments[1].equalsIgnoreCase("off")) {
config.format().set(false);

Util.msg(Util.getTranslation("toolwarn.command.formattingDisabled"), true);
Util.msg(I18n.translate("toolwarn.command.formattingDisabled"), true);
} else
Util.msg(Util.getTranslation("toolwarn.command.onOff"), true);
Util.msg(I18n.translate("toolwarn.command.onOff"), true);

} else if(arguments[0].equalsIgnoreCase("debug")) {
if(arguments.length < 2) {
Util.msg(Util.getTranslation("toolwarn.command.onOnceOff"), true);
Util.msg(I18n.translate("toolwarn.command.onOnceOff"), true);
return true;
}

if(arguments[1].equalsIgnoreCase("on")) {
config.debug().set(true);

Util.msg(Util.getTranslation("toolwarn.command.debugEnabled"), true);
Util.msg(I18n.translate("toolwarn.command.debugEnabled"), true);
} else if(arguments[1].equalsIgnoreCase("once")) {
ClientPlayer player = Laby.labyAPI().minecraft().getClientPlayer();
if(player == null) return true;
ItemStack itemStack = player.getMainHandItemStack();
if(itemStack == null) return true;
Util.msg(Util.getTranslation("toolwarn.command.debugOnce"), true);
Util.msg(I18n.translate("toolwarn.command.debugOnce"), true);
ToolType toolType = ToolType.getByItem(itemStack);

if(toolType == ToolType.None)
Util.msg(Util.getTranslation("toolwarn.command.noEventTriggered"), false);
Util.msg(I18n.translate("toolwarn.command.noEventTriggered"), false);
else {
int itemWarnInt = (toolType.getWarnPercentage(config) * itemStack.getMaximumDamage()) / 100;
int itemUsedInt = itemStack.getMaximumDamage() - itemStack.getCurrentDamageValue();

Util.msg(Util.getTranslation("toolwarn.messages.debug", toolType.displayName(), (config.format().get() ? Util.formatNumber(itemStack.getMaximumDamage()) : itemStack.getMaximumDamage()), (config.format().get() ? Util.formatNumber(itemUsedInt) : itemUsedInt), (config.format().get() ? Util.formatNumber(itemWarnInt) : itemWarnInt)), false);
Util.msg(I18n.translate("toolwarn.messages.debug", toolType.displayName(), (config.format().get() ? Util.formatNumber(itemStack.getMaximumDamage()) : itemStack.getMaximumDamage()), (config.format().get() ? Util.formatNumber(itemUsedInt) : itemUsedInt), (config.format().get() ? Util.formatNumber(itemWarnInt) : itemWarnInt)), false);
}
} else if(arguments[1].equalsIgnoreCase("off")) {
config.debug().set(false);
Util.msg(Util.getTranslation("toolwarn.command.debugDisabled"), true);
Util.msg(I18n.translate("toolwarn.command.debugDisabled"), true);
} else {
Util.msg(Util.getTranslation("toolwarn.command.onOnceOff"), true);
Util.msg(I18n.translate("toolwarn.command.onOnceOff"), true);
}
} else if(arguments[0].equalsIgnoreCase("config")) {
String yes = "§a" + Util.getTranslation("toolwarn.command.yes");
String no = "§c" + Util.getTranslation("toolwarn.command.no");
String yes = "§a" + I18n.translate("toolwarn.command.yes");
String no = "§c" + I18n.translate("toolwarn.command.no");

Util.msg(Util.getTranslation("toolwarn.command.config",
Util.getTranslation("toolwarn.settings.enabled.name"),
Util.msg(I18n.translate("toolwarn.command.config",
I18n.translate("toolwarn.settings.enabled.name"),
config.enabled().get() ? yes : no,
Util.getTranslation("toolwarn.settings.format.name"),
I18n.translate("toolwarn.settings.format.name"),
config.format().get() ? yes : no,
Util.getTranslation("toolwarn.settings.debug.name"),
I18n.translate("toolwarn.settings.debug.name"),
config.debug().get() ? yes : no,
Util.getTranslation("toolwarn.settings.lastHit.name"),
I18n.translate("toolwarn.settings.lastHit.name"),
config.lastHit().get() ? yes : no,
Util.getTranslation("toolwarn.settings.swordPercentage.name"),
I18n.translate("toolwarn.settings.swordPercentage.name"),
"§b" + config.swordPercentage().get() + "%",
Util.getTranslation("toolwarn.settings.pickaxePercentage.name"),
I18n.translate("toolwarn.settings.pickaxePercentage.name"),
"§b" + config.pickAxePercentage().get() + "%",
Util.getTranslation("toolwarn.settings.axePercentage.name"),
I18n.translate("toolwarn.settings.axePercentage.name"),
"§b" + config.axePercentage().get() + "%",
Util.getTranslation("toolwarn.settings.shovelPercentage.name"),
I18n.translate("toolwarn.settings.shovelPercentage.name"),
"§b" + config.shovelPercentage().get() + "%"
), false);
} else {
Util.msg(Util.getTranslation("toolwarn.command.invalidSubcommand"), true);
Util.msg(I18n.translate("toolwarn.command.invalidSubcommand"), true);
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.rappytv.toolwarn;
package com.rappytv.toolwarn.config;

import com.rappytv.toolwarn.config.subconfig.TbwSoundSubConfig;
import net.labymod.api.addon.AddonConfig;
import net.labymod.api.client.gui.screen.widget.widgets.input.SliderWidget.SliderSetting;
import net.labymod.api.client.gui.screen.widget.widgets.input.SwitchWidget.SwitchSetting;
import net.labymod.api.configuration.loader.annotation.ConfigName;
import net.labymod.api.configuration.loader.annotation.SpriteSlot;
import net.labymod.api.configuration.loader.annotation.SpriteTexture;
import net.labymod.api.configuration.loader.property.ConfigProperty;
import net.labymod.api.configuration.settings.annotation.SettingSection;

@ConfigName("settings")
@SpriteTexture(value = "settings")
Expand All @@ -15,19 +17,24 @@ public class TbwConfiguration extends AddonConfig {
@SwitchSetting
@SpriteSlot(size = 32)
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);
@SettingSection("general")
@SwitchSetting
@SpriteSlot(size = 32, x = 1)
private final ConfigProperty<Boolean> openChat = new ConfigProperty<>(true);
@SwitchSetting
@SpriteSlot(size = 32, x = 2)
private final ConfigProperty<Boolean> format = new ConfigProperty<>(true);
@SwitchSetting
@SpriteSlot(size = 32, y = 1)
private final ConfigProperty<Boolean> lastHit = new ConfigProperty<>(true);
@SwitchSetting
@SpriteSlot(size = 32, x = 3)
private final ConfigProperty<Boolean> debug = new ConfigProperty<>(false);

@SwitchSetting
@SpriteSlot(size = 32, y = 1)
private final ConfigProperty<Boolean> lastHit = new ConfigProperty<>(true);
@SettingSection("sounds")
private final TbwSoundSubConfig sounds = new TbwSoundSubConfig();

@SettingSection("tools")
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 3)
private final ConfigProperty<Integer> swordPercentage = new ConfigProperty<>(5);
Expand All @@ -51,13 +58,17 @@ public ConfigProperty<Boolean> openChat() {
public ConfigProperty<Boolean> format() {
return format;
}
public ConfigProperty<Boolean> lastHit() {
return lastHit;
}
public ConfigProperty<Boolean> debug() {
return debug;
}

public ConfigProperty<Boolean> lastHit() {
return lastHit;
public TbwSoundSubConfig sounds() {
return sounds;
}

public ConfigProperty<Integer> swordPercentage() {
return swordPercentage;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.rappytv.toolwarn.config.subconfig;

import com.rappytv.toolwarn.util.WarnSound;
import net.labymod.api.client.gui.screen.widget.widgets.input.dropdown.DropdownWidget.DropdownSetting;
import net.labymod.api.configuration.loader.Config;
import net.labymod.api.configuration.loader.annotation.ParentSwitch;
import net.labymod.api.configuration.loader.property.ConfigProperty;

public class TbwSoundSubConfig extends Config {

@ParentSwitch
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);
@DropdownSetting
private final ConfigProperty<WarnSound> warnSound = new ConfigProperty<>(WarnSound.PLING);
@DropdownSetting
private final ConfigProperty<WarnSound> lastHitSound = new ConfigProperty<>(WarnSound.ANVIL_USE);

public ConfigProperty<Boolean> enabled() {
return enabled;
}
public ConfigProperty<WarnSound> warnSound() {
return warnSound;
}
public ConfigProperty<WarnSound> lastHitSound() {
return lastHitSound;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.rappytv.toolwarn.listener;

import com.rappytv.toolwarn.TbwAddon;
import com.rappytv.toolwarn.TbwConfiguration;
import com.rappytv.toolwarn.config.TbwConfiguration;
import com.rappytv.toolwarn.util.ToolType;
import com.rappytv.toolwarn.util.Util;
import com.rappytv.toolwarn.util.WarnSound;
import net.labymod.api.Laby;
import net.labymod.api.client.entity.player.ClientPlayer;
import net.labymod.api.client.entity.player.GameMode;
Expand Down Expand Up @@ -46,19 +47,35 @@ public void toolUsed(ItemStack itemStack, ToolType toolType) {
int itemWarnInt = (toolType.getWarnPercentage(config) * itemStack.getMaximumDamage()) / 100;
int itemUsedInt = itemStack.getMaximumDamage() - itemStack.getCurrentDamageValue();
if(config.debug().get())
Util.msg(Util.getTranslation("toolwarn.messages.debug", toolType.displayName(), (config.format().get() ? Util.formatNumber(itemStack.getMaximumDamage()) : itemStack.getMaximumDamage()), (config.format().get() ? Util.formatNumber(itemUsedInt) : itemUsedInt), (config.format().get() ? Util.formatNumber(itemWarnInt) : itemWarnInt)), false);
Util.msg(I18n.translate("toolwarn.messages.debug", toolType.displayName(), (config.format().get() ? Util.formatNumber(itemStack.getMaximumDamage()) : itemStack.getMaximumDamage()), (config.format().get() ? Util.formatNumber(itemUsedInt) : itemUsedInt), (config.format().get() ? Util.formatNumber(itemWarnInt) : itemWarnInt)), false);

if(itemUsedInt == itemWarnInt) {
if(!warns.contains(itemStack)) {
if(this.config.openChat().get()) Laby.labyAPI().minecraft().openChat("");
Util.msg(I18n.getTranslation("toolwarn.messages.warning", toolType.getWarnPercentage(config)), true);
warns.add(itemStack);

if(config.sounds().enabled().get() && config.sounds().warnSound().get() != WarnSound.NONE) {
Laby.labyAPI().minecraft().sounds().playSound(
config.sounds().warnSound().get().getResourceLocation(),
1f,
1f
);
}
}
} else if(isLastHit(itemStack)) {
if(!warns.contains(itemStack)) {
if(this.config.openChat().get()) Laby.labyAPI().minecraft().openChat("");
Util.msg(Util.getTranslation("toolwarn.messages.lastHit"), true);
Util.msg(I18n.translate("toolwarn.messages.lastHit"), true);
warns.add(itemStack);

if(config.sounds().enabled().get() && config.sounds().lastHitSound().get() != WarnSound.NONE) {
Laby.labyAPI().minecraft().sounds().playSound(
config.sounds().lastHitSound().get().getResourceLocation(),
1f,
1f
);
}
}
} else {
warns.remove(itemStack);
Expand All @@ -67,6 +84,6 @@ public void toolUsed(ItemStack itemStack, ToolType toolType) {

public boolean isLastHit(ItemStack i) {
if (!addon.configuration().lastHit().get()) return false;
return (i.getMaximumDamage() - i.getCurrentDamageValue()) == 1;
return (i.getMaximumDamage() - i.getCurrentDamageValue()) <= 1;
}
}
14 changes: 14 additions & 0 deletions core/src/main/java/com/rappytv/toolwarn/util/ITbwSounds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.rappytv.toolwarn.util;

import net.labymod.api.client.resources.ResourceLocation;
import net.labymod.api.reference.annotation.Referenceable;
import org.jetbrains.annotations.NotNull;

@Referenceable
public interface ITbwSounds {

@NotNull ResourceLocation getPlingSound();
@NotNull ResourceLocation getLevelUpSound();
@NotNull ResourceLocation getGlassBreakSound();
@NotNull ResourceLocation getAnvilUseSound();
}
5 changes: 3 additions & 2 deletions core/src/main/java/com/rappytv/toolwarn/util/ToolType.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.rappytv.toolwarn.util;

import com.rappytv.toolwarn.TbwConfiguration;
import com.rappytv.toolwarn.config.TbwConfiguration;
import net.labymod.api.client.world.item.ItemStack;
import net.labymod.api.util.I18n;

public enum ToolType {
None, Sword, Pickaxe, Axe, Shovel;
Expand Down Expand Up @@ -30,6 +31,6 @@ public int getWarnPercentage(TbwConfiguration configuration) {
}

public String displayName() {
return Util.getTranslation("toolwarn.tooltype." + this.name().toLowerCase());
return I18n.translate("toolwarn.tooltype." + this.name().toLowerCase());
}
}
11 changes: 2 additions & 9 deletions core/src/main/java/com/rappytv/toolwarn/util/Util.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.rappytv.toolwarn.util;

import com.rappytv.toolwarn.TbwAddon;
import net.labymod.api.util.I18n;
import net.labymod.api.Laby;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
Expand All @@ -10,14 +10,7 @@
public class Util {

public static void msg(String text, boolean prefix) {
TbwAddon.get().displayMessage(prefix ? TbwAddon.prefix + text : text);
}

public static String getTranslation(String key, Object ...args) {
String translation = I18n.getTranslation(key, args);
if(translation == null)
return key;
return translation;
Laby.references().chatExecutor().displayClientMessage(prefix ? TbwAddon.prefix + text : text);
}

public static String formatNumber(int number) {
Expand Down
Loading

0 comments on commit 9b4acad

Please sign in to comment.