Skip to content

Instantly share code, notes, and snippets.

@Konicai
Last active August 25, 2024 02:55
Show Gist options
  • Select an option

  • Save Konicai/552d2721ace374f2e465cc01ea4c2495 to your computer and use it in GitHub Desktop.

Select an option

Save Konicai/552d2721ace374f2e465cc01ea4c2495 to your computer and use it in GitHub Desktop.
debug configuration
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.debug;
public record DebugFlag<T>(DebugType<T> type, T value) {
}
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.debug;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class DebugFlags {
/**
* Our get method isn't synchronized (to prioritize speed over waiting for flag updates).
* For this reason, we need to use a thread safe map so that usage of Map#set and Map#get may overlap safely.
*/
private final Map<String, DebugFlag<?>> flags = new ConcurrentHashMap<>();
/**
* Use of this method must be synchronized on this object, to ensure the map is not updated
* during or after the existing flag check code.
*/
private <T> void setNoSync(DebugFlag<T> flag) {
DebugType<T> type = flag.type();
String id = type.id();
DebugFlag<?> existing = flags.get(id);
if (existing != null && !existing.type().equals(type)) {
throw new IllegalArgumentException("DebugType %s already registered as a different type: %s".formatted(existing.type(), type));
}
flags.put(id, new DebugFlag<>(type, flag.value()));
}
public synchronized <T> void set(DebugFlag<T> flag) {
setNoSync(flag);
}
public synchronized void set(Iterable<DebugFlag<?>> flags) {
for (DebugFlag<?> flag : flags) {
setNoSync(flag);
}
}
@SuppressWarnings("unchecked")
public <T> T get(DebugType<T> type, T defaultValue) {
DebugFlag<?> flag = flags.get(type.id());
if (flag == null) {
return defaultValue;
}
if (!flag.type().equals(type)) {
throw new IllegalArgumentException("Cannot retrieve %s as different type %s is already contained".formatted(type, flag.type()));
}
return (T) flag;
}
}
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.debug;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.NonNull;
public record DebugType<T>(@NonNull String id, @NonNull TypeToken<T> valueType) {
public DebugType(@NonNull String id, @NonNull Class<T> valueType) {
this(id, TypeToken.get(valueType));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment