Skip to content

Commit

Permalink
Merge pull request #98 from TiagoFar78/chests
Browse files Browse the repository at this point in the history
Chests
  • Loading branch information
iquelli authored Apr 1, 2024
2 parents b759407 + 5d23c6e commit b40820a
Show file tree
Hide file tree
Showing 16 changed files with 338 additions and 258 deletions.
28 changes: 21 additions & 7 deletions src/main/java/net/tiagofar78/prisonescape/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import net.tiagofar78.prisonescape.bukkit.BukkitItems;
import net.tiagofar78.prisonescape.game.PrisonEscapeGame;
import net.tiagofar78.prisonescape.game.PrisonEscapeItem;
import net.tiagofar78.prisonescape.game.prisonbuilding.ClickReturnAction;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.GameManager;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
Expand All @@ -22,6 +22,7 @@
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.weather.WeatherChangeEvent;
import org.bukkit.inventory.ItemStack;

public class Events implements Listener {

Expand Down Expand Up @@ -69,10 +70,9 @@ public void playerInteractWithPrison(PlayerInteractEvent e) {
);
PrisonEscapeLocation location = new PrisonEscapeLocation(block.getX(), block.getY(), block.getZ());

if (block.getType() == Material.CHEST) {
game.playerInteractWithPrison(e.getPlayer().getName(), location, itemInHand);
int returnCode = game.playerInteractWithPrison(e.getPlayer().getName(), location, itemInHand);
if (returnCode == 0) {
e.setCancelled(true);
return;
}
}

Expand Down Expand Up @@ -108,6 +108,7 @@ public void playerCloseInventory(InventoryCloseEvent e) {
game.playerCloseMenu(e.getPlayer().getName());
}

@SuppressWarnings("deprecation")
@EventHandler
public void playerClickInventory(InventoryClickEvent e) {
PrisonEscapeGame game = GameManager.getGame();
Expand All @@ -124,9 +125,22 @@ public void playerClickInventory(InventoryClickEvent e) {
}

PrisonEscapeItem item = BukkitItems.convertToPrisonEscapeItem(e.getCursor());
int returnCode = game.playerClickMenu(e.getWhoClicked().getName(), e.getSlot(), item);
if (returnCode == -1) {
e.setCancelled(true);
ClickReturnAction returnAction = game.playerClickMenu(e.getWhoClicked().getName(), e.getSlot(), item);
if (returnAction == ClickReturnAction.IGNORE) {
return;
}

e.setCancelled(true);

if (returnAction == ClickReturnAction.DELETE_HOLD_AND_SELECTED) {
e.setCursor(null);
e.setCurrentItem(null);
} else if (returnAction == ClickReturnAction.CHANGE_HOLD_AND_SELECTED) {
ItemStack cursor = e.getCursor();
ItemStack current = e.getCurrentItem();

e.setCursor(current);
e.setCurrentItem(cursor);
}
}

Expand Down

This file was deleted.

60 changes: 60 additions & 0 deletions src/main/java/net/tiagofar78/prisonescape/bukkit/BukkitMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

public class BukkitMenu {

private static final int SLOTS_PER_LINE = 9;
private static final ItemStack GLASS_ITEM = createGlassItem();

private static ItemStack createGlassItem() {
Expand All @@ -25,6 +26,10 @@ private static ItemStack createGlassItem() {
return item;
}

// #########################################
// # Vault #
// #########################################

private static final int[] NON_HIDDEN_ITEMS_INDEXES = {9 + 2, 9 + 3, 9 + 5, 9 + 6};
private static final int HIDDEN_ITEM_INDEX = 9 * 4 + 4;

Expand Down Expand Up @@ -94,4 +99,59 @@ private static ItemStack createHiddenIndicatorItem(MessageLanguageManager messag
return item;
}

// #########################################
// # Chest #
// #########################################

private static final int[] CHEST_CONTENT_INDEXES =
{SLOTS_PER_LINE * 1 + 2, SLOTS_PER_LINE * 1 + 3, SLOTS_PER_LINE * 1 + 4, SLOTS_PER_LINE * 1 + 5, SLOTS_PER_LINE * 1 + 6};

public static void openChest(String playerName, List<PrisonEscapeItem> contents) {
Player bukkitPlayer = Bukkit.getPlayer(playerName);
if (bukkitPlayer == null || !bukkitPlayer.isOnline()) {
return;
}

MessageLanguageManager messages = MessageLanguageManager.getInstanceByPlayer(playerName);

int lines = 3;
String title = messages.getContainerName();
Inventory inv = Bukkit.createInventory(bukkitPlayer, lines * SLOTS_PER_LINE, title);

for (int i = 0; i < lines * SLOTS_PER_LINE; i++) {
inv.setItem(i, GLASS_ITEM);
}

for (int i = 0; i < contents.size(); i++) {
ItemStack item = BukkitItems.convertToItemStack(contents.get(i));
inv.setItem(CHEST_CONTENT_INDEXES[i], item);
}

bukkitPlayer.openInventory(inv);
}

public static int convertToIndexChest(int slot) {
for (int i = 0; i < CHEST_CONTENT_INDEXES.length; i++) {
if (CHEST_CONTENT_INDEXES[i] == slot) {
return i;
}
}

return -1;
}

// ########################################
// # Player Inventory #
// ########################################

public static void setItem(String playerName, int slot, PrisonEscapeItem item) {
Player bukkitPlayer = Bukkit.getPlayer(playerName);
if (bukkitPlayer == null || !bukkitPlayer.isOnline()) {
return;
}

ItemStack bukkitItem = BukkitItems.convertToItemStack(item);
bukkitPlayer.getInventory().setItem(slot, bukkitItem);
}

}
7 changes: 0 additions & 7 deletions src/main/java/net/tiagofar78/prisonescape/game/MenuType.java

This file was deleted.

Loading

0 comments on commit b40820a

Please sign in to comment.