Skip to content

Commit

Permalink
Play chest opening and closing animation
Browse files Browse the repository at this point in the history
  • Loading branch information
rutgerkok committed Mar 26, 2013
1 parent c5cf2e4 commit 03e369e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/nl/rutgerkok/betterenderchest/BetterEnderChest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import nl.rutgerkok.betterenderchest.nms.NMSHandler;

import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin;

public interface BetterEnderChest {
/**
Expand Down Expand Up @@ -94,6 +95,13 @@ public interface BetterEnderChest {
*/
NMSHandler getNMSHandler();

/**
* Gets the plugin that is implementing this interface.
*
* @return The plugin that is implementing this interface.
*/
JavaPlugin getPlugin();

/**
* Returns the plugin folder, in which the config.yml is stored. Chest are
* stored in getChestSaveLocation().
Expand Down
8 changes: 6 additions & 2 deletions src/nl/rutgerkok/betterenderchest/BetterEnderChestPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public NMSHandler getNMSHandler() {
return nmsHandler;
}

@Override
public JavaPlugin getPlugin() {
return this;
}

@Override
public File getPluginFolder() {
return getDataFolder();
Expand Down Expand Up @@ -147,8 +152,7 @@ public void initConfig() {
SaveLocation saveLocation = SaveLocation.getSaveLocation(givenSaveLocation);

if (saveLocation == null) {
log(getConfig().getString("BetterEnderChest.saveFolderLocation")
+ " is not a valid save location. Defaulting to PLUGIN_FOLDER.", Level.WARNING);
log(givenSaveLocation + " is not a valid save location. Defaulting to " + defaultSaveLocation + ".", Level.WARNING);
saveLocation = SaveLocation.getDefaultSaveLocation();
}
chestSaveLocation = saveLocation.getFolder(this);
Expand Down
60 changes: 60 additions & 0 deletions src/nl/rutgerkok/betterenderchest/BetterEnderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;

import nl.rutgerkok.betterenderchest.io.BetterEnderIOLogic;
Expand All @@ -13,12 +14,20 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.metadata.MetadataValue;
import org.bukkit.metadata.Metadatable;

/**
* Various utilities used in the BetterEnderChest plugin.
*
*/
public class BetterEnderUtils {
// Constants for the metadata key names
private static final String LAST_CHEST_X = "BECLastChestX";
private static final String LAST_CHEST_Y = "BECLastChestY";
private static final String LAST_CHEST_Z = "BECLastChestZ";

/**
* Closes the inventory for all the viewers. Always call this before
* deleting it!
Expand Down Expand Up @@ -113,6 +122,35 @@ public static Inventory getCorrectlyResizedInventory(Player player, Inventory in
return inventory;
}

/**
* Returns the last location where the player opened an Ender Chest, or null
* if it wasn't found.
*
* @param player
* The player.
* @return The last location where the player opened an Ender Chest.
*/
public static Location getLastEnderChestOpeningLocation(Player player) {
MetadataValue x = getMetadataValue(LAST_CHEST_X, player);
MetadataValue y = getMetadataValue(LAST_CHEST_Y, player);
MetadataValue z = getMetadataValue(LAST_CHEST_Z, player);
if (x != null && y != null && z != null) {
return new Location(player.getWorld(), x.asInt(), y.asInt(), z.asInt());
}
return null;
}

private static MetadataValue getMetadataValue(String key, Metadatable lookup) {
if (lookup == null) {
return null;
}
List<MetadataValue> values = lookup.getMetadata(key);
if (values == null || values.size() == 0) {
return null;
}
return values.get(0);
}

/**
* Returns a resized inventory. Returns null if nothing had to be resized.
*
Expand Down Expand Up @@ -155,4 +193,26 @@ private static Inventory getResizedEmptyInventory(Player player, Inventory inven
return null;
}

/**
* Sets the location of the player where he/she last opened an Ender Chest.
* Use null as the location to clear the stored chest location.
*
* @param player
* The player that opened the Ender Chest.
* @param location
* Where the Ender Chest was.
* @param plugin
* The BetterEnderChest interface.
*/
public static void setLastEnderChestOpeningLocation(Player player, Location location, BetterEnderChest plugin) {
player.removeMetadata(LAST_CHEST_X, plugin.getPlugin());
player.removeMetadata(LAST_CHEST_Y, plugin.getPlugin());
player.removeMetadata(LAST_CHEST_Z, plugin.getPlugin());
if (location != null) {
player.setMetadata(LAST_CHEST_X, new FixedMetadataValue(plugin.getPlugin(), location.getBlockX()));
player.setMetadata(LAST_CHEST_Y, new FixedMetadataValue(plugin.getPlugin(), location.getBlockY()));
player.setMetadata(LAST_CHEST_Z, new FixedMetadataValue(plugin.getPlugin(), location.getBlockZ()));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Sound;
import org.bukkit.block.Block;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
Expand Down Expand Up @@ -126,7 +126,7 @@ public void onCraftItem(CraftItemEvent event) {
}
}

// Play sound and show warning message for public chests
// Play animation and show warning message for public chests
@EventHandler
public void onInventoryClose(InventoryCloseEvent event) {
if (!(event.getPlayer() instanceof Player)) {
Expand All @@ -135,8 +135,14 @@ public void onInventoryClose(InventoryCloseEvent event) {
Player player = (Player) event.getPlayer();
if (event.getInventory().getHolder() instanceof BetterEnderInventoryHolder) {

// Play closing sound
player.getWorld().playSound(player.getLocation(), Sound.CHEST_CLOSE, 1.0F, 1.0F);
// Play closing animation
Location lastOpened = BetterEnderUtils.getLastEnderChestOpeningLocation(player);
if (lastOpened != null) {
plugin.getNMSHandler().closeEnderChest(lastOpened);

// Clear the inventory opening location
BetterEnderUtils.setLastEnderChestOpeningLocation(player, null, plugin);
}

// If it's a public chest, show a warning about that
BetterEnderInventoryHolder holder = (BetterEnderInventoryHolder) event.getInventory().getHolder();
Expand All @@ -148,6 +154,9 @@ public void onInventoryClose(InventoryCloseEvent event) {
}
}

/*
* Takes over vanilla Ender Chest if another plugin opened them.
*/
@EventHandler
public void onInventoryOpen(InventoryOpenEvent event) {
if (!(event.getPlayer() instanceof Player)) {
Expand All @@ -156,11 +165,6 @@ public void onInventoryOpen(InventoryOpenEvent event) {

Player player = (Player) event.getPlayer();

// Check for BetterEnderChests
if (event.getInventory().getHolder() instanceof BetterEnderInventoryHolder) {
player.getWorld().playSound(player.getLocation(), Sound.CHEST_OPEN, 1.0F, 1.0F);
}

// Check for vanilla Ender Chests
if (plugin.getCompabilityMode() && event.getInventory().getType().equals(InventoryType.ENDER_CHEST)) {
// Plugin opened the vanilla Ender Chest, take it over
Expand Down Expand Up @@ -194,7 +198,6 @@ public void onInventoryOpen(InventoryOpenEvent event) {
Inventory inventory = chests.getInventory(inventoryName, plugin.getWorldGroupManager().getGroup(player.getWorld().getName()));
player.openInventory(inventory);
}

}

// Makes sure the chests show up
Expand All @@ -211,19 +214,20 @@ public void onPlayerInteract(PlayerInteractEvent event) {

// Some objects
Player player = event.getPlayer();
Block clickedBlock = event.getClickedBlock();
String groupName = plugin.getWorldGroupManager().getGroup(player.getWorld().getName());
String inventoryName = "";

// Find out the inventory that should be opened
if (protectionBridge.isProtected(event.getClickedBlock())) {
if (protectionBridge.isProtected(clickedBlock)) {
// Protected Ender Chest
if (protectionBridge.canAccess(player, event.getClickedBlock())) {
if (protectionBridge.canAccess(player, clickedBlock)) {
// player can access the chest
if (player.hasPermission("betterenderchest.user.open.privatechest")) {
// and has the correct permission node

// Get the owner's name
inventoryName = protectionBridge.getOwnerName(event.getClickedBlock());
inventoryName = protectionBridge.getOwnerName(clickedBlock);
} else {
// Show an error
player.sendMessage(ChatColor.RED + "You do not have permissions to use your private Ender Chest.");
Expand Down Expand Up @@ -261,6 +265,10 @@ public void onPlayerInteract(PlayerInteractEvent event) {

// Show the inventory
player.openInventory(inventory);

// Play animation, store location
plugin.getNMSHandler().openEnderChest(clickedBlock.getLocation());
BetterEnderUtils.setLastEnderChestOpeningLocation(player, clickedBlock.getLocation(), plugin);
}

}

0 comments on commit 03e369e

Please sign in to comment.