Skip to content

Commit

Permalink
feat(tsl): add notificationDelay preference
Browse files Browse the repository at this point in the history
- Added notificationDelay preference to control sleep time between event handlings
- Fixed a bug not reading enum preferences after correcting them
  • Loading branch information
iGoodie committed Oct 24, 2019
1 parent 500eee4 commit 7d4a2b3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ org.gradle.daemon=false

mod_id=twitchspawn
mod_group=net.programmer.igoodie
mod_version=1.4.5
mod_version=1.4.6

minecraft_version=1.14.4
forge_version=28.0.83
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static PreferencesConfig create(File file) {
preferencesConfig.messageDisplay = getEnum(config, "messageDisplay", MessageDisplay.class);
preferencesConfig.notificationVolume = config.get("notificationVolume");
preferencesConfig.notificationPitch = config.get("notificationPitch");
preferencesConfig.notificationDelay = config.getInt("notificationDelay");

config.close();

Expand Down Expand Up @@ -106,19 +107,23 @@ private static ConfigSpec getSpecs() {
if (!(rawValue instanceof Number))
return false;
double value = ((Number) rawValue).doubleValue();
return value == -1.0 || 0.0 <= value && value <= 1.0;
return (value == -1.0) || (0.0 <= value && value <= 1.0);
});
spec.defineInRange("notificationDelay", 5000, 0, Integer.MAX_VALUE);

return spec;
}

private static <T extends Enum<T>> T getEnum(CommentedFileConfig config, String path, Class<T> enumClass) {
Object value = config.get(path);

if (!(value instanceof String))
return null;
if (value instanceof String)
return Enum.valueOf(enumClass, ((String) value).toUpperCase());

return Enum.valueOf(enumClass, ((String) value).toUpperCase());
if (enumClass.isInstance(value))
return enumClass.cast(value);

return null;
}

private static <T extends Enum<T>> void defineEnum(ConfigSpec spec, String path, T defaultValue, Class<T> enumClass) {
Expand Down Expand Up @@ -154,5 +159,6 @@ private static String defaultScript() {

public double notificationVolume;
public double notificationPitch;
public int notificationDelay;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.programmer.igoodie.twitchspawn.tslanguage;

import net.programmer.igoodie.twitchspawn.TwitchSpawn;
import net.programmer.igoodie.twitchspawn.configuration.ConfigManager;
import net.programmer.igoodie.twitchspawn.tslanguage.event.TSLEvent;
import net.programmer.igoodie.twitchspawn.tslanguage.event.TSLEventPair;
import net.programmer.igoodie.twitchspawn.tslanguage.keyword.TSLEventKeyword;
Expand All @@ -13,8 +14,6 @@

public class TSLRulesetCollection {

private static long TITLE_DURATION = 5 * 1000; // milliseconds

private TSLRuleset defaultRuleset;
private Map<String, TSLRuleset> streamerRulesets; // Maps lowercase nicks to TSLTree
private Map<String, TimeTaskQueue> eventQueues; // Maps lowercase nicks to TimeTaskQueue
Expand Down Expand Up @@ -91,7 +90,7 @@ public TimeTaskQueue getQueue(String streamerNick) {
TimeTaskQueue queue = eventQueues.get(streamerNick.toLowerCase());

if (queue == null) { // Lazy init
queue = new TimeTaskQueue(TITLE_DURATION);
queue = new TimeTaskQueue(ConfigManager.PREFERENCES.notificationDelay);
eventQueues.put(streamerNick.toLowerCase(), queue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,13 @@ protected void notifyPlayer(ServerPlayerEntity player, String title, String subt
if (ConfigManager.PREFERENCES.messageDisplay == PreferencesConfig.MessageDisplay.TITLES) {
// Form title and subtitle packets
STitlePacket packet = new STitlePacket(STitlePacket.Type.TITLE, text,
DEFAULT_FADE_IN_TICKS, DEFAULT_STAY_TICKS, DEFAULT_FADE_OUT_TICKS);
(int) (ConfigManager.PREFERENCES.notificationDelay * 0.1f / 50), // 10
(int) (ConfigManager.PREFERENCES.notificationDelay * 0.7f / 50), // 70
(int) (ConfigManager.PREFERENCES.notificationDelay * 0.2f / 50)); // 20
STitlePacket subtitlePacket = new STitlePacket(STitlePacket.Type.SUBTITLE, subtext,
DEFAULT_FADE_IN_TICKS, DEFAULT_STAY_TICKS, DEFAULT_FADE_OUT_TICKS);
(int) (ConfigManager.PREFERENCES.notificationDelay * 0.1f / 50), // 10
(int) (ConfigManager.PREFERENCES.notificationDelay * 0.7f / 50), // 70
(int) (ConfigManager.PREFERENCES.notificationDelay * 0.2f / 50)); // 20

// Send them over
player.connection.sendPacket(packet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ notificationVolume = 1.0
# Min: 0.0 - Max: 1.0
# Alternatively -1.0 for random pitch each time
notificationPitch = 1.0

# Delay between each event to be handled
# Unit of it is milliseconds (5000 milliseconds = 5 seconds)
# Min: 0
notificationDelay = 5000

0 comments on commit 7d4a2b3

Please sign in to comment.