Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shop menu and Money System #156

Merged
merged 9 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/main/java/net/tiagofar78/prisonescape/Events.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package net.tiagofar78.prisonescape;

import net.tiagofar78.prisonescape.game.PrisonEscapeGame;
import net.tiagofar78.prisonescape.game.prisonbuilding.ClickReturnAction;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.items.FunctionalItem;
import net.tiagofar78.prisonescape.items.Item;
import net.tiagofar78.prisonescape.items.ItemFactory;
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.GameManager;
import net.tiagofar78.prisonescape.menus.ClickReturnAction;

import org.bukkit.Location;
import org.bukkit.Material;
Expand All @@ -25,10 +25,12 @@
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerMoveEvent;
Expand Down Expand Up @@ -130,6 +132,11 @@ public void playerCloseInventory(InventoryCloseEvent e) {
return;
}


if (e.getInventory().getType() == InventoryType.PLAYER) {
return;
}

game.playerCloseMenu(e.getPlayer().getName());
}

Expand All @@ -144,6 +151,7 @@ public void playerClickInventory(InventoryClickEvent e) {
return;
}

Player player = (Player) e.getWhoClicked();
boolean isPlayerInv = false;
if (e.getClickedInventory().getType() == InventoryType.PLAYER) {
Inventory topInv = e.getView().getTopInventory();
Expand All @@ -157,13 +165,18 @@ public void playerClickInventory(InventoryClickEvent e) {
return;
}

if (e.getAction() == InventoryAction.DROP_ALL_SLOT || e.getAction() == InventoryAction.DROP_ONE_SLOT) {
int slot = e.getSlot();
game.playerDropItem(player.getName(), slot);
return;
}

isPlayerInv = true;
}

ItemStack cursor = e.getCursor();
ItemStack current = e.getCurrentItem();

Player player = (Player) e.getWhoClicked();
Item item = ItemFactory.createItem(e.getCursor());
ClickReturnAction returnAction = game.playerClickMenu(player.getName(), e.getSlot(), item, isPlayerInv);
if (returnAction == ClickReturnAction.IGNORE) {
Expand Down Expand Up @@ -296,4 +309,16 @@ public void onPlayerCombat(EntityDamageByEntityEvent e) {
}
}

@EventHandler
public void onPlayerDropItem(PlayerDropItemEvent e) {
PrisonEscapeGame game = GameManager.getGame();
if (game == null) {
return;
}
Player player = e.getPlayer();
int slot = player.getInventory().getHeldItemSlot();

game.playerDropItem(player.getName(), slot);
}

}
36 changes: 36 additions & 0 deletions src/main/java/net/tiagofar78/prisonescape/bukkit/BukkitMenu.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package net.tiagofar78.prisonescape.bukkit;

import net.tiagofar78.prisonescape.items.CameraItem;
import net.tiagofar78.prisonescape.items.EnergyDrinkItem;
import net.tiagofar78.prisonescape.items.Item;
import net.tiagofar78.prisonescape.items.RadarItem;
import net.tiagofar78.prisonescape.items.SensorItem;
import net.tiagofar78.prisonescape.items.TrapItem;
import net.tiagofar78.prisonescape.managers.MessageLanguageManager;

import org.bukkit.Bukkit;
Expand Down Expand Up @@ -163,4 +168,35 @@ public static int convertToIndexPlayerInventory(int slot) {
return -1;
}

// ########################################
// # Shop #
// ########################################

private static final int NUM_OF_ITEMS_FOR_SALE = 5;

public static void openShop(String playerName) {
Player bukkitPlayer = Bukkit.getPlayer(playerName);
if (bukkitPlayer == null || !bukkitPlayer.isOnline()) {
return;
}

Inventory shopMenu = Bukkit.createInventory(null, 9, "Buy Menu");
MessageLanguageManager messages = MessageLanguageManager.getInstanceByPlayer(playerName);

shopMenu.setItem(0, new EnergyDrinkItem().toItemStack(messages));
shopMenu.setItem(1, new TrapItem().toItemStack(messages));
shopMenu.setItem(2, new SensorItem().toItemStack(messages));
shopMenu.setItem(3, new CameraItem().toItemStack(messages));
shopMenu.setItem(4, new RadarItem().toItemStack(messages));

bukkitPlayer.openInventory(shopMenu);
}

public static int convertToIndexShop(int slot) {
if (slot >= NUM_OF_ITEMS_FOR_SALE) {
return -1;
}

return slot;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import net.tiagofar78.prisonescape.game.phases.Phase;
import net.tiagofar78.prisonescape.game.phases.Waiting;
import net.tiagofar78.prisonescape.game.prisonbuilding.Chest;
import net.tiagofar78.prisonescape.game.prisonbuilding.ClickReturnAction;
import net.tiagofar78.prisonescape.game.prisonbuilding.Clickable;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonBuilding;
import net.tiagofar78.prisonescape.game.prisonbuilding.PrisonEscapeLocation;
import net.tiagofar78.prisonescape.game.prisonbuilding.Vault;
Expand All @@ -25,6 +23,9 @@
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.GameManager;
import net.tiagofar78.prisonescape.managers.MessageLanguageManager;
import net.tiagofar78.prisonescape.menus.ClickReturnAction;
import net.tiagofar78.prisonescape.menus.Clickable;
import net.tiagofar78.prisonescape.menus.Shop;

import org.bukkit.block.Block;
import org.bukkit.event.player.PlayerInteractEvent;
Expand Down Expand Up @@ -680,6 +681,11 @@ private void playerOpenVault(PrisonEscapePlayer player, int vaultIndex, Item ite
vault.open(player);
}

public void playerDropItem(String playerName, int slot) {
PrisonEscapePlayer player = getPrisonEscapePlayer(playerName);
player.removeItem(slot);
iquelli marked this conversation as resolved.
Show resolved Hide resolved
}

private void policeSearchVault(PrisonEscapePlayer player, Vault vault, MessageLanguageManager messagesPolice) {
PrisonEscapePlayer vaultOwner = vault.getOwner();
MessageLanguageManager messagesPrisioner = MessageLanguageManager.getInstanceByPlayer(vaultOwner.getName());
Expand Down Expand Up @@ -732,6 +738,17 @@ public void playerDrankEnergyDrink(String playerName, int eneryDrinkIndex) {
player.removeItem(contentIndex);
}

public void policeOpenShop(String playerName) {
PrisonEscapePlayer player = getPlayerOnPoliceTeam(playerName);
if (player == null) {
return;
}

Shop shop = new Shop();
_playerOpenMenu.put(player.getName(), shop);
shop.open(player);
}

public void policeHandcuffedPrisioner(String policeName, String prisionerName) {
PrisonEscapePlayer police = getPrisonEscapePlayer(policeName);
PrisonEscapePlayer prisioner = getPrisonEscapePlayer(prisionerName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.tiagofar78.prisonescape.game;

import net.tiagofar78.prisonescape.bukkit.BukkitMenu;
import net.tiagofar78.prisonescape.items.CameraItem;
import net.tiagofar78.prisonescape.items.Item;
import net.tiagofar78.prisonescape.items.SensorItem;
import net.tiagofar78.prisonescape.items.TrapItem;
import net.tiagofar78.prisonescape.managers.ConfigManager;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -17,6 +21,11 @@ public class PrisonEscapePlayer {
private boolean _isOnline;
private boolean _hasEscaped;
private List<Item> _inventory;
private int _balance;

private int _numOfCamerasBought = 0;
private int _numOfSensorsBought = 0;
private int _numOfTrapsBought = 0;

public PrisonEscapePlayer(String name) {
_name = name;
Expand All @@ -25,6 +34,7 @@ public PrisonEscapePlayer(String name) {
_inRestrictedArea = false;
_isOnline = true;
_inventory = createInventory();
_balance = ConfigManager.getInstance().getStartingBalance();
}

private List<Item> createInventory() {
Expand Down Expand Up @@ -102,8 +112,12 @@ public void setItem(int index, Item item) {
BukkitMenu.setItem(_name, index, item);
}

public void removeItem(int index) {
setItem(index, null);
public void removeItem(int slot) {
int index = BukkitMenu.convertToIndexPlayerInventory(slot);
if (index == -1) {
return;
}
_inventory.set(index, null);
}

public boolean hasIllegalItems() {
Expand Down Expand Up @@ -146,6 +160,64 @@ public boolean canBeArrested() {
return _isWanted || _inRestrictedArea;
}

// ########################################
// # Balance #
// ########################################

public int getBalance() {
return _balance;
}

public void setBalance(int balance) {
_balance = balance;
}

public void increaseBalance(int amount) {
_balance += amount;
}

public void decreaseBalance(int amount) {
_balance -= amount;
}

public int buyItem(Item item, int price) {
if (!canBuyItem(item)) {
return -1;
}
if (price > _balance) {
return -2;
}

if (giveItem(item) == -1) {
return -3;
}

decreaseBalance(price);
updateItemCount(item);
return 0;
}

private boolean canBuyItem(Item item) {
if (item instanceof TrapItem && _numOfTrapsBought >= ((TrapItem) item).getLimit()) {
return false;
} else if (item instanceof CameraItem && _numOfCamerasBought >= ((CameraItem) item).getLimit()) {
return false;
} else if (item instanceof SensorItem && _numOfSensorsBought >= ((SensorItem) item).getLimit()) {
return false;
}
return true;
}

private void updateItemCount(Item item) {
if (item instanceof TrapItem) {
_numOfTrapsBought++;
} else if (item instanceof CameraItem) {
_numOfCamerasBought++;
} else if (item instanceof SensorItem) {
_numOfSensorsBought++;
}
}

// ########################################
// # Util #
// ########################################
Expand All @@ -155,4 +227,5 @@ public boolean equals(Object o) {
return o instanceof PrisonEscapePlayer && ((PrisonEscapePlayer) o).getName().equals(this.getName());
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.tiagofar78.prisonescape.items.NullItem;
import net.tiagofar78.prisonescape.managers.ConfigManager;
import net.tiagofar78.prisonescape.managers.MessageLanguageManager;
import net.tiagofar78.prisonescape.menus.ClickReturnAction;
import net.tiagofar78.prisonescape.menus.Clickable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -21,7 +23,7 @@ public class Chest implements Clickable {
private List<ItemProbability> _itemsProbability;
private boolean _isOpened;

protected Chest(String regionName) {
public Chest(String regionName) {
ConfigManager config = ConfigManager.getInstance();

this._contents = createContentsList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import net.tiagofar78.prisonescape.game.PrisonEscapePlayer;
import net.tiagofar78.prisonescape.items.Item;
import net.tiagofar78.prisonescape.items.NullItem;
import net.tiagofar78.prisonescape.menus.ClickReturnAction;
import net.tiagofar78.prisonescape.menus.Clickable;

import java.util.ArrayList;
import java.util.List;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/net/tiagofar78/prisonescape/items/Buyable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.tiagofar78.prisonescape.items;

public interface Buyable {

int getPrice();

int getLimit();

}
21 changes: 19 additions & 2 deletions src/main/java/net/tiagofar78/prisonescape/items/CameraItem.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package net.tiagofar78.prisonescape.items;

import net.tiagofar78.prisonescape.managers.ConfigManager;

import org.bukkit.Material;

public class CameraItem extends Item {
public class CameraItem extends Item implements Buyable {

@Override
public int getPrice() {
return ConfigManager.getInstance().getCameraPrice();
}

@Override
public int getLimit() {
return ConfigManager.getInstance().getCameraLimit();
}

@Override
public boolean isMetalic() {
Expand All @@ -16,7 +28,12 @@ public boolean isIllegal() {

@Override
public Material getMaterial() {
return Material.ENDER_EYE;
return Material.OBSERVER;
}

@Override
public boolean isBuyable() {
return true;
}

}
Loading
Loading