Fix some more issues

This commit is contained in:
2025-07-11 21:54:27 +02:00
parent 43cca3376e
commit a6b703b821
8 changed files with 122 additions and 28 deletions
@@ -0,0 +1,97 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.bausystem.region;
import de.steamwar.bausystem.region.flags.Flag;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import java.util.NoSuchElementException;
import java.util.Optional;
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class FlagOptional<T extends Enum<T> & Flag.Value<T>> {
private final Flag<T> flag;
private final T value;
public static <T extends Enum<T> & Flag.Value<T>> FlagOptional<T> of(Flag<T> flag) {
return new FlagOptional<>(flag, null);
}
public static <T extends Enum<T> & Flag.Value<T>> FlagOptional<T> of(Flag<T> flag, T value) {
return new FlagOptional<>(flag, value);
}
public boolean isPresent() {
return value != null;
}
public boolean isEmpty() {
return value == null;
}
public boolean is(T value) {
if (isEmpty()) return false;
return this.value.equals(value);
}
public boolean isWithDefault(T value) {
if (isEmpty()) {
return flag.getDefaultValue().equals(value);
} else {
return this.value.equals(value);
}
}
public T get() {
if (isEmpty()) {
throw new NoSuchElementException("No value present");
}
return value;
}
public T getWithDefault() {
if (isEmpty()) {
return flag.getDefaultValue();
} else {
return value;
}
}
public T orElse(T defaultValue) {
if (isEmpty()) {
return defaultValue;
} else {
return value;
}
}
public Optional<String> name() {
if (isEmpty()) {
return Optional.empty();
}
return Optional.of(value.name());
}
public String nameWithDefault() {
return getWithDefault().name();
}
}
@@ -21,13 +21,11 @@ package de.steamwar.bausystem.region;
import de.steamwar.bausystem.region.flags.Flag;
import java.util.Optional;
public interface FlagStorage {
<T extends Enum<T> & Flag.Value<T>> RegionFlagPolicy has(Flag<T> flag);
<T extends Enum<T> & Flag.Value<T>> boolean set(Flag<T> flag, T value);
<T extends Enum<T> & Flag.Value<T>> Optional<T> get(Flag<T> flag);
<T extends Enum<T> & Flag.Value<T>> FlagOptional<T> get(Flag<T> flag);
}
@@ -19,15 +19,15 @@ public enum RegionType implements EnumDisplay {
(region, extension) -> region.getArea().getMinPoint(false),
(region, extension) -> region.getArea().getMaxPoint(false)),
BUILD("REGION_TYPE_BUILD",
region -> region.getBuildArea().isPresent(),
region -> region.getBuildArea().get(),
(region, extension) -> region.getBuildArea().get().getMinPoint(extension),
(region, extension) -> region.getBuildArea().get().getMaxPoint(extension)),
region -> !region.getBuildArea().isEmpty(),
Region::getBuildArea,
(region, extension) -> region.getBuildArea().getMinPoint(extension),
(region, extension) -> region.getBuildArea().getMaxPoint(extension)),
TESTBLOCK("REGION_TYPE_ONLY_TB",
region -> region.getTestblockArea().isPresent(),
region -> region.getTestblockArea().get(),
(region, extension) -> region.getTestblockArea().get().getMinPoint(extension),
(region, extension) -> region.getTestblockArea().get().getMaxPoint(extension)),
region -> !region.getTestblockArea().isEmpty(),
Region::getTestblockArea,
(region, extension) -> region.getTestblockArea().getMinPoint(extension),
(region, extension) -> region.getTestblockArea().getMaxPoint(extension)),
;
private String chatValue;