Skip to content

Commit

Permalink
Merge pull request #19 from RappyLabyAddons/feat/activity
Browse files Browse the repository at this point in the history
Actual complete overhaul
  • Loading branch information
RappyTV authored Mar 14, 2024
2 parents 3a5d3ee + c5c6689 commit 7acaa34
Show file tree
Hide file tree
Showing 28 changed files with 1,233 additions and 310 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ labyMod {
author = "RappyTV"
description = "Stops you from using your currently used tool when its almost destroyed."
minecraftVersion = "1.8<1.20.4"
version = System.getenv().getOrDefault("VERSION", "1.3.4")
version = System.getenv().getOrDefault("VERSION", "1.4.0")
}

minecraft {
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/com/rappytv/toolwarn/TbwAddon.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.rappytv.toolwarn.config.TbwConfiguration;
import com.rappytv.toolwarn.core.generated.DefaultReferenceStorage;
import com.rappytv.toolwarn.listener.ConfigMigrationListener;
import com.rappytv.toolwarn.listener.GameTickListener;
import com.rappytv.toolwarn.util.ITbwSounds;
import net.labymod.api.Laby;
Expand All @@ -19,10 +20,13 @@ public class TbwAddon extends LabyAddon<TbwConfiguration> {

public static Component prefix;
private static ITbwSounds sounds;
private static TbwAddon instance;

@Override
protected void preConfigurationLoad() {
Laby.references().revisionRegistry().register(new SimpleRevision("toolwarn", new SemanticVersion("1.4.3"), "2024-01-26"));
Laby.references().revisionRegistry().register(new SimpleRevision("toolwarn", new SemanticVersion("1.3.4"), "2024-01-26"));
Laby.references().revisionRegistry().register(new SimpleRevision("toolwarn", new SemanticVersion("1.4.0"), "2024-03-14"));
registerListener(new ConfigMigrationListener());
}

@Override
Expand All @@ -32,10 +36,15 @@ protected void enable() {
.append(Component.text("» ", NamedTextColor.DARK_GRAY));
sounds = ((DefaultReferenceStorage) this.referenceStorageAccessor()).iTbwSounds();
registerSettingCategory();
instance = this;

registerListener(new GameTickListener(this));
}

public static TbwAddon get() {
return instance;
}

public static ITbwSounds getSounds() {
return sounds;
}
Expand Down
105 changes: 30 additions & 75 deletions core/src/main/java/com/rappytv/toolwarn/config/TbwConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,105 +1,60 @@
package com.rappytv.toolwarn.config;

import com.rappytv.toolwarn.config.subconfig.TbwSoundSubConfig;
import com.rappytv.toolwarn.ui.ToolConfigActivity;
import com.rappytv.toolwarn.util.WarnTool;
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.activity.Activity;
import net.labymod.api.client.gui.screen.widget.widgets.activity.settings.ActivitySettingWidget.ActivitySetting;
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.Exclude;
import net.labymod.api.configuration.loader.annotation.IntroducedIn;
import net.labymod.api.configuration.loader.annotation.SpriteSlot;
import net.labymod.api.configuration.loader.annotation.SpriteTexture;
import net.labymod.api.configuration.loader.annotation.VersionCompatibility;
import net.labymod.api.configuration.loader.property.ConfigProperty;
import net.labymod.api.configuration.settings.annotation.SettingSection;
import net.labymod.api.util.MethodOrder;
import java.util.ArrayList;
import java.util.List;

@ConfigName("settings")
@SpriteTexture(value = "settings")
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 = 3)
private final ConfigProperty<Boolean> lastHit = new ConfigProperty<>(true);
private final ConfigProperty<Boolean> enabled = new ConfigProperty<>(true);

@SettingSection("sounds")
@SpriteSlot(size = 32, y = 1)
private final TbwSoundSubConfig sounds = new TbwSoundSubConfig();
@Exclude
private final List<WarnTool> tools = new ArrayList<>();

@SettingSection("tools")
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 2)
private final ConfigProperty<Integer> swordPercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 2, x = 1)
private final ConfigProperty<Integer> pickaxePercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 2, x = 2)
private final ConfigProperty<Integer> axePercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 2, x = 3)
private final ConfigProperty<Integer> shovelPercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 3)
@VersionCompatibility("1.14<*")
@IntroducedIn(namespace = "globaltags", value = "1.4.3")
private final ConfigProperty<Integer> crossbowPercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 3, x = 1)
@IntroducedIn(namespace = "globaltags", value = "1.4.3")
private final ConfigProperty<Integer> lighterPercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 3, x = 2)
@IntroducedIn(namespace = "globaltags", value = "1.4.3")
private final ConfigProperty<Integer> shearsPercentage = new ConfigProperty<>(5);
@SliderSetting(steps = 1, min = 1, max = 25)
@SpriteSlot(size = 32, y = 3, x = 3)
@VersionCompatibility("1.13<*")
@IntroducedIn(namespace = "globaltags", value = "1.4.3")
private final ConfigProperty<Integer> tridentPercentage = new ConfigProperty<>(5);
@IntroducedIn(namespace = "toolwarn", value = "1.4.0")
@SpriteSlot(size = 32, x = 1)
@MethodOrder(after = "enabled")
@ActivitySetting
public Activity toolConfig() {
return new ToolConfigActivity();
}

@Override
public ConfigProperty<Boolean> enabled() {
return enabled;
}
public ConfigProperty<Boolean> openChat() {
return openChat;
}
public ConfigProperty<Boolean> lastHit() {
return lastHit;
}

public TbwSoundSubConfig sounds() {
return sounds;
public List<WarnTool> getTools() {
return tools;
}

public ConfigProperty<Integer> swordPercentage() {
return swordPercentage;
}
public ConfigProperty<Integer> pickAxePercentage() {
return pickaxePercentage;
}
public ConfigProperty<Integer> axePercentage() {
return axePercentage;
}
public ConfigProperty<Integer> shovelPercentage() {
return shovelPercentage;
}
public ConfigProperty<Integer> crossbowPercentage() {
return crossbowPercentage;
public void removeInvalidTools() {
this.tools.removeIf(entry ->
entry.getWarnAt() < 1
|| entry.getWarnAt() > 25
|| entry.getType() == null
);
}
public ConfigProperty<Integer> lighterPercentage() {
return lighterPercentage;
}
public ConfigProperty<Integer> shearsPercentage() {
return shearsPercentage;
}
public ConfigProperty<Integer> tridentPercentage() {
return tridentPercentage;

@Override
public int getConfigVersion() {
return 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.rappytv.toolwarn.listener;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rappytv.toolwarn.config.TbwConfiguration;
import com.rappytv.toolwarn.util.WarnSound;
import com.rappytv.toolwarn.util.WarnTool;
import com.rappytv.toolwarn.util.WarnTool.Type;
import net.labymod.api.configuration.loader.Config;
import net.labymod.api.event.Subscribe;
import net.labymod.api.event.labymod.config.ConfigurationVersionUpdateEvent;

@SuppressWarnings("FieldCanBeLocal")
public class ConfigMigrationListener {

private final Gson gson = new Gson();
private final int defaultPercentage = 5;

@Subscribe
public void onConfigVersionUpdate(ConfigurationVersionUpdateEvent event) {
Class<? extends Config> configClass = event.getConfigClass();
int usedVersion = event.getUsedVersion();

if(configClass == TbwConfiguration.class) {
if(usedVersion == 1) migrateFromOne(event);
}
}

private void migrateFromOne(ConfigurationVersionUpdateEvent event) {
JsonObject config = event.getJsonObject();
if(!config.has("sounds")) return;

JsonObject sounds = config.get("sounds").getAsJsonObject();

WarnSound warnSound = WarnSound.NONE;
WarnSound lastHitSound = WarnSound.NONE;
boolean openChat = true;
boolean lastHitWarn = true;
int sword = defaultPercentage;
int pickaxe = defaultPercentage;
int axe = defaultPercentage;
int shovel = defaultPercentage;
int crossbow = defaultPercentage;
int lighter = defaultPercentage;
int shears = defaultPercentage;
int trident = defaultPercentage;

try {
warnSound = WarnSound.valueOf(sounds.get("warnSound").getAsString());
lastHitSound = WarnSound.valueOf(sounds.get("lastHitSound").getAsString());
openChat = config.get("openChat").getAsBoolean();
lastHitWarn = config.get("lastHit").getAsBoolean();
sword = config.get("swordPercentage").getAsInt();
pickaxe = config.get("pickaxePercentage").getAsInt();
axe = config.get("axePercentage").getAsInt();
shovel = config.get("shovelPercentage").getAsInt();
crossbow = config.get("crossbowPercentage").getAsInt();
lighter = config.get("lighterPercentage").getAsInt();
shears = config.get("shearsPercentage").getAsInt();
trident = config.get("tridentPercentage").getAsInt();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}

JsonArray tools = new JsonArray();
tools.add(gson.toJsonTree(new WarnTool(Type.SWORD, warnSound, lastHitSound, sword, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.PICKAXE, warnSound, lastHitSound, pickaxe, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.AXE, warnSound, lastHitSound, axe, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.SHOVEL, warnSound, lastHitSound, shovel, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.CROSSBOW, warnSound, lastHitSound, crossbow, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.LIGHTER, warnSound, lastHitSound, lighter, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.SHEARS, warnSound, lastHitSound, shears, openChat, lastHitWarn)));
tools.add(gson.toJsonTree(new WarnTool(Type.TRIDENT, warnSound, lastHitSound, trident, openChat, lastHitWarn)));

config.add("tools", tools);
event.setJsonObject(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import com.rappytv.toolwarn.TbwAddon;
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 com.rappytv.toolwarn.util.WarnTool;
import com.rappytv.toolwarn.util.WarnTool.Type;
import net.labymod.api.Laby;
import net.labymod.api.client.component.Component;
import net.labymod.api.client.component.format.NamedTextColor;
Expand Down Expand Up @@ -38,51 +39,56 @@ public void onTick(GameTickEvent event) {
if(itemStack.getMaximumDamage() == 0) return;
if(player.gameMode() != GameMode.SURVIVAL && player.gameMode() != GameMode.ADVENTURE) return;

toolUsed(itemStack, ToolType.getByItem(itemStack));
checkForWarn(itemStack, Type.getByItem(itemStack));
}

public void toolUsed(ItemStack itemStack, ToolType toolType) {
if(toolType == ToolType.None) return;
private void checkForWarn(ItemStack itemStack, WarnTool.Type type) {
if(type == Type.NONE) return;
if(Laby.labyAPI().minecraft().minecraftWindow().isScreenOpened()) return;

int itemWarnInt = (toolType.getWarnPercentage(config) * itemStack.getMaximumDamage()) / 100;
int itemUsedInt = itemStack.getMaximumDamage() - itemStack.getCurrentDamageValue();

if(itemUsedInt == itemWarnInt) {
if(!warns.contains(itemStack)) {
if(this.config.openChat().get()) Laby.labyAPI().minecraft().openChat("");
Util.msg(Component.translatable("toolwarn.messages.warning", NamedTextColor.RED, Component.text(toolType.getWarnPercentage(config))), true);
warns.add(itemStack);
for(WarnTool tool : config.getTools()) {
if(tool.getType() != type) continue;
int itemWarnInt = (tool.getWarnAt() * itemStack.getMaximumDamage()) / 100;

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(Component.translatable("toolwarn.messages.lastHit", NamedTextColor.RED), true);
warns.add(itemStack);
if(itemUsedInt == itemWarnInt) {
if(!warns.contains(itemStack)) {
if(tool.openChat()) Laby.labyAPI().minecraft().openChat("");
Util.msg(Component.translatable(
"toolwarn.messages.warning",
NamedTextColor.RED,
Component.text(tool.getWarnAt())
), 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
if(tool.getSound() != WarnSound.NONE) {
Laby.labyAPI().minecraft().sounds().playSound(
tool.getSound().getResourceLocation(),
1f,
1f
);
}
}
} else if(tool.lastHitWarn() && itemUsedInt <= 1) {
if(!warns.contains(itemStack)) {
if(tool.openChat()) Laby.labyAPI().minecraft().openChat("");
Util.msg(
Component.translatable("toolwarn.messages.lastHit", NamedTextColor.RED),
true
);
warns.add(itemStack);
if(tool.getLastSound() != WarnSound.NONE) {
Laby.labyAPI().minecraft().sounds().playSound(
tool.getLastSound().getResourceLocation(),
1f,
1f
);
}
}
} else {
warns.remove(itemStack);
}
} else {
warns.remove(itemStack);
}
}

public boolean isLastHit(ItemStack i) {
if (!addon.configuration().lastHit().get()) return false;
return (i.getMaximumDamage() - i.getCurrentDamageValue()) <= 1;
}
}
Loading

0 comments on commit 7acaa34

Please sign in to comment.