Refactored /npc commands

This commit is contained in:
CoderTim 2014-10-02 22:08:00 -04:00
parent a5a2c1a837
commit f6f484c134
14 changed files with 144 additions and 178 deletions

View File

@ -0,0 +1,9 @@
<component name="libraryTable">
<library name="commons-logging">
<CLASSES>
<root url="jar://$PROJECT_DIR$/Libraries/commons-logging-1.1.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View File

@ -20,6 +20,8 @@
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Debug" />
<ConfigurationWrapper RunnerId="Run" />
<method />
<method>
<option name="Make" enabled="false" />
</method>
</configuration>
</component>

View File

@ -8,6 +8,9 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="craftbukkit" level="project" />
<orderEntry type="library" name="commons-dbcp2" level="project" />
<orderEntry type="library" name="commons-pool2" level="project" />
<orderEntry type="library" name="commons-logging" level="project" />
</component>
</module>

View File

@ -1,6 +1,9 @@
package mineplex.core.npc.Commands;
import java.sql.SQLException;
import org.bukkit.ChatColor;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
@ -14,75 +17,58 @@ public class AddCommand extends CommandBase<NpcManager>
{
public AddCommand(NpcManager plugin)
{
super(plugin, Rank.OWNER, "add");
super(plugin, Rank.DEVELOPER, "add");
}
@Override
public void Execute(Player caller, String[] args)
{
if (args == null)
{
Plugin.Help(caller);
}
else
{
EntityType type;
try
{
int radius = Integer.parseInt(args[0]);
String mobName = null;
if (args.length > 1)
{
mobName = args[1];
if (args.length > 2)
{
for (int i = 2; i < args.length; i++)
{
mobName += " " + args[i];
}
}
while (mobName.indexOf('(') != -1)
{
int startIndex = mobName.indexOf('(');
if (mobName.indexOf(')') == -1)
break;
int endIndex = mobName.indexOf(')');
if (endIndex < startIndex)
break;
String originalText = mobName.substring(startIndex, endIndex + 1);
String colorString = mobName.substring(startIndex + 1, endIndex);
ChatColor color = UtilEnum.fromString(ChatColor.class, colorString);
mobName = mobName.replace(originalText, color + "");
}
}
/*
if (mobName.indexOf('_') != -1)
{
String[] mobParts = mobName.split("_");
mobName = mobParts[0];
mobSecondLine = mobParts[1];
}
*/
Plugin.SetNpcInfo(caller, radius, mobName, caller.getLocation());
UtilPlayer.message(caller, F.main(Plugin.GetName(), "Location set, now right click entity."));
type = EntityType.valueOf(args[0].toUpperCase());
}
catch(NumberFormatException exception)
catch (IllegalArgumentException e)
{
Plugin.Help(caller, "Invalid radius.");
Plugin.Help(caller, "Invalid entity.");
return;
}
catch(IllegalArgumentException exception)
double radius = 0;
if (args.length >= 2)
{
Plugin.Help(caller, "Invalid color.");
try
{
radius = Double.parseDouble(args[1]);
}
catch (NumberFormatException e)
{
Plugin.Help(caller, "Invalid radius.");
return;
}
}
boolean adult = true;
if (args.length >= 3)
adult = Boolean.parseBoolean(args[2]);
String name = null;
if (args.length >= 4)
name = ChatColor.translateAlternateColorCodes('&', args[3]);
try
{
Plugin.AddNpc(caller, type, radius, adult, name);
}
catch (SQLException e)
{
Plugin.Help(caller, "Database error.");
}
}
}

View File

@ -1,31 +0,0 @@
package mineplex.core.npc.Commands;
import org.bukkit.entity.Player;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.npc.NpcManager;
public class ClearCommand extends CommandBase<NpcManager>
{
public ClearCommand(NpcManager plugin)
{
super(plugin, Rank.OWNER, "clear");
}
@Override
public void Execute(Player caller, String[] args)
{
if (args != null)
{
Plugin.Help(caller);
}
else
{
Plugin.ClearNpcs();
UtilPlayer.message(caller, F.main(Plugin.GetName(), "Cleared npcs."));
}
}
}

View File

@ -12,19 +12,18 @@ public class DeleteCommand extends CommandBase<NpcManager>
{
public DeleteCommand(NpcManager plugin)
{
super(plugin, Rank.OWNER, "del");
super(plugin, Rank.DEVELOPER, "del");
}
@Override
public void Execute(Player caller, String[] args)
{
if (args != null)
{
Plugin.Help(caller);
}
else
{
Plugin.PrepDeleteNpc(caller);
UtilPlayer.message(caller, F.main(Plugin.GetName(), "Now right click npc."));
}
}

View File

@ -12,19 +12,18 @@ public class HomeCommand extends CommandBase<NpcManager>
{
public HomeCommand(NpcManager plugin)
{
super(plugin, Rank.OWNER, "home");
super(plugin, Rank.DEVELOPER, "home");
}
@Override
public void Execute(Player caller, String[] args)
{
if (args != null)
{
Plugin.Help(caller);
}
else
{
Plugin.TeleportNpcsHome();
UtilPlayer.message(caller, F.main(Plugin.GetName(), "Npcs teleported to home locations."));
}
}

View File

@ -10,13 +10,11 @@ public class NpcCommand extends MultiCommandBase<NpcManager>
{
public NpcCommand(NpcManager plugin)
{
super(plugin, Rank.OWNER, "npc");
super(plugin, Rank.DEVELOPER, "npc");
AddCommand(new AddCommand(plugin));
AddCommand(new DeleteCommand(plugin));
AddCommand(new ClearCommand(plugin));
AddCommand(new HomeCommand(plugin));
AddCommand(new ReattachCommand(plugin));
}
@Override

View File

@ -1,31 +0,0 @@
package mineplex.core.npc.Commands;
import mineplex.core.command.CommandBase;
import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.npc.NpcManager;
import org.bukkit.entity.Player;
public class ReattachCommand extends CommandBase<NpcManager>
{
public ReattachCommand(NpcManager plugin)
{
super(plugin, Rank.OWNER, "reattach");
}
@Override
public void Execute(Player caller, String[] args)
{
if (args != null)
{
Plugin.Help(caller);
}
else
{
Plugin.ReattachNpcs();
UtilPlayer.message(caller, F.main(Plugin.GetName(), "Npcs reattached."));
}
}
}

View File

@ -64,7 +64,7 @@ public class Npc
return _location;
}
public int getRadius()
public double getRadius()
{
return getDatabaseRecord().getRadius();
}

View File

@ -1,12 +1,5 @@
package mineplex.core.npc;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashSet;
@ -16,12 +9,10 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftAgeable;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftCreature;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftLivingEntity;
import org.bukkit.entity.Ageable;
@ -39,7 +30,6 @@ import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;
import net.minecraft.server.v1_7_R4.EntityAgeable;
import net.minecraft.server.v1_7_R4.EntityInsentient;
import mineplex.core.MiniPlugin;
@ -47,7 +37,6 @@ import mineplex.core.common.Rank;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilEnt;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.common.util.UtilWorld;
import mineplex.core.creature.Creature;
import mineplex.core.creature.event.CreatureKillEntitiesEvent;
import mineplex.core.database.DBPool;
@ -56,13 +45,12 @@ import mineplex.core.npc.event.NpcDamageByEntityEvent;
import mineplex.core.npc.event.NpcInteractEntityEvent;
import mineplex.database.Tables;
import mineplex.database.tables.records.NpcsRecord;
import org.jooq.DSLContext;
import org.jooq.Result;
import org.jooq.impl.DSL;
public class NpcManager extends MiniPlugin
{
private static String getItemStackYaml(ItemStack stack)
private static String itemStackToYaml(ItemStack stack)
{
if (stack == null || stack.getType() == Material.AIR)
return null;
@ -74,11 +62,32 @@ public class NpcManager extends MiniPlugin
}
}
private static ItemStack yamlToItemStack(String yaml)
{
if (yaml == null)
return null;
else
{
try
{
YamlConfiguration configuration = new YamlConfiguration();
configuration.loadFromString(yaml);
return configuration.getItemStack("stack");
}
catch (InvalidConfigurationException e)
{
e.printStackTrace();
return null;
}
}
}
private final Creature _creature;
private final Set<Npc> _npcs = new HashSet<>();
private final Set<UUID> _npcDeletingPlayers = new HashSet<>();
public NpcManager(JavaPlugin plugin, Creature creature) throws SQLException
public NpcManager(JavaPlugin plugin, Creature creature)
{
super("NpcManager", plugin);
@ -94,7 +103,14 @@ public class NpcManager extends MiniPlugin
_plugin.getServer().getPluginManager().registerEvents(this, _plugin);
LoadNpcs();
try
{
LoadNpcs();
}
catch (SQLException e)
{
e.printStackTrace();
}
}
public void AddCommands()
@ -105,11 +121,9 @@ public class NpcManager extends MiniPlugin
public void Help(Player caller, String message)
{
UtilPlayer.message(caller, F.main(_moduleName, "Commands List:"));
UtilPlayer.message(caller, F.help("/npc add <radius> <name>", "Right click mob to attach npc.", Rank.OWNER));
UtilPlayer.message(caller, F.help("/npc del ", "Right click npc to delete", Rank.OWNER));
UtilPlayer.message(caller, F.help("/npc clear", "Removes all npcs", Rank.OWNER));
UtilPlayer.message(caller, F.help("/npc home", " Teleport npcs to home locations.", Rank.OWNER));
UtilPlayer.message(caller, F.help("/npc reattach", "Attempt to reattach npcs to entities.", Rank.OWNER));
UtilPlayer.message(caller, F.help("/npc add <type> [radius] [adult] [name]", "Right click mob to attach npc.", Rank.DEVELOPER));
UtilPlayer.message(caller, F.help("/npc del ", "Right click npc to delete", Rank.DEVELOPER));
UtilPlayer.message(caller, F.help("/npc home", " Teleport npcs to home locations.", Rank.DEVELOPER));
if (message != null)
UtilPlayer.message(caller, F.main(_moduleName, ChatColor.RED + message));
@ -242,18 +256,18 @@ public class NpcManager extends MiniPlugin
}
}
public Entity AddNpc(Player player, String serverType, EntityType entityType, String name, int radius, boolean adult) throws SQLException
public Entity AddNpc(Player player, EntityType entityType, double radius, boolean adult, String name) throws SQLException
{
try (Connection connection = DBPool.getInstance().getConnection())
{
String helmet = getItemStackYaml(player.getInventory().getHelmet());
String chestplate = getItemStackYaml(player.getInventory().getHelmet());
String leggings = getItemStackYaml(player.getInventory().getHelmet());
String boots = getItemStackYaml(player.getInventory().getHelmet());
String inHand = getItemStackYaml(player.getInventory().getHelmet());
String helmet = itemStackToYaml(player.getInventory().getHelmet());
String chestplate = itemStackToYaml(player.getInventory().getChestplate());
String leggings = itemStackToYaml(player.getInventory().getLeggings());
String boots = itemStackToYaml(player.getInventory().getBoots());
String inHand = itemStackToYaml(player.getInventory().getItemInHand());
NpcsRecord npcsRecord = new NpcsRecord();
npcsRecord.setServer(serverType);
NpcsRecord npcsRecord = DSL.using(connection).newRecord(Tables.npcs);
npcsRecord.setServer(GetPlugin().getClass().getSimpleName());
npcsRecord.setName(name);
npcsRecord.setWorld(player.getWorld().getName());
npcsRecord.setX(player.getLocation().getX());
@ -268,8 +282,14 @@ public class NpcManager extends MiniPlugin
npcsRecord.setBoots(boots);
npcsRecord.setInHand(inHand);
DSL.using(connection).executeInsert(npcsRecord);
npcsRecord.detach();
try
{
npcsRecord.insert();
}
finally
{
npcsRecord.detach();
}
Npc npc = new Npc(npcsRecord);
_npcs.add(npc);
@ -311,6 +331,17 @@ public class NpcManager extends MiniPlugin
UtilEnt.silence(entity, true);
}
if (npc.getDatabaseRecord().getHelmet() != null)
entity.getEquipment().setHelmet(yamlToItemStack(npc.getDatabaseRecord().getHelmet()));
if (npc.getDatabaseRecord().getChestplate() != null)
entity.getEquipment().setChestplate(yamlToItemStack(npc.getDatabaseRecord().getChestplate()));
if (npc.getDatabaseRecord().getLeggings() != null)
entity.getEquipment().setLeggings(yamlToItemStack(npc.getDatabaseRecord().getLeggings()));
if (npc.getDatabaseRecord().getBoots() != null)
entity.getEquipment().setBoots(yamlToItemStack(npc.getDatabaseRecord().getBoots()));
if (npc.getDatabaseRecord().getInHand() != null)
entity.getEquipment().setItemInHand(yamlToItemStack(npc.getDatabaseRecord().getInHand()));
npc.setEntity(entity);
return entity;
@ -324,15 +355,18 @@ public class NpcManager extends MiniPlugin
{
try (Connection connection = DBPool.getInstance().getConnection())
{
DSLContext context = DSL.using(connection);
context.executeDelete(npc.getDatabaseRecord());
npc.getDatabaseRecord().attach(DSL.using(connection).configuration());
npc.getDatabaseRecord().delete();
entity.remove();
_npcs.remove(npc);
return true;
}
finally
{
npc.getDatabaseRecord().detach();
}
}
return false;

View File

@ -11,7 +11,7 @@ package mineplex.database.tables;
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class Npcs extends org.jooq.impl.TableImpl<mineplex.database.tables.records.NpcsRecord> implements java.io.Serializable, java.lang.Cloneable {
private static final long serialVersionUID = 1867321317;
private static final long serialVersionUID = -1308334886;
/**
* The singleton instance of <code>Account.npcs</code>
@ -64,7 +64,7 @@ public class Npcs extends org.jooq.impl.TableImpl<mineplex.database.tables.recor
/**
* The column <code>Account.npcs.radius</code>.
*/
public final org.jooq.TableField<mineplex.database.tables.records.NpcsRecord, java.lang.Integer> radius = createField("radius", org.jooq.impl.SQLDataType.INTEGER.nullable(false).defaulted(true), this, "");
public final org.jooq.TableField<mineplex.database.tables.records.NpcsRecord, java.lang.Double> radius = createField("radius", org.jooq.impl.SQLDataType.FLOAT.nullable(false).defaulted(true), this, "");
/**
* The column <code>Account.npcs.entityType</code>.

View File

@ -9,9 +9,9 @@ package mineplex.database.tables.records;
@javax.annotation.Generated(value = { "http://www.jooq.org", "3.4.2" },
comments = "This class is generated by jOOQ")
@java.lang.SuppressWarnings({ "all", "unchecked", "rawtypes" })
public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.database.tables.records.NpcsRecord> implements java.io.Serializable, java.lang.Cloneable, org.jooq.Record15<java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.Integer, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String> {
public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.database.tables.records.NpcsRecord> implements java.io.Serializable, java.lang.Cloneable, org.jooq.Record15<java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String> {
private static final long serialVersionUID = 1527372083;
private static final long serialVersionUID = -202208690;
/**
* Setter for <code>Account.npcs.id</code>.
@ -114,15 +114,15 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
/**
* Setter for <code>Account.npcs.radius</code>.
*/
public void setRadius(java.lang.Integer value) {
public void setRadius(java.lang.Double value) {
setValue(7, value);
}
/**
* Getter for <code>Account.npcs.radius</code>.
*/
public java.lang.Integer getRadius() {
return (java.lang.Integer) getValue(7);
public java.lang.Double getRadius() {
return (java.lang.Double) getValue(7);
}
/**
@ -243,7 +243,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
* {@inheritDoc}
*/
@Override
public org.jooq.Row15<java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.Integer, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String> fieldsRow() {
public org.jooq.Row15<java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String> fieldsRow() {
return (org.jooq.Row15) super.fieldsRow();
}
@ -251,7 +251,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
* {@inheritDoc}
*/
@Override
public org.jooq.Row15<java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.Integer, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String> valuesRow() {
public org.jooq.Row15<java.lang.Integer, java.lang.String, java.lang.String, java.lang.String, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.Double, java.lang.String, java.lang.Boolean, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String> valuesRow() {
return (org.jooq.Row15) super.valuesRow();
}
@ -315,7 +315,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
* {@inheritDoc}
*/
@Override
public org.jooq.Field<java.lang.Integer> field8() {
public org.jooq.Field<java.lang.Double> field8() {
return mineplex.database.tables.Npcs.npcs.radius;
}
@ -435,7 +435,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
* {@inheritDoc}
*/
@Override
public java.lang.Integer value8() {
public java.lang.Double value8() {
return getRadius();
}
@ -562,7 +562,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
* {@inheritDoc}
*/
@Override
public NpcsRecord value8(java.lang.Integer value) {
public NpcsRecord value8(java.lang.Double value) {
setRadius(value);
return this;
}
@ -634,7 +634,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
* {@inheritDoc}
*/
@Override
public NpcsRecord values(java.lang.Integer value1, java.lang.String value2, java.lang.String value3, java.lang.String value4, java.lang.Double value5, java.lang.Double value6, java.lang.Double value7, java.lang.Integer value8, java.lang.String value9, java.lang.Boolean value10, java.lang.String value11, java.lang.String value12, java.lang.String value13, java.lang.String value14, java.lang.String value15) {
public NpcsRecord values(java.lang.Integer value1, java.lang.String value2, java.lang.String value3, java.lang.String value4, java.lang.Double value5, java.lang.Double value6, java.lang.Double value7, java.lang.Double value8, java.lang.String value9, java.lang.Boolean value10, java.lang.String value11, java.lang.String value12, java.lang.String value13, java.lang.String value14, java.lang.String value15) {
return this;
}
@ -652,7 +652,7 @@ public class NpcsRecord extends org.jooq.impl.UpdatableRecordImpl<mineplex.datab
/**
* Create a detached, initialised NpcsRecord
*/
public NpcsRecord(java.lang.Integer id, java.lang.String server, java.lang.String name, java.lang.String world, java.lang.Double x, java.lang.Double y, java.lang.Double z, java.lang.Integer radius, java.lang.String entityType, java.lang.Boolean adult, java.lang.String helmet, java.lang.String chestplate, java.lang.String leggings, java.lang.String boots, java.lang.String inHand) {
public NpcsRecord(java.lang.Integer id, java.lang.String server, java.lang.String name, java.lang.String world, java.lang.Double x, java.lang.Double y, java.lang.Double z, java.lang.Double radius, java.lang.String entityType, java.lang.Boolean adult, java.lang.String helmet, java.lang.String chestplate, java.lang.String leggings, java.lang.String boots, java.lang.String inHand) {
super(mineplex.database.tables.Npcs.npcs);
setValue(0, id);

View File

@ -19,9 +19,7 @@ public class NpcProtectListener implements Listener
@EventHandler(priority = EventPriority.LOWEST)
public void CustomDamage(CustomDamageEvent event)
{
if (event.GetDamageeEntity() != null && _npcManager.GetNpcByUUID(event.GetDamageeEntity().getUniqueId()) != null)
{
if (event.GetDamageeEntity() != null && _npcManager.getNpcByEntityUUID(event.GetDamageeEntity().getUniqueId()) != null)
event.SetCancelled("NPC");
}
}
}