forked from SteamWar/SteamWar
Replace the particle inventory with a main menu showing the 7 slots and unlocked presets, a slot editor with a read-only view for locked slots, and an entry editor with pickers for designs, choices, colours and icons. Slots are limited to 10 entries and a particles-per-tick budget.
83 lines
2.9 KiB
Java
83 lines
2.9 KiB
Java
/*
|
|
* This file is a part of the SteamWar software.
|
|
*
|
|
* Copyright (C) 2026 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.lobby.particle.slot;
|
|
|
|
import com.google.gson.JsonParser;
|
|
import de.steamwar.lobby.LobbySystem;
|
|
import de.steamwar.lobby.particle.ParticleRequirement;
|
|
import de.steamwar.sql.SteamwarUser;
|
|
import de.steamwar.sql.UserConfig;
|
|
import de.steamwar.sql.UserPerm;
|
|
import lombok.experimental.UtilityClass;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
@UtilityClass
|
|
public class ParticleSlots {
|
|
|
|
public final int COUNT = 7;
|
|
public final int MAX_ENTRIES = 10;
|
|
private final String KEY_PREFIX = "lobby-particle-slot-";
|
|
|
|
// Particle positions per tick a slot may spawn
|
|
public double maxCost(SteamwarUser user) {
|
|
return user.hasPerm(UserPerm.TEAM) ? 80 : 40;
|
|
}
|
|
|
|
// Slots 1-3 for everyone, 4-5 for team members, 6-7 for the server team
|
|
public ParticleRequirement requirement(int index) {
|
|
if (index < 3) return ParticleRequirement.NO_REQUIRMENT;
|
|
if (index < 5) return ParticleRequirement.TEAM_MEMBER;
|
|
return ParticleRequirement.SERVER_TEAM;
|
|
}
|
|
|
|
public String defaultName(int index) {
|
|
return "Slot " + (index + 1);
|
|
}
|
|
|
|
public ParticleSlot load(int userId, int index) {
|
|
String saved = UserConfig.getConfig(userId, key(index));
|
|
if (saved == null) {
|
|
return null;
|
|
}
|
|
try {
|
|
return ParticleSlot.fromJson(JsonParser.parseString(saved).getAsJsonObject(), defaultName(index));
|
|
} catch (RuntimeException e) {
|
|
LobbySystem.getInstance().getLogger().log(Level.WARNING, "Could not load particle slot " + (index + 1) + " of user " + userId, e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
public void save(int userId, int index, ParticleSlot slot) {
|
|
if (slot == null) {
|
|
UserConfig.removePlayerConfig(userId, key(index));
|
|
} else {
|
|
UserConfig.updatePlayerConfig(userId, key(index), slot.toJson().toString());
|
|
}
|
|
}
|
|
|
|
private String key(int index) {
|
|
if (index < 0 || index >= COUNT) {
|
|
throw new IllegalArgumentException("Invalid particle slot " + index);
|
|
}
|
|
return KEY_PREFIX + (index + 1);
|
|
}
|
|
}
|