Skip to content

Instantly share code, notes, and snippets.

@FerusGrim
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FerusGrim/fa6cef7654be8376c3da to your computer and use it in GitHub Desktop.
Save FerusGrim/fa6cef7654be8376c3da to your computer and use it in GitHub Desktop.
MySQL Manager
/*
* Copyright (C) 2014 Nicholas Badger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ferusgrim.grimlist;
import com.ferusgrim.grimlist.manager.factory.Manager;
import com.ferusgrim.grimlist.manager.factory.ManagerType;
import org.bukkit.plugin.java.JavaPlugin;
public class GrimList extends JavaPlugin {
private static GrimList instance;
private Manager manager;
@Override
public void onEnable() {
instance = this;
saveDefaultConfig();
setupManager();
registerEvents();
registerCommands();
}
private void setupManager() {
manager = registerManager();
getManager().initialize();
getManager().setupServerSync(this);
}
@Override
public void onDisable() {
getManager().shutdown();
manager = null;
}
public static GrimList getInstance() {
return instance;
}
public Manager getManager() {
return manager;
}
private void registerEvents() {}
private void registerCommands() {}
private Manager registerManager() {
final String typeStr = getConfig().getString("General.Manager-Type");
for (ManagerType type : ManagerType.values()) {
if (type.getName().equalsIgnoreCase(typeStr)) {
if (type == ManagerType.SQLITE || type == ManagerType.REMOTE) {
getLogger().warning("SQLite/Remote are not supported, yet! Defaulting to FILE.");
return ManagerType.FILE.getManager();
}
return type.getManager();
}
}
getLogger().warning("Invalid Manager-Type in configuration. Defaulting to FILE.");
return ManagerType.FILE.getManager();
}
}
/*
* Copyright (C) 2014 Nicholas Badger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ferusgrim.grimlist.manager.factory;
import com.ferusgrim.grimlist.GrimList;
import org.bukkit.scheduler.BukkitTask;
import java.util.List;
import java.util.UUID;
public interface Manager {
public void initialize();
public void shutdown();
public void serverSync();
public String getName();
public long getUpdateInterval();
public boolean isEnabled();
public List<UUID> getWhiteList();
public List<UUID> getAddList();
public List<UUID> getRemoveList();
public void clearQueues();
public void add(UUID uuid);
public void remove(UUID uuid);
public boolean isWhitelisted(UUID uuid);
public boolean isWhitelistedInSource(UUID uuid);
public void setupServerSync(GrimList plugin);
public BukkitTask getServerSyncTask();
}
/*
* Copyright (C) 2014 Nicholas Badger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ferusgrim.grimlist.manager.factory;
import com.ferusgrim.grimlist.GrimList;
import org.bukkit.scheduler.BukkitTask;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
public abstract class ManagerBase implements Manager {
private final String name;
private final long updateInterval;
private final boolean enabled;
private final List<UUID> whiteList;
private final List<UUID> addList;
private final List<UUID> removeList;
private BukkitTask serverSyncTask;
public ManagerBase(String name, long updateInterval, boolean enabled) {
this.name = name;
this.updateInterval = updateInterval;
this.enabled = enabled;
whiteList = new ArrayList<UUID>();
addList = new ArrayList<UUID>();
removeList = new ArrayList<UUID>();
}
@Override
public String getName() {
return name;
}
@Override
public long getUpdateInterval() {
return updateInterval;
}
@Override
public boolean isEnabled() {
return enabled;
}
@Override
public List<UUID> getWhiteList() {
return whiteList;
}
@Override
public List<UUID> getAddList() {
return addList;
}
@Override
public List<UUID> getRemoveList() {
return removeList;
}
@Override
public void clearQueues() {
getAddList().clear();
getRemoveList().clear();
}
@Override
public void add(UUID uuid) {
getWhiteList().add(uuid);
getAddList().add(uuid);
getRemoveList().remove(uuid);
}
@Override
public void remove(UUID uuid) {
getWhiteList().remove(uuid);
getAddList().remove(uuid);
getRemoveList().add(uuid);
}
@Override
public boolean isWhitelisted(UUID uuid) {
return getWhiteList().contains(uuid);
}
@Override
public void setupServerSync(final GrimList plugin) {
serverSyncTask = plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, new Runnable() {
@Override
public void run() {
final long start = System.currentTimeMillis();
serverSync();
plugin.getLogger().info
(
"Server successfully synced in: " + ((System.currentTimeMillis() - start) / 1000) + " seconds!"
);
}
}, getUpdateInterval(), getUpdateInterval());
}
@Override
public BukkitTask getServerSyncTask() {
return serverSyncTask;
}
}
/*
* Copyright (C) 2014 Nicholas Badger
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ferusgrim.grimlist.manager;
import com.ferusgrim.grimlist.GrimList;
import com.ferusgrim.grimlist.manager.factory.ManagerBase;
import java.util.UUID;
public class MySQLManager extends ManagerBase {
private final GrimList plugin;
public MySQLManager(GrimList plugin) {
super("MySQL",
plugin.getConfig().getLong("MySQL.Update-Interval"),
plugin.getConfig().getBoolean("General.Enabled"));
this.plugin = plugin;
}
@Override
public void initialize() {
// serverSync();
}
@Override
public void shutdown() {
// serverSync();
}
@Override
public void serverSync() {
// Merge existing data with data from Database, somehow.
}
@Override
public boolean isWhitelistedInSource(UUID uuid) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment