Added ErrorProne for potential bugs. Fixed potential bugs.

This commit is contained in:
Bas de Jong
2025-10-12 07:14:26 +02:00
parent e3bce3889e
commit ea6264e519
12 changed files with 265 additions and 72 deletions

View File

@@ -6,6 +6,8 @@ import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;
import java.util.Locale;
/**
* Utility class for configuring logging levels dynamically at runtime using Log4j 2.
*
@@ -144,7 +146,7 @@ public final class Logging {
* @param levelToLog name of the logging level (e.g., "DEBUG", "INFO")
*/
public static void enableLogsForClass(String className, String levelToLog) {
Level level = Level.valueOf(levelToLog.trim().toUpperCase());
Level level = Level.valueOf(levelToLog.trim().toUpperCase(Locale.ROOT));
if (level != null && verifyStringIsActualClass(className)) {
enableLogsForClassInternal(className, level);
}

View File

@@ -0,0 +1,7 @@
package org.toop.framework.annotations;
import java.lang.annotation.*;
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.TYPE})
public @interface TestsOnly {}

View File

@@ -2,6 +2,7 @@ package org.toop.framework.audio;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.toop.framework.annotations.TestsOnly;
import org.toop.framework.audio.interfaces.Dispatcher;
import org.toop.framework.resource.ResourceManager;
import org.toop.framework.resource.resources.BaseResource;
@@ -12,7 +13,7 @@ import java.util.*;
public class MusicManager<T extends AudioResource> implements org.toop.framework.audio.interfaces.MusicManager<T> {
private static final Logger logger = LogManager.getLogger(MusicManager.class);
private final List<T> backgroundMusic = new LinkedList<>();
private final List<T> backgroundMusic = new ArrayList<>();
private final Dispatcher dispatcher;
private final List<T> resources;
private int playingIndex = 0;
@@ -27,8 +28,11 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
createShuffled();
}
// Used in unit testing
MusicManager(List<T> resources, Dispatcher dispatcher) {
/**
* {@code @TestsOnly} DO NOT USE
*/
@TestsOnly
public MusicManager(List<T> resources, Dispatcher dispatcher) {
this.dispatcher = dispatcher;
this.resources = new ArrayList<>(resources);
backgroundMusic.addAll(resources);
@@ -49,6 +53,7 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
backgroundMusic.addAll(resources);
}
@Override
public void play() {
if (playing) {
logger.warn("MusicManager is already playing.");
@@ -114,6 +119,7 @@ public class MusicManager<T extends AudioResource> implements org.toop.framework
});
}
@Override
public void stop() {
if (!playing) return;

View File

@@ -5,6 +5,7 @@ import org.apache.logging.log4j.Logger;
import org.toop.framework.resource.ResourceManager;
import org.toop.framework.resource.ResourceMeta;
import org.toop.framework.resource.resources.BaseResource;
import org.toop.framework.resource.resources.MusicAsset;
import org.toop.framework.resource.resources.SoundEffectAsset;
import javax.sound.sampled.Clip;
@@ -12,10 +13,7 @@ import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
public class SoundEffectManager implements org.toop.framework.audio.interfaces.SoundEffectManager<SoundEffectAsset> {
@@ -23,6 +21,8 @@ public class SoundEffectManager implements org.toop.framework.audio.interfaces.S
private final HashMap<String, SoundEffectAsset> soundEffectResources;
public SoundEffectManager(){
MusicAsset[] abcd = {};
new MusicManager<>(Arrays.stream(abcd).toList(), new JavaFXDispatcher());
// If there are duplicates, takes discards the first
soundEffectResources = ResourceManager.getAllOfType(SoundEffectAsset.class).stream()
.collect(Collectors.toMap(ResourceMeta::getName, ResourceMeta::getResource, (a, b) -> b, HashMap::new));

View File

@@ -52,8 +52,11 @@ public enum VolumeControl {
FX(),
MUSIC();
@SuppressWarnings("ImmutableEnumChecker")
private final List<AudioManager<? extends AudioResource>> managers = new CopyOnWriteArrayList<>();
@SuppressWarnings("ImmutableEnumChecker")
private double volume = 1.0;
@SuppressWarnings("ImmutableEnumChecker")
private double masterVolume = 1.0;
/**

View File

@@ -9,10 +9,10 @@ public class EventsBase {
/**
* WIP, DO NOT USE!
*
* @param eventName
* @param args
* @return
* @throws Exception
* @param eventName todo
* @param args todo
* @return todo
* @throws Exception todo
*/
public static Object get(String eventName, Object... args) throws Exception {
Class<?> clazz = Class.forName("org.toop.framework.eventbus.events.Events$ServerEvents$" + eventName);
@@ -24,11 +24,11 @@ public class EventsBase {
/**
* WIP, DO NOT USE!
*
* @param eventCategory
* @param eventName
* @param args
* @return
* @throws Exception
* @param eventCategory todo
* @param eventName todo
* @param args todo
* @return todo
* @throws Exception todo
*/
public static Object get(String eventCategory, String eventName, Object... args)
throws Exception {
@@ -42,10 +42,10 @@ public class EventsBase {
/**
* WIP, DO NOT USE!
*
* @param eventName
* @param args
* @return
* @throws Exception
* @param eventName todo
* @param args todo
* @return todo
* @throws Exception todo
*/
public static Object get2(String eventName, Object... args) throws Exception {
// Fully qualified class name

View File

@@ -119,6 +119,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
}
private void gameWinConditionHandler(String rec) {
@SuppressWarnings("StreamToString")
String condition =
Pattern.compile("\\b(win|draw|lose)\\b", Pattern.CASE_INSENSITIVE)
.matcher(rec)
@@ -180,6 +181,7 @@ public class NetworkingGameClientHandler extends ChannelInboundHandlerAdapter {
}
private void gameYourTurnHandler(String rec) {
@SuppressWarnings("StreamToString")
String msg =
Pattern.compile("TURNMESSAGE:\\s*\"([^\"]*)\"")
.matcher(rec)