Implement destroy command.

This commit is contained in:
Jesse Prescott 2018-06-03 22:11:06 +01:00
parent dc93e8c145
commit 699b551562
5 changed files with 204 additions and 2 deletions

View File

@ -15,10 +15,10 @@
package com.gmail.bleedobsidian.itemcase;
import com.gmail.bleedobsidian.itemcase.commands.CreateCommand;
import com.gmail.bleedobsidian.itemcase.commands.DestroyCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
/**
@ -48,6 +48,9 @@ public final class CommandHandler implements CommandExecutor {
case "create":
new CreateCommand().execute(sender, label, args);
break;
case "destroy":
new DestroyCommand().execute(sender, label, args);
break;
default:
this.showHelp(sender, label);
break;

View File

@ -0,0 +1,109 @@
/*
* ItemCase 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/gpl.html>.
*/
package com.gmail.bleedobsidian.itemcase.commands;
import com.gmail.bleedobsidian.itemcase.ChatLogger;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
import com.gmail.bleedobsidian.itemcase.Itemcase;
import com.gmail.bleedobsidian.itemcase.managers.ItemcaseManager;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
/**
* A command handler for the 'create' command.
*
* @author Jesse Prescott (BleedObsidian)
*/
public final class DestroyCommand implements Command {
@Override
public void execute(CommandSender sender, String label,
String[] args) {
// If sender is not a player.
if(!(sender instanceof Player)) {
// Send message.
ItemCaseCore.instance.getGenericLogger().message(
sender, "command.not-player");
// Exit.
return;
}
// Get chat logger.
ChatLogger chatLogger = ItemCaseCore.instance.getChatLogger();
// Cast sender to player.
Player player = (Player) sender;
// Check if player has permission.
if(!player.hasPermission("itemcase.destroy")) {
// Send message.
chatLogger.message(player, "command.permission");
// Exit.
return;
}
// The target location.
Location target = null;
// Get ItemcaseManager.
ItemcaseManager manager = ItemCaseCore.instance.getItemcaseManager();
// Get the players target block.
target = player.getTargetBlock(null, 3).getLocation();
// Check this target is an itemcase.
if(!manager.isItemcase(target)) {
// Show message.
chatLogger.message(player, "command.destroy.invalid-location");
// Exit.
return;
}
// Get itemcase.
Itemcase itemcase = manager.getItemcase(target);
// Get owner.
OfflinePlayer owner = itemcase.getOwner();
// Check if this player owns this itemcase.
if(!owner.equals(player)) {
// Check if player is allowed to destroy other peoples itemcases.
if(!player.hasPermission("itemcase.destroy.other")) {
// Show message.
chatLogger.message(player, "command.destroy.not-owner");
// Exit.
return;
}
}
// Destroy itemcase.
manager.destroyItemcase(itemcase);
// Show message.
chatLogger.message(player, "command.destroy.success");
}
}

View File

@ -82,6 +82,29 @@ public class WorldFile extends ConfigurationFile {
this.save(ItemCaseCore.instance);
}
/**
* Deletes the given Itemcase from config.
*
* @param itemcase Itemcase.
* @throws IOException
*/
public void deleteItemcase(Itemcase itemcase) throws IOException {
// Get block coordinates of Itemcase.
int blockX = itemcase.getLocation().getBlockX();
int blockY = itemcase.getLocation().getBlockY();
int blockZ = itemcase.getLocation().getBlockZ();
// Create a unique key for this itemcase based on location.
String key = "itemcases." + blockX + "/" + blockY + "/" + blockZ;
// Delete itemcase.
this.file.set(key, null);
// Attempt to save to file.
this.save(ItemCaseCore.instance);
}
/**
* Attempts to load all itemcases from the config.
*

View File

@ -134,6 +134,36 @@ public final class ItemcaseManager {
}
}
/**
* Destroy given Itemcase.
*
* @param itemcase Itemcase.
*/
public void destroyItemcase(Itemcase itemcase) {
// Despawn Itemcase's item.
itemcase.despawnItem();
// Remove itemcase from list.
this.itemcases.remove(itemcase);
// Get config file for itemcase's world.
WorldFile file = this.worldFiles.get(itemcase.getLocation().getWorld());
// Attempt to delete itemcase.
try {
// Delete itemcase.
file.deleteItemcase(itemcase);
} catch (IOException e) {
// Log error.
ItemCaseCore.instance.getConsoleLogger().severe(
"Failed to delete itemcase from config.", e);
}
}
/**
* Unload all currently loaded Itemcases.
*/
@ -183,6 +213,29 @@ public final class ItemcaseManager {
return false;
}
/**
* Attempt to get the itemcase at the given location.
*
* @param location Location.
* @return ItemCase.
*/
public Itemcase getItemcase(Location location) {
// For every itemcase.
for(Itemcase itemcase : this.itemcases) {
// Check if location matches.
if(itemcase.getLocation().equals(location)) {
// Return itemcase.
return itemcase;
}
}
// No itemcase found.
return null;
}
/**
* @return A list of all active Itemcase instances.
*/

View File

@ -51,4 +51,18 @@ command:
main-hand: "You must be holding something in your main hand."
# Shown to the player upon successful creation.
success: "ItemCase created."
success: "ItemCase created."
# Messages shown when using the 'destroy' command.
destroy:
# Shown when a player uses the destroy command but is not looking at
# an itemcase.
invalid-location: "This block is not an ItemCase."
# Shown when a player tries to destroy another persons itemcase when
# they do not have permission itemcase.destroy.other.
not-owner: "You do not own this ItemCase."
# Shown to the player upon successful deletion.
success: "ItemCase destroyed."