Part 1 / 2

Only thing left is to fix all the code issues from moving out the discord and shop implementations.
This commit is contained in:
Paul Reilly
2023-03-09 01:42:18 -06:00
parent f53696aa9e
commit 2265783afb
319 changed files with 1531 additions and 1440 deletions

View File

@ -0,0 +1,145 @@
package me.totalfreedom.totalfreedommod.punishments;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;
import me.totalfreedom.totalfreedommod.config.IConfig;
import org.bukkit.configuration.ConfigurationSection;
public class Punishment implements IConfig
{
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
private String username = null;
private String ip = null;
private String by = null;
private PunishmentType type = null;
private String reason = null;
private Date issued_on = null;
public Punishment()
{
}
public Punishment(String username, String ip, String by, PunishmentType type, String reason)
{
this.username = username;
this.ip = ip;
this.by = by;
this.type = type;
this.reason = reason;
this.issued_on = new Date();
}
public static SimpleDateFormat getDateFormat()
{
return DATE_FORMAT;
}
@Override
public void loadFrom(ConfigurationSection cs)
{
this.username = cs.getString("username", null);
this.ip = cs.getString("ip", null);
this.by = cs.getString("by", null);
this.type = PunishmentType.valueOf(Objects.requireNonNull(cs.getString("type", null)).toUpperCase());
this.reason = cs.getString("reason", null);
try
{
this.issued_on = DATE_FORMAT.parse(cs.getString("issued_on", null));
}
catch (ParseException e)
{
this.issued_on = null;
}
}
@Override
public void saveTo(ConfigurationSection cs)
{
cs.set("username", username);
cs.set("ip", ip);
cs.set("by", by);
cs.set("type", type.name().toLowerCase());
cs.set("reason", reason);
cs.set("issued_on", DATE_FORMAT.format(issued_on));
}
@Override
public boolean isValid()
{
return username != null || ip != null;
}
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public String getIp()
{
return ip;
}
public void setIp(String ip)
{
this.ip = ip;
}
public String getBy()
{
return by;
}
public void setBy(String by)
{
this.by = by;
}
public PunishmentType getType()
{
return type;
}
public void setType(PunishmentType type)
{
this.type = type;
}
public String getReason()
{
return reason;
}
public void setReason(String reason)
{
this.reason = reason;
}
public Date getIssuedOn()
{
return issued_on;
}
public void setIssuedOn(Date issued_on)
{
this.issued_on = issued_on;
}
}

View File

@ -0,0 +1,125 @@
package me.totalfreedom.totalfreedommod.punishments;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.config.YamlConfig;
import me.totalfreedom.totalfreedommod.util.FLog;
public class PunishmentList extends FreedomService
{
public static final String CONFIG_FILENAME = "punishments.yml";
private final Set<Punishment> punishments = Sets.newHashSet();
//
private final YamlConfig config;
public PunishmentList()
{
this.config = new YamlConfig(plugin, CONFIG_FILENAME);
}
@Override
public void onStart()
{
config.load();
punishments.clear();
for (String id : config.getKeys(false))
{
if (!config.isConfigurationSection(id))
{
FLog.warning("Failed to load punishment number " + id + "!");
continue;
}
Punishment punishment = new Punishment();
punishment.loadFrom(Objects.requireNonNull(config.getConfigurationSection(id)));
if (!punishment.isValid())
{
FLog.warning("Not adding punishment number " + id + ". Missing information.");
continue;
}
punishments.add(punishment);
}
FLog.info("Loaded " + punishments.size() + " punishments.");
}
@Override
public void onStop()
{
saveAll();
FLog.info("Saved " + punishments.size() + " player bans");
}
public void saveAll()
{
config.clear();
for (Punishment punishment : punishments)
{
punishment.saveTo(config.createSection(String.valueOf(punishment.hashCode())));
}
// Save config
config.save();
}
public int clear()
{
int removed = punishments.size();
punishments.clear();
saveAll();
return removed;
}
public int clear(String username)
{
List<Punishment> removed = new ArrayList<>();
for (Punishment punishment : punishments)
{
if (punishment.getUsername().equalsIgnoreCase(username))
{
removed.add(punishment);
}
}
if (removed.size() != 0)
{
punishments.removeAll(removed);
saveAll();
}
return removed.size();
}
public int getLastPunishmentID()
{
int size = punishments.size();
if (size == 0)
{
return 1;
}
return size;
}
public void logPunishment(Punishment punishment)
{
if (punishments.add(punishment))
{
saveAll();
}
}
}

View File

@ -0,0 +1,17 @@
package me.totalfreedom.totalfreedommod.punishments;
public enum PunishmentType
{
MUTE,
SMITE,
KICK,
TEMPBAN,
BAN,
DOOM,
WARN,
CAGE,
BLOCKEDIT,
BLOCKPVP,
BLOCKCMD,
ORBIT
}