Originally Posted by
Tyrant
I dont personally know you but by the code shown I'm not really impressed. Interface design is quite amateur, though thats just the design aspect. Goodluck tho
Recoded it for U , are you impressed now ?
Code:
/*
@author
________ __ _____
\______ \ ____ ___ ____/ |_ ___________ / \ ___________ _________ ____
| | \_/ __ \\ \/ /\ __\/ __ \_ __ \ / \ / \ / _ \_ __ \/ ___\__ \ / \
| ` \ ___/ > < | | \ ___/| | \/ / Y ( <_> ) | \/ /_/ > __ \| | \
/_______ /\___ >__/\_ \ |__| \___ >__| \____|__ /\____/|__| \___ (____ /___| /
\/ \/ \/ \/ \/ /_____/ \/ \/
*/
package com.elvarg.game.content.sanction;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import com.elvarg.Server;
import com.elvarg.game.entity.impl.player.Player;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
/**
* Handles the sanctions given to player
*
* @author 2012
*
*/
public class SanctionManager {
/**
* The file location
*/
private static final String LOCATION = "./data/sanctions/sanctions.json";
/**
* The date format
*/
public static final SimpleDateFormat SANCTION_FORMAT = new SimpleDateFormat("dd/MM/yyyyy hh:mm:ss");
/**
* The sanctions
*/
public static ArrayList<Sanction> sanctions = new ArrayList<Sanction>();
/**
private int blackMarks;
/**
* The duration selected in hours
*/
private int durationSelected;
/**
* The rule selected
*/
private int ruleSelected;
/**
* The sanctions
*/
public enum Sanctions {
JAIL(SanctionSeverity.LOW, -1),
MUTE(SanctionSeverity.MEDIUM, 7),
BAN(SanctionSeverity.MEDIUM, 21),
IP_MUTE(SanctionSeverity.HIGH, 28),
IP_BAN(SanctionSeverity.HIGH, 56),
MAC_BAN(SanctionSeverity.EXTREME, 90),
SN_BAN(SanctionSeverity.EXTREME, 90),
HD_BAN(SanctionSeverity.EXTREME, 90),
;
/**
* The severity
*/
private SanctionSeverity severity;
/**
* How many days to expire
*/
private int expiry;
/**
* Represents a sanction
*
* @param severity
* the severity
*/
Sanctions(SanctionSeverity severity, int expiry) {
this.setSeverity(severity);
this.setExpiry(expiry);
}
/**
* Sets the severity
*
* @return the severity
*/
public SanctionSeverity getSeverity() {
return severity;
}
/**
* Sets the severity
*
* @param severity
* the severity
*/
public void setSeverity(SanctionSeverity severity) {
this.severity = severity;
}
/**
* Sets the expiry
*
* @return the expiry
*/
public int getExpiry() {
return expiry;
}
/**
* Sets the expiry
*
* @param expiry
* the expiry
*/
public void setExpiry(int expiry) {
this.expiry = expiry;
}
}
/**
* The severity of the punishment
*/
public enum SanctionSeverity {
/*
* Low punishments like jail
*/
LOW,
/*
* Medium punishments like mute
*/
MEDIUM,
/*
* High punishment like ban
*/
HIGH,
/*
* High punishment like ipban/mac ban
*/
EXTREME,
}
/**
* Checking if player is muted
*
* @param player
* the player
* @return player muted
*/
public static boolean isMuted(Player player) {
/*
* Loop through the sanctions
*/
for (Sanction san : sanctions) {
/*
* Invalid
*/
if (san == null) {
continue;
}
/*
* Muted
*/
if (san.getSanction().equals(Sanctions.MUTE) || san.getSanction().equals(Sanctions.IP_MUTE)) {
/*
* Found player
*/
if (san.getName().equals(player.getUsername())) {
return true;
}
}
}
return false;
}
/**
* Loading
*/
public static void load() {
Path path = Paths.get(LOCATION);
File file = path.toFile();
if (!file.exists()) {
return;
}
try (FileReader fileReader = new FileReader(file)) {
JsonParser fileParser = new JsonParser();
Gson builder = new GsonBuilder().create();
JsonObject reader = (JsonObject) fileParser.parse(fileReader);
if (reader.has("sanctions")) {
Sanction[] string = builder.fromJson(reader.get("sanctions").getAsJsonArray(), Sanction[].class);
for (Sanction l : string) {
sanctions.add(l);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Saving
*/
public static void save() {
Path path = Paths.get(LOCATION);
File file = path.toFile();
file.getParentFile().setWritable(true);
if (!file.getParentFile().exists()) {
try {
file.getParentFile().mkdirs();
} catch (SecurityException e) {
}
}
try (FileWriter writer = new FileWriter(file)) {
Gson builder = new GsonBuilder().setPrettyPrinting().create();
JsonObject object = new JsonObject();
object.add("sanctions", builder.toJsonTree(sanctions));
writer.write(builder.toJson(object));
writer.close();
} catch (Exception e) {
Server.getLogger().log(Level.WARNING, "An error has occured while saving a server settings file!", e);
}
}
/**
* Sets the blackMarks
*
* @return the blackMarks
*/
public int getBlackMarks() {
return blackMarks;
}
/**
* Sets the blackMarks
*
* @param blackMarks
* the blackMarks
*/
public void setBlackMarks(int blackMarks) {
this.blackMarks = blackMarks;
}
/**
* Sets the durationSelected
*
* @return the durationSelected
*/
public int getDurationSelected() {
return durationSelected;
}
/**
* Sets the durationSelected
*
* @param durationSelected
* the durationSelected
*/
public void setDurationSelected(int durationSelected) {
this.durationSelected = durationSelected;
}
/**
* Sets the ruleSelected
*
* @return the ruleSelected
*/
public int getRuleSelected() {
return ruleSelected;
}
/**
* Sets the ruleSelected
*
* @param ruleSelected
* the ruleSelected
*/
public void setRuleSelected(int ruleSelected) {
this.ruleSelected = ruleSelected;
}
}
Originally Posted by
Dexter Morgan
Thank you. Perhaps you could point me in the right direction in private message and explain a few things I could do better
Your code is clean & documented , nothing to change pal'