Skip to content

Instantly share code, notes, and snippets.

@NeatMonster
Created March 1, 2012 16:37
Show Gist options
  • Save NeatMonster/1951166 to your computer and use it in GitHub Desktop.
Save NeatMonster/1951166 to your computer and use it in GitHub Desktop.
RandomMOTD
package fr.aumgn.motd;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.bukkit.Bukkit;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
import org.bukkit.plugin.java.JavaPlugin;
/**
* @author NeatMonster.
*/
public class RandomMOTD extends JavaPlugin implements Listener {
public class MOTD {
private boolean cancelled;
private final String message;
private final String name;
private final int weight;
/**
* Instantiates a new MOTD.
*
* @param name
* the name of the MOTD
* @param message
* the message of the MOTD
* @param weight
* the weight of the MOTD
*/
public MOTD(final String name, final String message, final int weight) {
this.name = name;
this.message = message;
this.weight = weight;
cancelled = false;
}
/**
* Gets the message of this MOTD.
*
* @return the message of this MOTD
*/
public String getMessage() {
return message;
}
/**
* Gets the name of this MOTD.
*
* @return the name of this MOTD
*/
public String getName() {
return name;
}
/**
* Gets the weight of this MOTD.
*
* @return the weight of this MOTD
*/
public int getWeight() {
return weight;
}
/**
* Gets the cancellation state of this MOTD.
*
* @return true if this MOTD cancelled
*/
public boolean isCancelled() {
return cancelled;
}
/**
* Sets the cancellation state of this MOTD.
*
* @param cancelled
* true if you wish to cancel this MOTD
*/
public void setCancelled(final boolean cancel) {
cancelled = cancel;
}
}
private final List<MOTD> motds = new ArrayList<MOTD>();
private final Random random = new Random();
/**
* Adds a MOTD to the MOTDs' list.
*
* @param motd
* the MOTD to add
*/
public void addMOTD(final MOTD motd) {
motds.add(motd);
saveMOTDs();
}
/**
* Gets the MOTDs' list.
*
* @return the MOTDs' list
*/
public List<MOTD> getMOTDs() {
return motds;
}
/*
* (non-Javadoc)
* @see org.bukkit.plugin.java.JavaPlugin#onEnable()
*/
@Override
public void onEnable() {
super.onEnable();
getConfig().addDefault("Message1.Message", "This is the first MOTD.");
getConfig().addDefault("Message1.Weight", 50);
getConfig().addDefault("Message2.Message", "This is the second MOTD.");
getConfig().addDefault("Message2.Weight", 50);
getConfig().options().copyDefaults(true);
saveConfig();
for (final String key : getConfig().getKeys(false))
motds.add(new MOTD(key, getConfig().getString(key + ".Message"), getConfig().getInt(key + ".Weight")));
System.out.println("[RandomMOTD] " + motds.size() + " MOTDs loaded.");
Bukkit.getPluginManager().registerEvents(this, this);
}
@EventHandler(ignoreCancelled = true)
public void onServerListPing(final ServerListPingEvent event) {
if (motds.size() == 0 || total() == 0)
return;
int total = 0;
final double selected = random.nextDouble() * total();
for (final MOTD motd : motds)
if (!motd.isCancelled()) {
total += motd.getWeight();
if (total >= selected) {
event.setMotd(motd.getMessage());
break;
}
}
}
/**
* Removes a MOTD of the MOTDs' list.
*
* @param motd
* the MOTD to remove
*/
public void removeMOTD(final MOTD motd) {
motds.remove(motd);
saveMOTDs();
}
/**
* Saves all the MOTDs.
*/
public void saveMOTDs() {
for (final MOTD motd : motds) {
getConfig().set(motd.getName() + ".Message", motd.getMessage());
getConfig().set(motd.getName() + ".Weight", motd.getWeight());
}
saveConfig();
}
private int total() {
int weight = 0;
for (final MOTD motd : motds)
if (!motd.isCancelled())
weight += motd.getWeight();
return weight;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment