Improved tool usage

This commit is contained in:
Jascha Starke 2013-04-01 22:19:58 +02:00
parent 90cc0d0ccf
commit 85a0008fbe
5 changed files with 103 additions and 12 deletions

View file

@ -146,6 +146,7 @@
<param>de.jaschastarke.minecraft.limitedcreative.limits.NoLimitPermissions:PARENT</param> <param>de.jaschastarke.minecraft.limitedcreative.limits.NoLimitPermissions:PARENT</param>
<param>de.jaschastarke.minecraft.limitedcreative.cmdblocker.CmdBlockPermissions:CONTAINER</param> <param>de.jaschastarke.minecraft.limitedcreative.cmdblocker.CmdBlockPermissions:CONTAINER</param>
<param>de.jaschastarke.minecraft.limitedcreative.regions.RegionPermissions:REGION</param> <param>de.jaschastarke.minecraft.limitedcreative.regions.RegionPermissions:REGION</param>
<param>de.jaschastarke.minecraft.limitedcreative.blockstate.BlockStatePermissions:PARENT</param>
</registeredPermissions> </registeredPermissions>
<registeredCommands> <registeredCommands>
<param>de.jaschastarke.minecraft.limitedcreative.MainCommand</param> <param>de.jaschastarke.minecraft.limitedcreative.MainCommand</param>

View file

@ -20,7 +20,7 @@ public class ModBlockStates extends CoreModule<LimitedCreative> {
} }
@Override @Override
public String getName() { public String getName() {
return "GameModePerm"; return "BlockState";
} }
@Override @Override

View file

@ -31,7 +31,10 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub
@Override @Override
public void setValue(IConfigurationNode node, Object pValue) throws InvalidValueException { public void setValue(IConfigurationNode node, Object pValue) throws InvalidValueException {
super.setValue(node, pValue); if (node.getName().equals("tool"))
setTool(pValue);
else
super.setValue(node, pValue);
if (node.getName().equals("enabled")) { if (node.getName().equals("enabled")) {
if (getEnabled()) { if (getEnabled()) {
if (entry.initialState != ModuleState.NOT_INITIALIZED) if (entry.initialState != ModuleState.NOT_INITIALIZED)
@ -75,10 +78,10 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub
* The id or technical name (http://tinyurl.com/bukkit-material) of an item that displays information about the * The id or technical name (http://tinyurl.com/bukkit-material) of an item that displays information about the
* right-clicked block. * right-clicked block.
* *
* default: WOOD_AXE * default: WOOD_PICKAXE
*/ */
@IsConfigurationNode(order = 200) @IsConfigurationNode(order = 200)
public Material getToolType() { public Material getTool() {
if (config.isString("tool")) { if (config.isString("tool")) {
Material v = Material.getMaterial(config.getString("tool")); Material v = Material.getMaterial(config.getString("tool"));
if (v != null) if (v != null)
@ -88,12 +91,40 @@ public class BlockStateConfig extends Configuration implements IConfigurationSub
if (v != null) if (v != null)
return v; return v;
} else { } else {
Object v = config.get("tool", Material.WOOD_AXE); Object v = config.get("tool", Material.WOOD_PICKAXE);
if (v instanceof Material) if (v instanceof Material)
return (Material) v; return (Material) v;
} }
mod.getLog().warn("Unknown BlockStateTool: " + config.get("tool")); mod.getLog().warn("Unknown BlockStateTool: " + config.get("tool"));
return Material.WOOD_AXE; return Material.WOOD_PICKAXE;
} }
protected void setTool(Object val) throws InvalidValueException {
String v = (String) val;
Material m = null;
try {
int i = Integer.parseInt(v);
if (i > 0)
m = Material.getMaterial(i);
} catch (NumberFormatException e) {
m = null;
}
if (m == null)
m = Material.getMaterial(v);
if (m == null)
throw new InvalidValueException("Material '" + v + "' not found");
else
config.set("tool", m);
}
@Override
public Object getValue(final IConfigurationNode node) {
Object val = super.getValue(node);
if (node.getName().equals("tool") && val != null) {
return val.toString();
} else {
return val;
}
}
} }

View file

@ -0,0 +1,43 @@
/*
* Limited Creative - (Bukkit Plugin)
* Copyright (C) 2012 jascha@ja-s.de
*
* 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 de.jaschastarke.minecraft.limitedcreative.blockstate;
import org.bukkit.permissions.PermissionDefault;
import de.jaschastarke.maven.ArchiveDocComments;
import de.jaschastarke.minecraft.lib.permissions.BasicPermission;
import de.jaschastarke.minecraft.lib.permissions.IAbstractPermission;
import de.jaschastarke.minecraft.lib.permissions.IPermission;
import de.jaschastarke.minecraft.lib.permissions.IPermissionContainer;
import de.jaschastarke.minecraft.lib.permissions.SimplePermissionContainerNode;
import de.jaschastarke.minecraft.limitedcreative.Permissions;
@ArchiveDocComments
public class BlockStatePermissions extends SimplePermissionContainerNode {
public BlockStatePermissions(IAbstractPermission parent, String name) {
super(parent, name);
}
public static final IPermissionContainer PARENT = new BlockStatePermissions(Permissions.CONTAINER, "blockstate");
/**
* Grants ability to use the configured tool to get info about placed blocks.
*/
public static final IPermission TOOL = new BasicPermission(PARENT, "tool", PermissionDefault.OP);
}

View file

@ -1,5 +1,6 @@
package de.jaschastarke.minecraft.limitedcreative.blockstate; package de.jaschastarke.minecraft.limitedcreative.blockstate;
import org.bukkit.ChatColor;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -8,7 +9,7 @@ import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import de.jaschastarke.bukkit.lib.chat.ChatFormattings; import de.jaschastarke.bukkit.lib.chat.ChatFormattings;
import de.jaschastarke.bukkit.lib.chat.NullFormatter; import de.jaschastarke.bukkit.lib.chat.InGameFormatter;
import de.jaschastarke.minecraft.limitedcreative.ModBlockStates; import de.jaschastarke.minecraft.limitedcreative.ModBlockStates;
import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source; import de.jaschastarke.minecraft.limitedcreative.blockstate.BlockState.Source;
@ -22,17 +23,32 @@ public class PlayerListener implements Listener {
public void onInteract(PlayerInteractEvent event) { public void onInteract(PlayerInteractEvent event) {
if (event.isCancelled()) if (event.isCancelled())
return; return;
if (event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK && mod.getPlugin().getPermManager().hasPermission(event.getPlayer(), BlockStatePermissions.TOOL)) {
Block b = event.getClickedBlock(); Block b = event.getClickedBlock();
if (b != null && event.getPlayer().getItemInHand().getType().equals(mod.getConfig().getToolType())) { if (b != null && event.getPlayer().getItemInHand().getType().equals(mod.getConfig().getTool())) {
BlockState s = mod.getDB().find(BlockState.class, new BlockLocation(b.getLocation())); BlockState s = mod.getDB().find(BlockState.class, new BlockLocation(b.getLocation()));
NullFormatter f = new NullFormatter(); InGameFormatter f = new InGameFormatter(mod.getPlugin().getLang());
String ret = null; String ret = null;
if (s == null || s.getSource() == Source.UNKNOWN) { if (s == null || s.getSource() == Source.UNKNOWN) {
ret = f.formatString(ChatFormattings.ERROR, f.getString("block_state.tool_info.unknown")); ret = f.formatString(ChatFormattings.ERROR, f.getString("block_state.tool_info.unknown", b.getType().toString()));
} else { } else {
String k = "block_state.tool_info." + s.getSource().name().toLowerCase(); String k = "block_state.tool_info." + s.getSource().name().toLowerCase();
ret = f.formatString(ChatFormattings.INFO, f.getString(k, b.getType(), s.getPlayerName(), s.getDate())); String gm = s.getGameMode().toString().toLowerCase();
switch (s.getGameMode()) {
case CREATIVE:
gm = ChatColor.GOLD + gm + ChatColor.RESET;
case SURVIVAL:
gm = ChatColor.GREEN + gm + ChatColor.RESET;
case ADVENTURE:
gm = ChatColor.DARK_GREEN + gm + ChatColor.RESET;
default:
break;
}
ret = f.formatString(ChatFormattings.INFO, f.getString(k, b.getType().toString(),
s.getPlayerName(),
gm,
s.getDate()));
} }
if (ret != null) if (ret != null)
event.getPlayer().sendMessage(ret); event.getPlayer().sendMessage(ret);