You've already forked Arcturus-Morningstar-Extended
mirror of
https://github.com/duckietm/Arcturus-Morningstar-Extended.git
synced 2026-06-19 15:06:19 +00:00
fix(wired): bound condition inputs
This commit is contained in:
+11
-8
@@ -59,16 +59,14 @@ public class WiredConditionHabboCount extends InteractionWiredCondition {
|
|||||||
|
|
||||||
if (wiredData.startsWith("{")) {
|
if (wiredData.startsWith("{")) {
|
||||||
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
||||||
this.lowerLimit = data.lowerLimit;
|
this.applyLimits(data.lowerLimit, data.upperLimit);
|
||||||
this.upperLimit = data.upperLimit;
|
this.userSource = WiredConditionInputGuard.normalizeUserSource(data.userSource);
|
||||||
this.userSource = data.userSource;
|
|
||||||
} else {
|
} else {
|
||||||
String[] data = wiredData.split(":");
|
String[] data = wiredData.split(":");
|
||||||
|
|
||||||
if (data.length >= 2) {
|
if (data.length >= 2) {
|
||||||
try {
|
try {
|
||||||
this.lowerLimit = Integer.parseInt(data[0].trim());
|
this.applyLimits(Integer.parseInt(data[0].trim()), Integer.parseInt(data[1].trim()));
|
||||||
this.upperLimit = Integer.parseInt(data[1].trim());
|
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
// malformed legacy data — keep the constructed defaults
|
// malformed legacy data — keep the constructed defaults
|
||||||
}
|
}
|
||||||
@@ -110,14 +108,19 @@ public class WiredConditionHabboCount extends InteractionWiredCondition {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveData(WiredSettings settings) {
|
public boolean saveData(WiredSettings settings) {
|
||||||
if(settings.getIntParams().length < 2) return false;
|
if(settings.getIntParams().length < 2) return false;
|
||||||
this.lowerLimit = settings.getIntParams()[0];
|
|
||||||
this.upperLimit = settings.getIntParams()[1];
|
|
||||||
int[] params = settings.getIntParams();
|
int[] params = settings.getIntParams();
|
||||||
this.userSource = (params.length > 2) ? params[2] : WiredSourceUtil.SOURCE_TRIGGER;
|
this.applyLimits(params[0], params[1]);
|
||||||
|
this.userSource = (params.length > 2) ? WiredConditionInputGuard.normalizeUserSource(params[2]) : WiredSourceUtil.SOURCE_TRIGGER;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void applyLimits(int lowerLimit, int upperLimit) {
|
||||||
|
int[] limits = WiredConditionInputGuard.normalizeUserCountRange(lowerLimit, upperLimit);
|
||||||
|
this.lowerLimit = limits[0];
|
||||||
|
this.upperLimit = limits[1];
|
||||||
|
}
|
||||||
|
|
||||||
static class JsonData {
|
static class JsonData {
|
||||||
int lowerLimit;
|
int lowerLimit;
|
||||||
int upperLimit;
|
int upperLimit;
|
||||||
|
|||||||
+57
@@ -0,0 +1,57 @@
|
|||||||
|
package com.eu.habbo.habbohotel.items.interactions.wired.conditions;
|
||||||
|
|
||||||
|
import com.eu.habbo.habbohotel.games.GameTeamColors;
|
||||||
|
import com.eu.habbo.habbohotel.wired.core.WiredSourceUtil;
|
||||||
|
|
||||||
|
public final class WiredConditionInputGuard {
|
||||||
|
public static final int MAX_USER_COUNT_LIMIT = 1000;
|
||||||
|
public static final int MAX_TIMER_CYCLES = 24 * 60 * 60 * 2;
|
||||||
|
|
||||||
|
private WiredConditionInputGuard() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GameTeamColors normalizeTeamColor(GameTeamColors value, GameTeamColors fallback) {
|
||||||
|
return (value != null) ? value : fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GameTeamColors normalizeTeamColorType(int value, GameTeamColors fallback) {
|
||||||
|
for (GameTeamColors color : GameTeamColors.values()) {
|
||||||
|
if (color.type == value) {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fallback;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int normalizeUserSource(int value) {
|
||||||
|
return WiredSourceUtil.isDefaultUserSource(value) ? value : WiredSourceUtil.SOURCE_TRIGGER;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int normalizeTimerCycles(int value) {
|
||||||
|
if (value < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.min(value, MAX_TIMER_CYCLES);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int[] normalizeUserCountRange(int lowerLimit, int upperLimit) {
|
||||||
|
int lower = clampUserCount(lowerLimit);
|
||||||
|
int upper = clampUserCount(upperLimit);
|
||||||
|
|
||||||
|
if (lower > upper) {
|
||||||
|
return new int[]{upper, lower};
|
||||||
|
}
|
||||||
|
|
||||||
|
return new int[]{lower, upper};
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int clampUserCount(int value) {
|
||||||
|
if (value < 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Math.min(value, MAX_USER_COUNT_LIMIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
+3
-3
@@ -52,10 +52,10 @@ public class WiredConditionLessTimeElapsed extends InteractionWiredCondition {
|
|||||||
try {
|
try {
|
||||||
if (wiredData.startsWith("{")) {
|
if (wiredData.startsWith("{")) {
|
||||||
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
||||||
this.cycles = data.cycles;
|
this.cycles = WiredConditionInputGuard.normalizeTimerCycles(data.cycles);
|
||||||
} else {
|
} else {
|
||||||
if (!wiredData.equals(""))
|
if (!wiredData.equals(""))
|
||||||
this.cycles = Integer.parseInt(wiredData);
|
this.cycles = WiredConditionInputGuard.normalizeTimerCycles(Integer.parseInt(wiredData));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ public class WiredConditionLessTimeElapsed extends InteractionWiredCondition {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveData(WiredSettings settings) {
|
public boolean saveData(WiredSettings settings) {
|
||||||
if(settings.getIntParams().length < 1) return false;
|
if(settings.getIntParams().length < 1) return false;
|
||||||
this.cycles = settings.getIntParams()[0];
|
this.cycles = WiredConditionInputGuard.normalizeTimerCycles(settings.getIntParams()[0]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+3
-3
@@ -52,10 +52,10 @@ public class WiredConditionMoreTimeElapsed extends InteractionWiredCondition {
|
|||||||
try {
|
try {
|
||||||
if (wiredData.startsWith("{")) {
|
if (wiredData.startsWith("{")) {
|
||||||
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
||||||
this.cycles = data.cycles;
|
this.cycles = WiredConditionInputGuard.normalizeTimerCycles(data.cycles);
|
||||||
} else {
|
} else {
|
||||||
if (!wiredData.equals(""))
|
if (!wiredData.equals(""))
|
||||||
this.cycles = Integer.parseInt(wiredData);
|
this.cycles = WiredConditionInputGuard.normalizeTimerCycles(Integer.parseInt(wiredData));
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
}
|
}
|
||||||
@@ -90,7 +90,7 @@ public class WiredConditionMoreTimeElapsed extends InteractionWiredCondition {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveData(WiredSettings settings) {
|
public boolean saveData(WiredSettings settings) {
|
||||||
if(settings.getIntParams().length < 1) return false;
|
if(settings.getIntParams().length < 1) return false;
|
||||||
this.cycles = settings.getIntParams()[0];
|
this.cycles = WiredConditionInputGuard.normalizeTimerCycles(settings.getIntParams()[0]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-10
@@ -59,15 +59,13 @@ public class WiredConditionNotHabboCount extends InteractionWiredCondition {
|
|||||||
|
|
||||||
if (wiredData.startsWith("{")) {
|
if (wiredData.startsWith("{")) {
|
||||||
WiredConditionHabboCount.JsonData data = WiredManager.getGson().fromJson(wiredData, WiredConditionHabboCount.JsonData.class);
|
WiredConditionHabboCount.JsonData data = WiredManager.getGson().fromJson(wiredData, WiredConditionHabboCount.JsonData.class);
|
||||||
this.lowerLimit = data.lowerLimit;
|
this.applyLimits(data.lowerLimit, data.upperLimit);
|
||||||
this.upperLimit = data.upperLimit;
|
this.userSource = WiredConditionInputGuard.normalizeUserSource(data.userSource);
|
||||||
this.userSource = data.userSource;
|
|
||||||
} else {
|
} else {
|
||||||
String[] data = wiredData.split(":");
|
String[] data = wiredData.split(":");
|
||||||
if (data.length >= 2) {
|
if (data.length >= 2) {
|
||||||
try {
|
try {
|
||||||
this.lowerLimit = Integer.parseInt(data[0].trim());
|
this.applyLimits(Integer.parseInt(data[0].trim()), Integer.parseInt(data[1].trim()));
|
||||||
this.upperLimit = Integer.parseInt(data[1].trim());
|
|
||||||
} catch (NumberFormatException ignored) {
|
} catch (NumberFormatException ignored) {
|
||||||
// malformed legacy data — keep the constructed defaults
|
// malformed legacy data — keep the constructed defaults
|
||||||
}
|
}
|
||||||
@@ -78,8 +76,8 @@ public class WiredConditionNotHabboCount extends InteractionWiredCondition {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onPickUp() {
|
public void onPickUp() {
|
||||||
this.upperLimit = 0;
|
this.lowerLimit = 10;
|
||||||
this.lowerLimit = 20;
|
this.upperLimit = 20;
|
||||||
this.userSource = WiredSourceUtil.SOURCE_TRIGGER;
|
this.userSource = WiredSourceUtil.SOURCE_TRIGGER;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,14 +107,19 @@ public class WiredConditionNotHabboCount extends InteractionWiredCondition {
|
|||||||
@Override
|
@Override
|
||||||
public boolean saveData(WiredSettings settings) {
|
public boolean saveData(WiredSettings settings) {
|
||||||
if(settings.getIntParams().length < 2) return false;
|
if(settings.getIntParams().length < 2) return false;
|
||||||
this.lowerLimit = settings.getIntParams()[0];
|
|
||||||
this.upperLimit = settings.getIntParams()[1];
|
|
||||||
int[] params = settings.getIntParams();
|
int[] params = settings.getIntParams();
|
||||||
this.userSource = (params.length > 2) ? params[2] : WiredSourceUtil.SOURCE_TRIGGER;
|
this.applyLimits(params[0], params[1]);
|
||||||
|
this.userSource = (params.length > 2) ? WiredConditionInputGuard.normalizeUserSource(params[2]) : WiredSourceUtil.SOURCE_TRIGGER;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void applyLimits(int lowerLimit, int upperLimit) {
|
||||||
|
int[] limits = WiredConditionInputGuard.normalizeUserCountRange(lowerLimit, upperLimit);
|
||||||
|
this.lowerLimit = limits[0];
|
||||||
|
this.upperLimit = limits[1];
|
||||||
|
}
|
||||||
|
|
||||||
static class JsonData {
|
static class JsonData {
|
||||||
int lowerLimit;
|
int lowerLimit;
|
||||||
int upperLimit;
|
int upperLimit;
|
||||||
|
|||||||
+5
-5
@@ -97,12 +97,12 @@ public class WiredConditionTeamMember extends InteractionWiredCondition {
|
|||||||
|
|
||||||
if (wiredData.startsWith("{")) {
|
if (wiredData.startsWith("{")) {
|
||||||
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
JsonData data = WiredManager.getGson().fromJson(wiredData, JsonData.class);
|
||||||
this.teamColor = data.teamColor;
|
this.teamColor = WiredConditionInputGuard.normalizeTeamColor(data.teamColor, GameTeamColors.RED);
|
||||||
this.userSource = data.userSource;
|
this.userSource = WiredConditionInputGuard.normalizeUserSource(data.userSource);
|
||||||
this.quantifier = this.normalizeQuantifier(data.quantifier, QUANTIFIER_ANY);
|
this.quantifier = this.normalizeQuantifier(data.quantifier, QUANTIFIER_ANY);
|
||||||
} else {
|
} else {
|
||||||
if (!wiredData.equals(""))
|
if (!wiredData.equals(""))
|
||||||
this.teamColor = GameTeamColors.values()[Integer.parseInt(wiredData)];
|
this.teamColor = WiredConditionInputGuard.normalizeTeamColorType(Integer.parseInt(wiredData), GameTeamColors.RED);
|
||||||
this.userSource = WiredSourceUtil.SOURCE_TRIGGER;
|
this.userSource = WiredSourceUtil.SOURCE_TRIGGER;
|
||||||
this.quantifier = QUANTIFIER_ANY;
|
this.quantifier = QUANTIFIER_ANY;
|
||||||
}
|
}
|
||||||
@@ -147,8 +147,8 @@ public class WiredConditionTeamMember extends InteractionWiredCondition {
|
|||||||
public boolean saveData(WiredSettings settings) {
|
public boolean saveData(WiredSettings settings) {
|
||||||
if(settings.getIntParams().length < 1) return false;
|
if(settings.getIntParams().length < 1) return false;
|
||||||
int[] params = settings.getIntParams();
|
int[] params = settings.getIntParams();
|
||||||
this.teamColor = GameTeamColors.values()[params[0]];
|
this.teamColor = WiredConditionInputGuard.normalizeTeamColorType(params[0], GameTeamColors.RED);
|
||||||
this.userSource = (params.length > 1) ? params[1] : WiredSourceUtil.SOURCE_TRIGGER;
|
this.userSource = (params.length > 1) ? WiredConditionInputGuard.normalizeUserSource(params[1]) : WiredSourceUtil.SOURCE_TRIGGER;
|
||||||
this.quantifier = (params.length > 2) ? this.normalizeQuantifier(params[2], QUANTIFIER_ALL) : QUANTIFIER_ANY;
|
this.quantifier = (params.length > 2) ? this.normalizeQuantifier(params[2], QUANTIFIER_ALL) : QUANTIFIER_ANY;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
+41
@@ -0,0 +1,41 @@
|
|||||||
|
package com.eu.habbo.habbohotel.items.interactions.wired.conditions;
|
||||||
|
|
||||||
|
import com.eu.habbo.habbohotel.games.GameTeamColors;
|
||||||
|
import com.eu.habbo.habbohotel.wired.core.WiredSourceUtil;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
|
class WiredConditionInputGuardTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void teamColorsAreResolvedByProtocolTypeWithoutArrayIndexing() {
|
||||||
|
assertEquals(GameTeamColors.RED, WiredConditionInputGuard.normalizeTeamColorType(1, GameTeamColors.RED));
|
||||||
|
assertEquals(GameTeamColors.TEN, WiredConditionInputGuard.normalizeTeamColorType(14, GameTeamColors.RED));
|
||||||
|
assertEquals(GameTeamColors.RED, WiredConditionInputGuard.normalizeTeamColorType(-1, GameTeamColors.RED));
|
||||||
|
assertEquals(GameTeamColors.RED, WiredConditionInputGuard.normalizeTeamColorType(Integer.MAX_VALUE, GameTeamColors.RED));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void userSourcesFallBackToTriggerWhenUnknown() {
|
||||||
|
assertEquals(WiredSourceUtil.SOURCE_TRIGGER, WiredConditionInputGuard.normalizeUserSource(-100));
|
||||||
|
assertEquals(WiredSourceUtil.SOURCE_SELECTOR, WiredConditionInputGuard.normalizeUserSource(WiredSourceUtil.SOURCE_SELECTOR));
|
||||||
|
assertEquals(WiredSourceUtil.SOURCE_SIGNAL, WiredConditionInputGuard.normalizeUserSource(WiredSourceUtil.SOURCE_SIGNAL));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void userCountRangesArePositiveOrderedAndCapped() {
|
||||||
|
assertArrayEquals(new int[]{0, 50}, WiredConditionInputGuard.normalizeUserCountRange(-50, 50));
|
||||||
|
assertArrayEquals(new int[]{10, 20}, WiredConditionInputGuard.normalizeUserCountRange(20, 10));
|
||||||
|
assertArrayEquals(new int[]{1000, 1000}, WiredConditionInputGuard.normalizeUserCountRange(Integer.MAX_VALUE, Integer.MAX_VALUE));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void elapsedTimerCyclesArePositiveAndBounded() {
|
||||||
|
assertEquals(0, WiredConditionInputGuard.normalizeTimerCycles(-1));
|
||||||
|
assertEquals(42, WiredConditionInputGuard.normalizeTimerCycles(42));
|
||||||
|
assertEquals(WiredConditionInputGuard.MAX_TIMER_CYCLES,
|
||||||
|
WiredConditionInputGuard.normalizeTimerCycles(Integer.MAX_VALUE));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user