mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-06-29 19:46:42 +00:00
Punishment logging!
This commit is contained in:
@ -0,0 +1,85 @@
|
||||
package me.totalfreedom.totalfreedommod.punishments;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import net.pravian.aero.base.ConfigLoadable;
|
||||
import net.pravian.aero.base.ConfigSavable;
|
||||
import net.pravian.aero.base.Validatable;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
|
||||
public class Punishment implements ConfigLoadable, ConfigSavable, Validatable
|
||||
{
|
||||
|
||||
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z");
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private String username = null;
|
||||
@Getter
|
||||
private String ip = null;
|
||||
@Getter
|
||||
@Setter
|
||||
private String by = null;
|
||||
@Getter
|
||||
@Setter
|
||||
private PunishmentType type = null;
|
||||
@Getter
|
||||
@Setter
|
||||
private String reason = null;
|
||||
@Getter
|
||||
@Setter
|
||||
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();
|
||||
}
|
||||
|
||||
@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(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;
|
||||
}
|
||||
}
|
@ -0,0 +1,128 @@
|
||||
package me.totalfreedom.totalfreedommod.punishments;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.ArrayList;
|
||||
import me.totalfreedom.totalfreedommod.FreedomService;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import net.pravian.aero.config.YamlConfig;
|
||||
import com.google.common.collect.Sets;
|
||||
|
||||
public class PunishmentList extends FreedomService
|
||||
{
|
||||
|
||||
private final Set<Punishment> punishments = Sets.newHashSet();
|
||||
public static final String CONFIG_FILENAME = "punishments.yml";
|
||||
|
||||
//
|
||||
private final YamlConfig config;
|
||||
|
||||
public PunishmentList(TotalFreedomMod plugin)
|
||||
{
|
||||
super(plugin);
|
||||
this.config = new YamlConfig(plugin, CONFIG_FILENAME);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected 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(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
|
||||
protected void onStop()
|
||||
{
|
||||
saveAll();
|
||||
logger.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 boolean logPunishment(Punishment punishment)
|
||||
{
|
||||
if (punishments.add(punishment))
|
||||
{
|
||||
saveAll();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package me.totalfreedom.totalfreedommod.punishments;
|
||||
|
||||
public enum PunishmentType
|
||||
{
|
||||
MUTE,
|
||||
SMITE,
|
||||
KICK,
|
||||
TEMPBAN,
|
||||
BAN,
|
||||
DOOM
|
||||
}
|
Reference in New Issue
Block a user