Simplified the MCoreConf and added a variablebook feature.

This commit is contained in:
Olof Larsson 2014-04-07 01:50:57 +02:00
parent a9c3f7448a
commit a7d3e96ab8
6 changed files with 92 additions and 65 deletions

View File

@ -35,6 +35,7 @@ permissions:
mcore.usys.aspect.use: {description: set multiverse for aspect, default: false} mcore.usys.aspect.use: {description: set multiverse for aspect, default: false}
# misc # misc
mcore.notpdelay: {description: teleport without delay, default: false} mcore.notpdelay: {description: teleport without delay, default: false}
mcore.variablebook: {description: replace ***book*** with content of book in your hand, default: false}
# -------------------------------------------- # # -------------------------------------------- #
# STAR NOTATION # STAR NOTATION
# -------------------------------------------- # # -------------------------------------------- #
@ -65,6 +66,7 @@ permissions:
mcore.usys.aspect.show: true mcore.usys.aspect.show: true
mcore.usys.aspect.use: true mcore.usys.aspect.use: true
mcore.notpdelay: true mcore.notpdelay: true
mcore.variablebook: true
# -------------------------------------------- # # -------------------------------------------- #
# KITS # KITS
# -------------------------------------------- # # -------------------------------------------- #
@ -96,6 +98,7 @@ permissions:
mcore.usys.aspect.list: true mcore.usys.aspect.list: true
mcore.usys.aspect.show: true mcore.usys.aspect.show: true
mcore.notpdelay: true mcore.notpdelay: true
mcore.variablebook: true
mcore.kit.rank0: mcore.kit.rank0:
default: false default: false
children: children:

View File

@ -2,12 +2,15 @@ package com.massivecraft.mcore;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -18,9 +21,13 @@ import org.bukkit.event.player.PlayerKickEvent;
import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result; import org.bukkit.event.player.PlayerLoginEvent.Result;
import org.bukkit.event.player.PlayerChatTabCompleteEvent; import org.bukkit.event.player.PlayerChatTabCompleteEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerRespawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.massivecraft.mcore.event.MCoreAfterPlayerRespawnEvent; import com.massivecraft.mcore.event.MCoreAfterPlayerRespawnEvent;
@ -35,6 +42,7 @@ import com.massivecraft.mcore.store.Coll;
import com.massivecraft.mcore.store.SenderColl; import com.massivecraft.mcore.store.SenderColl;
import com.massivecraft.mcore.util.SenderUtil; import com.massivecraft.mcore.util.SenderUtil;
import com.massivecraft.mcore.util.SmokeUtil; import com.massivecraft.mcore.util.SmokeUtil;
import com.massivecraft.mcore.util.Txt;
public class EngineMainMCore extends EngineAbstract public class EngineMainMCore extends EngineAbstract
{ {
@ -73,7 +81,7 @@ public class EngineMainMCore extends EngineAbstract
public void recipientChat(final AsyncPlayerChatEvent event) public void recipientChat(final AsyncPlayerChatEvent event)
{ {
// Return unless we are using the recipient chat event // Return unless we are using the recipient chat event
if (!MCoreConf.get().isUsingRecipientChatEvent()) return; if (!MCoreConf.get().usingRecipientChatEvent) return;
// Prepare vars // Prepare vars
MCorePlayerToRecipientChatEvent recipientEvent; MCorePlayerToRecipientChatEvent recipientEvent;
@ -150,6 +158,56 @@ public class EngineMainMCore extends EngineAbstract
} }
} }
// -------------------------------------------- //
// VARIABLE BOOK
// -------------------------------------------- //
@EventHandler(priority = EventPriority.NORMAL)
public void variableBook(PlayerCommandPreprocessEvent event)
{
event.setMessage(variableBook(event.getPlayer(), event.getMessage()));
}
@EventHandler(priority = EventPriority.LOW)
public void variableBook(AsyncPlayerChatEvent event)
{
event.setMessage(variableBook(event.getPlayer(), event.getMessage()));
}
public static String variableBook(Player player, String message)
{
// If we are using command variable book ...
if (!MCoreConf.get().usingVariableBook) return message;
// ... and the player has a book text ...
String bookText = getBookText(player);
if (bookText == null) return message;
// ... and permission to use command variable book ...
if (!MCorePerm.VARIABLEBOOK.has(player, false)) return message;
// ... then replace.
return StringUtils.replace(message, MCoreConf.get().variableBook, bookText);
}
public static String getBookText(CommandSender sender)
{
if (sender == null) return null;
if (!(sender instanceof HumanEntity)) return null;
HumanEntity human = (HumanEntity)sender;
ItemStack item = human.getItemInHand();
if (item == null) return null;
if (!item.hasItemMeta()) return null;
ItemMeta itemMeta = item.getItemMeta();
if (!(itemMeta instanceof BookMeta)) return null;
BookMeta bookMeta = (BookMeta)itemMeta;
if (!bookMeta.hasPages()) return null;
List<String> pages = bookMeta.getPages();
String ret = Txt.implode(pages, " ");
ret = ret.replaceAll("\\n+", " ");
return ret;
}
// -------------------------------------------- // // -------------------------------------------- //
// EXPLOSION FX // EXPLOSION FX
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -91,7 +91,7 @@ public class EngineOfflineCase extends EngineAbstract
public void forceOnePlayerNameCase(PlayerLoginEvent event) public void forceOnePlayerNameCase(PlayerLoginEvent event)
{ {
// Stop if the feature is disabled // Stop if the feature is disabled
if (!MCoreConf.get().isForcingOnePlayerNameCase()) return; if (!MCoreConf.get().forcingOnePlayerNameCase) return;
// Stop if we are using online mode // Stop if we are using online mode
if (Bukkit.getOnlineMode()) return; if (Bukkit.getOnlineMode()) return;

View File

@ -1,7 +1,6 @@
package com.massivecraft.mcore; package com.massivecraft.mcore;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -31,45 +30,45 @@ public class MCoreConf extends Entity<MCoreConf>
public List<String> aliasesOuterMCoreMStore = MUtil.list("mstore"); public List<String> aliasesOuterMCoreMStore = MUtil.list("mstore");
// These getters and setters are obnoxious, defensive copying, NPE avoiding and probably thread safe. public boolean usingRecipientChatEvent = true;
private boolean usingRecipientChatEvent = true; public boolean forcingOnePlayerNameCase = true;
public boolean isUsingRecipientChatEvent() { return this.usingRecipientChatEvent; }
public void setUsingRecipientChatEvent(boolean usingRecipientChatEvent) { this.usingRecipientChatEvent = usingRecipientChatEvent; this.changed(); }
private boolean forcingOnePlayerNameCase = true; public Map<String, String> permissionDeniedFormats = MUtil.map(
public boolean isForcingOnePlayerNameCase() { return this.forcingOnePlayerNameCase; }
public void setForcingOnePlayerNameCase(boolean forcingOnePlayerNameCase) { this.forcingOnePlayerNameCase = forcingOnePlayerNameCase; this.changed(); }
private Map<String, String> permissionDeniedFormats = MUtil.map(
"some.awesome.permission.node", "<b>You must be awesome to %s<b>.", "some.awesome.permission.node", "<b>You must be awesome to %s<b>.",
"some.derp.permission.node.1", "derp", "some.derp.permission.node.1", "derp",
"some.derp.permission.node.2", "derp", "some.derp.permission.node.2", "derp",
"some.derp.permission.node.3", "derp", "some.derp.permission.node.3", "derp",
"derp", "<b>Only derp people can %s<b>.\n<i>Ask a moderator to become derp." "derp", "<b>Only derp people can %s<b>.\n<i>Ask a moderator to become derp."
); );
public Map<String, String> getPermissionDeniedFormats() { return this.permissionDeniedFormats == null ? new LinkedHashMap<String, String>() : new LinkedHashMap<String, String>(this.permissionDeniedFormats); }
public void setPermissionDeniedFormats(Map<String, String> permissionDeniedFormats) { this.permissionDeniedFormats = permissionDeniedFormats == null ? new LinkedHashMap<String, String>() : new LinkedHashMap<String, String>(permissionDeniedFormats); this.changed(); }
private Map<String, Integer> permissionToTpdelay = MUtil.map( public String getPermissionDeniedFormat(String permissionName)
{
Map<String, String> map = this.permissionDeniedFormats;
String ret = map.get(permissionName);
if (ret == null) return null;
ret = MUtil.recurseResolveMap(ret, map);
return ret;
}
public Map<String, Integer> permissionToTpdelay = MUtil.map(
"mcore.notpdelay", 0, "mcore.notpdelay", 0,
"default", 10 "default", 10
); );
public Map<String, Integer> getPermissionToTpdelay() { return this.permissionToTpdelay == null ? new LinkedHashMap<String, Integer>() : new LinkedHashMap<String, Integer>(this.permissionToTpdelay); }
public void setPermissionToTpdelay(Map<String, Integer> permissionToTpdelay) { this.permissionToTpdelay = permissionToTpdelay == null ? new LinkedHashMap<String, Integer>() : new LinkedHashMap<String, Integer>(permissionToTpdelay); this.changed(); }
private List<String> deleteFiles = new ArrayList<String>(); public int getTpdelay(Permissible permissible)
public List<String> getDeleteFiles() { return this.deleteFiles == null ? new ArrayList<String>() : new ArrayList<String>(this.deleteFiles); } {
public void setDeleteFiles(List<String> deleteFiles) { this.deleteFiles = deleteFiles == null ? new ArrayList<String>() : new ArrayList<String>(deleteFiles); this.changed(); } Integer ret = PermUtil.pickFirstVal(permissible, permissionToTpdelay);
if (ret == null) ret = 0;
return ret;
}
public List<String> deleteFiles = new ArrayList<String>();
// Used in the MongoDB mstore driver. // Used in the MongoDB mstore driver.
private boolean catchingMongoDbErrorsOnSave = true; public boolean catchingMongoDbErrorsOnSave = true;
public boolean isCatchingMongoDbErrorsOnSave() { return this.catchingMongoDbErrorsOnSave; }
public void setCatchingMongoDbErrorsOnSave(boolean catchingMongoDbErrorsOnSave) { this.catchingMongoDbErrorsOnSave = catchingMongoDbErrorsOnSave; this.changed(); }
private boolean catchingMongoDbErrorsOnDelete = true; public boolean catchingMongoDbErrorsOnDelete = true;
public boolean isCatchingMongoDbErrorsOnDelete() { return this.catchingMongoDbErrorsOnDelete; }
public void setCatchingMongoDbErrorsOnDelete(boolean catchingMongoDbErrorsOnDelete) { this.catchingMongoDbErrorsOnDelete = catchingMongoDbErrorsOnDelete; this.changed(); }
public static WriteConcern getMongoDbWriteConcern(boolean catchingErrors) public static WriteConcern getMongoDbWriteConcern(boolean catchingErrors)
{ {
@ -77,47 +76,14 @@ public class MCoreConf extends Entity<MCoreConf>
} }
public WriteConcern getMongoDbWriteConcernSave() public WriteConcern getMongoDbWriteConcernSave()
{ {
return getMongoDbWriteConcern(this.isCatchingMongoDbErrorsOnSave()); return getMongoDbWriteConcern(this.catchingMongoDbErrorsOnSave);
} }
public WriteConcern getMongoDbWriteConcernDelete() public WriteConcern getMongoDbWriteConcernDelete()
{ {
return getMongoDbWriteConcern(this.isCatchingMongoDbErrorsOnDelete()); return getMongoDbWriteConcern(this.catchingMongoDbErrorsOnDelete);
} }
// -------------------------------------------- // public String variableBook = "***book***";
// HELP ACCESS public boolean usingVariableBook = true;
// -------------------------------------------- //
public String setPermissionDeniedFormat(String permissionName, String permissionDeniedFormat)
{
Map<String, String> temp = this.getPermissionDeniedFormats();
String ret = temp.put(permissionName, permissionDeniedFormat);
this.setPermissionDeniedFormats(temp);
return ret;
}
public String removePermissionDeniedFormat(String permissionName)
{
Map<String, String> temp = this.getPermissionDeniedFormats();
String ret = temp.remove(permissionName);
this.setPermissionDeniedFormats(temp);
return ret;
}
public String getPermissionDeniedFormat(String permissionName)
{
Map<String, String> map = this.getPermissionDeniedFormats();
String ret = map.get(permissionName);
if (ret == null) return null;
ret = MUtil.recurseResolveMap(ret, map);
return ret;
}
public int getTpdelay(Permissible permissible)
{
Integer ret = PermUtil.pickFirstVal(permissible, this.getPermissionToTpdelay());
if (ret == null) ret = 0;
return ret;
}
} }

View File

@ -34,7 +34,7 @@ public enum MCorePerm
USYS_ASPECT_SHOW("usys.aspect.show"), USYS_ASPECT_SHOW("usys.aspect.show"),
USYS_ASPECT_USE("usys.aspect.use"), USYS_ASPECT_USE("usys.aspect.use"),
NOTPDELAY("notpdelay"), NOTPDELAY("notpdelay"),
VARIABLEBOOK("variablebook"),
// END OF LIST // END OF LIST
; ;

View File

@ -18,7 +18,7 @@ public class TaskDeleteFiles implements Runnable
@Override @Override
public void run() public void run()
{ {
for (String deleteFile : MCoreConf.get().getDeleteFiles()) for (String deleteFile : MCoreConf.get().deleteFiles)
{ {
File file = new File(deleteFile); File file = new File(deleteFile);
file.delete(); file.delete();