Implement storage command.

This commit is contained in:
Jesse Prescott 2018-06-04 03:09:25 +01:00
parent 788eac8469
commit ff3c3e3888
9 changed files with 219 additions and 15 deletions

View File

@ -17,6 +17,7 @@ package com.gmail.bleedobsidian.itemcase;
import com.gmail.bleedobsidian.itemcase.commands.CreateCommand;
import com.gmail.bleedobsidian.itemcase.commands.ModifyCommand;
import com.gmail.bleedobsidian.itemcase.commands.DestroyCommand;
import com.gmail.bleedobsidian.itemcase.commands.StorageCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
@ -55,6 +56,9 @@ public final class CommandHandler implements CommandExecutor {
case "modify":
new ModifyCommand().execute(sender, label, args);
break;
case "storage":
new StorageCommand().execute(sender, label, args);
break;
default:
this.showHelp(sender, label);
break;

View File

@ -133,7 +133,7 @@ public final class ItemCaseCore extends JavaPlugin {
this.itemcaseManager.unloadItemcases();
// Log.
this.consoleLogger.info("config.info.unloaded");
this.consoleLogger.info("console.info.unloaded");
}
/**

View File

@ -49,6 +49,11 @@ public final class Itemcase {
//TODO: Despawn items that were left from a config delete.
/**
* The name of the inventory for Itemcase storage.
*/
public static final String INVENTORY_NAME = "ItemCase Storage";
/**
* Types of Itemcase.
*/

View File

@ -81,7 +81,7 @@ public final class ModifyCommand implements Command {
// Get the players target block.
target = player.getTargetBlock(null, 3).getLocation();
// Check if itemcase already exists here.
// Check if itemcase exists here.
if(!ItemCaseCore.instance.getItemcaseManager().isItemcase(target)) {
// Show message.
@ -262,7 +262,7 @@ public final class ModifyCommand implements Command {
// Set storage.
itemcase.setStorage(Bukkit.createInventory(
null, 54, "ItemCase Storage"));
null, 54, Itemcase.INVENTORY_NAME));
}
// Save itemcase.
@ -290,7 +290,7 @@ public final class ModifyCommand implements Command {
if(itemcase.getType() == Type.SHOWCASE) {
// Show message.
chatLogger.message(player, "command.modify.not-shop");
chatLogger.message(player, "command.not-shop");
// Exit.
return;
@ -372,7 +372,7 @@ public final class ModifyCommand implements Command {
if(itemcase.getType() == Type.SHOWCASE) {
// Show message.
chatLogger.message(player, "command.modify.not-shop");
chatLogger.message(player, "command.not-shop");
// Exit.
return;
@ -442,7 +442,7 @@ public final class ModifyCommand implements Command {
if(itemcase.getType() == Type.SHOWCASE) {
// Show message.
chatLogger.message(player, "command.modify.not-shop");
chatLogger.message(player, "command.not-shop");
// Exit.
return;

View File

@ -0,0 +1,125 @@
/*
* 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.Itemcase.StorageType;
import com.gmail.bleedobsidian.itemcase.Itemcase.Type;
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 StorageCommand 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.create")) {
// Send message.
chatLogger.message(player, "command.permission");
// Exit.
return;
}
// The target location.
Location target = null;
// Get the players target block.
target = player.getTargetBlock(null, 3).getLocation();
// Check if itemcase exists here.
if(!ItemCaseCore.instance.getItemcaseManager().isItemcase(target)) {
// Show message.
chatLogger.message(player, "command.invalid-location");
// Exit.
return;
}
// Get itemcase.
Itemcase itemcase =
ItemCaseCore.instance.getItemcaseManager().getItemcase(target);
// Check if itemcase is a shop.
if(itemcase.getType() == Type.SHOWCASE) {
// Show message.
chatLogger.message(player, "command.not-shop");
// Exit.
return;
}
// If itemcase has infinite storage.
if(itemcase.getStorageType() == StorageType.INFINITE) {
// Show message.
chatLogger.message(player, "command.storage.infinite");
// Exit.
return;
}
// Get owner.
OfflinePlayer owner = itemcase.getOwner();
// Check if this player owns this itemcase.
if(!owner.equals(player)) {
// Check if player is allowed to modify other peoples itemcases.
if(!player.hasPermission("itemcase.modify.other")) {
// Show message.
chatLogger.message(player, "command.not-owner");
// Exit.
return;
}
}
// Open itemcase storage to player.
player.openInventory(itemcase.getStorage());
}
}

View File

@ -32,6 +32,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.World;
import org.bukkit.configuration.MemorySection;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
@ -223,7 +224,7 @@ public class WorldFile extends ConfigurationFile {
// Deserialse inventory.
Inventory inventory = this.deserializeInventory(
this.file.getConfigurationSection(
key + "shop.storage").getValues(true));
key + "shop.storage").getValues(false));
// Set inventory.
itemcase.setStorage(inventory);
@ -341,9 +342,12 @@ public class WorldFile extends ConfigurationFile {
// Get slot.
int slot = Integer.parseInt(entry.getKey());
// Get memory section.
MemorySection section = (MemorySection) entry.getValue();
// Deserialize item.
ItemStack item = ItemStack.deserialize(
(Map<String, Object>) entry.getValue());
ItemStack item = ItemStack.deserialize((Map<String, Object>)
section.getValues(true));
// Set item in slot.
inventory.setItem(slot, item);

View File

@ -16,12 +16,16 @@ package com.gmail.bleedobsidian.itemcase.managers;
import com.gmail.bleedobsidian.itemcase.ItemCaseCore;
import com.gmail.bleedobsidian.itemcase.Itemcase;
import com.gmail.bleedobsidian.itemcase.Itemcase.StorageType;
import com.gmail.bleedobsidian.itemcase.Itemcase.Type;
import com.gmail.bleedobsidian.itemcase.configurations.WorldFile;
import com.onarandombox.MultiverseCore.event.MVWorldDeleteEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
@ -29,6 +33,7 @@ import org.bukkit.World;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.world.WorldLoadEvent;
import org.bukkit.inventory.ItemStack;
@ -346,6 +351,59 @@ public final class ItemcaseManager {
// Remove all itemcases that were in this world from list.
ItemcaseManager.this.itemcases.removeAll(itemcases);
}
@EventHandler(priority = EventPriority.MONITOR)
public void onInventoryCloseEvent(InventoryCloseEvent event) {
// Get inventory name.
String name = event.getInventory().getName();
// If inventory is Itemcase inventory.
if(!name.equals(Itemcase.INVENTORY_NAME)) {
// Exit.
return;
}
// For every Itemcase.
for(Itemcase itemcase : ItemcaseManager.this.itemcases) {
// If itemcase is not a shop it wont have storage.
if(itemcase.getType() == Type.SHOWCASE) {
// Skip.
continue;
}
// If itemcase is inifinite, it wont have storage.
if(itemcase.getStorageType() == StorageType.INFINITE) {
// Skip.
continue;
}
// If inventory belongs to this itemcase.
if(itemcase.getStorage().equals(event.getInventory())) {
// Get world file.
WorldFile file = ItemcaseManager.this.worldFiles.get(
itemcase.getLocation().getWorld());
// Attempt to save itemcase.
try {
// Save itemcase.
file.saveItemcase(itemcase);
} catch (IOException e) {
// Log error.
ItemCaseCore.instance.getConsoleLogger().severe(
"Failed to save itemcase after storage change.", e);
}
}
}
}
}
}

View File

@ -42,6 +42,11 @@ command:
# Shown when a player uses a command but is not looking at an itemcase.
invalid-location: "This block is not an ItemCase."
# Shown to the player when trying to set storage type on an itemcase
# that is not a shop or access the storage of an itemcase that is not a
# shop.
not-shop: "This ItemCase is not a shop."
# Messages shown when using the 'create' command.
create:
@ -69,10 +74,6 @@ command:
# Messages shwon when using the 'modify' command.
modify:
# Shown to the player when trying to set storage type on an itemcase
# that is not a shop.
not-shop: "This ItemCase is not a shop."
# Shown to the player when trying to set the sell price of an ItemCase
# that only buys items.
sell-only: "This ItemCase only sells items."
@ -85,4 +86,11 @@ command:
invalid-price: "Invalid price."
# Shown to the player upon successful modification.
success: "ItemCase flag modified."
success: "ItemCase flag modified."
# Messages shown when using the 'storage' command.
storage:
# Shown to the player when trying to access the storage of an infinite
# itemcase shop.
infinite: "This ItemCase has infinite items to buy/sell, therefore has no storage."

View File

@ -24,6 +24,6 @@ permissions:
itemcase.create.shop.infinite:
description: Create an itemcase shop that has infinite items to buy/sell.
itemcase.modify.other:
description: Modify flags of itemcases you don't own.
description: Modify flags and access storage of itemcases you don't own.
itemcase.destroy.other:
description: Destroy an itemcase you don't own.