Refactoring: added GateMarkerUtil and Logger classes.

Added support for updating markers after gate attributes changed.
This commit is contained in:
Tobias Ottenweller 2014-01-26 15:16:51 +01:00
parent 86116d07f5
commit a99a1e1a97
3 changed files with 200 additions and 87 deletions

View File

@ -0,0 +1,125 @@
/* Craft Inc. Gates Dynmap
Copyright (C) 2011-2014 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.dynmap;
import de.craftinc.gates.Gate;
import org.bukkit.Location;
import org.dynmap.markers.Marker;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerIcon;
import org.dynmap.markers.MarkerSet;
import java.io.InputStream;
import java.util.logging.Level;
public class GateMarkerUtil
{
protected static final String markerIconID = "de.craftinc.gates.maker-icon";
protected static final String markerIconLabel = "Gate";
protected static final String markerSetID = "de.craftinc.gates";
protected static final String markerSetName = "Gates";
protected MarkerIcon gateIcon;
protected MarkerSet markerSet;
protected MarkerAPI markerAPI;
public GateMarkerUtil(MarkerAPI markerAPI)
{
super();
this.markerAPI = markerAPI;
loadGateIcon();
loadMarkerSet();
}
protected void loadGateIcon()
{
this.gateIcon = this.markerAPI.getMarkerIcon(markerIconID);
if (this.gateIcon == null) {
InputStream inputStream = this.getClass().getResourceAsStream("/gate.png");
if (inputStream == null) {
Logger.log(Level.SEVERE, "Cannot load gate icon (missing resource)");
return;
}
this.gateIcon = this.markerAPI.createMarkerIcon(markerIconID, markerIconLabel, inputStream);
if (this.gateIcon == null) {
Logger.log(Level.SEVERE, "Cannot load gate icon");
}
}
}
protected void loadMarkerSet()
{
markerSet = markerAPI.getMarkerSet(markerSetID);
if (markerSet == null) {
markerSet = markerAPI.createMarkerSet(markerSetID, markerSetName, null, false);
}
}
public void addMarker(Gate gate)
{
if (gate.getLocation() == null) {
return;
}
String id = gate.getId();
String label = gate.getId();
Location l = gate.getLocation();
MarkerIcon icon = this.gateIcon;
this.markerSet.createMarker(id, label, false, l.getWorld().getName(), l.getX(), l.getY(), l.getZ(), icon, false);
}
public void updateMarker(Gate gate, String oldID)
{
Marker m = this.markerSet.findMarker(gate.getId());
if (oldID != null && m != null) {
m.deleteMarker();
m = null;
}
if (m == null) {
this.addMarker(gate);
}
else if (gate.getLocation() == null) {
this.removeMarker(gate);
}
else {
Location l = gate.getLocation();
m.setLocation(l.getWorld().getName(), l.getX(), l.getY(), l.getZ());
}
}
public void removeMarker(Gate gate)
{
Marker m = this.markerSet.findMarker(gate.getId());
m.deleteMarker();
}
}

View File

@ -0,0 +1,36 @@
/* Craft Inc. Gates Dynmap
Copyright (C) 2011-2014 Craft Inc. Gates Team (see AUTHORS.txt)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program (LGPLv3). If not, see <http://www.gnu.org/licenses/>.
*/
package de.craftinc.gates.dynmap;
import java.util.logging.Level;
public class Logger
{
public static void log(String msg)
{
log(Level.INFO, msg);
}
public static void log(Level level, String msg)
{
java.util.logging.Logger.getLogger("Minecraft").log(level, "["+Plugin.getPlugin().getDescription().getFullName()+"] "+msg);
}
}

View File

@ -18,47 +18,59 @@ package de.craftinc.gates.dynmap;
import de.craftinc.gates.Gate;
import de.craftinc.gates.GateChangeListener;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.dynmap.DynmapAPI;
import org.dynmap.markers.MarkerAPI;
import org.dynmap.markers.MarkerIcon;
import org.dynmap.markers.MarkerSet;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Plugin extends JavaPlugin
public class Plugin extends JavaPlugin implements GateChangeListener
{
private static final String markerSetID = "de.craftinc.gates";
private static final String markerSetName = "Gates";
protected static Plugin instance;
private static final String markerIconID = "de.craftinc.gates.maker-icon";
private static final String markerIconLabel = "Gate";
protected DynmapAPI dynmapAPI;
protected MarkerAPI markerAPI;
protected de.craftinc.gates.Plugin gatesPlugin;
protected GateMarkerUtil markerUtil;
DynmapAPI dynmapAPI;
MarkerAPI markerAPI;
de.craftinc.gates.Plugin gatesPlugin;
MarkerSet gateMarkers;
MarkerIcon gateIcon;
public Plugin()
{
instance = this;
}
public static Plugin getPlugin()
{
return instance;
}
@Override
public void onEnable()
{
super.onEnable();
if (!loadAPI()) {
return;
}
loadIcons();
createMakersSet();
this.markerUtil = new GateMarkerUtil(this.markerAPI);
loadAllGateMarkers();
setupChangeListener();
this.gatesPlugin.getGatesManager().addGateChangeListener(this);
}
@Override
public void onDisable()
{
this.gatesPlugin.getGatesManager().removeGateChangeListener(this);
super.onDisable();
}
@ -68,7 +80,7 @@ public class Plugin extends JavaPlugin
org.bukkit.plugin.Plugin dynmap = pm.getPlugin("dynmap");
if (dynmap == null) {
log(Level.SEVERE, "Cannot find dynmap!");
Logger.log(Level.SEVERE, "Cannot find dynmap!");
return false;
}
@ -76,14 +88,14 @@ public class Plugin extends JavaPlugin
markerAPI = dynmapAPI.getMarkerAPI();
if (markerAPI == null) {
log(Level.SEVERE, "Error loading Dynmap marker API!");
Logger.log(Level.SEVERE, "Error loading Dynmap marker API!");
return false;
}
gatesPlugin = (de.craftinc.gates.Plugin)pm.getPlugin("Craft Inc. Gates");
if (gatesPlugin == null) {
log(Level.SEVERE, "Cannot find Craft Inc. Gates");
Logger.log(Level.SEVERE, "Cannot find Craft Inc. Gates");
return false;
}
@ -91,80 +103,20 @@ public class Plugin extends JavaPlugin
}
private void loadIcons()
{
this.gateIcon = this.markerAPI.getMarkerIcon(markerSetID);
if (this.gateIcon == null) {
InputStream inputStream = this.getClass().getResourceAsStream("/gate.png");
if (inputStream == null) {
log(Level.SEVERE, "Cannot load gate icon (missing resource)");
return;
}
this.gateIcon = this.markerAPI.createMarkerIcon(markerIconID, markerIconLabel, inputStream);
if (this.gateIcon == null) {
log(Level.SEVERE, "Cannot load gate icon");
}
}
}
private void createMakersSet()
{
gateMarkers = markerAPI.getMarkerSet(markerSetID);
if (gateMarkers == null) {
gateMarkers = markerAPI.createMarkerSet(markerSetID, markerSetName, null, false);
}
}
private void loadAllGateMarkers()
{
List<Gate> allGates = this.gatesPlugin.getGatesManager().allGates();
for (Gate g : allGates) {
if (g.getLocation() == null) {
continue;
}
String id = g.getId();
String label = g.getId();
boolean markup = false;
String world = g.getLocation().getWorld().getName();
double x = g.getLocation().getX();
double y = g.getLocation().getY();
double z = g.getLocation().getZ();
MarkerIcon icon = this.gateIcon;
boolean is_persistent = false;
this.gateMarkers.createMarker(id, label, markup, world, x, y, z, icon, is_persistent);
this.markerUtil.addMarker(g);
}
}
private void setupChangeListener()
@Override
public void gateChangedHandler(Gate gate, Map<String, Object> stringObjectMap)
{
// TODO: implement this
String oldID = (String)stringObjectMap.get(GateChangeListener.changedID);
this.markerUtil.updateMarker(gate, oldID);
}
/*
* Logging
*/
public void log(String msg)
{
log(Level.INFO, msg);
}
public void log(Level level, String msg)
{
Logger.getLogger("Minecraft").log(level, "["+this.getDescription().getFullName()+"] "+msg);
}
}