fixed item duping bug when clicking items in personal inventory, added ability to purchase special utility items vian custom knowledge books
This commit is contained in:
parent
30b9370ca9
commit
fea684d56f
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
name: CoswayUtil
|
name: CoswayUtil
|
||||||
version: '1.11-RELEASE'
|
version: '1.12-RELEASE'
|
||||||
main: CoswayUtil.CoswayUtil
|
main: CoswayUtil.CoswayUtil
|
||||||
description: "utility plugin for Cosway servers, a yescraft network server"
|
description: "utility plugin for Cosway servers, a yescraft network server"
|
||||||
api-version: '1.21'
|
api-version: '1.21'
|
||||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -128,15 +128,7 @@ public class BlockShop implements Listener {
|
|||||||
config = plugin.getConfig(); // Reassign the updated config
|
config = plugin.getConfig(); // Reassign the updated config
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper method to add items to the shop
|
|
||||||
private void addItemToShop(Inventory shop, Material material, String displayName, double price) {
|
|
||||||
ItemStack item = new ItemStack(material);
|
|
||||||
ItemMeta meta = item.getItemMeta();
|
|
||||||
meta.setDisplayName(displayName);
|
|
||||||
meta.setLore(Collections.singletonList(ChatColor.GOLD + "Price: " + price + " currency"));
|
|
||||||
item.setItemMeta(meta);
|
|
||||||
shop.addItem(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle item right-click to open the shop
|
// Handle item right-click to open the shop
|
||||||
@EventHandler
|
@EventHandler
|
||||||
@ -152,14 +144,21 @@ public class BlockShop implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle purchases from the shop
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onInventoryClick(InventoryClickEvent event) {
|
public void onInventoryClick(InventoryClickEvent event) {
|
||||||
Player player = (Player) event.getWhoClicked();
|
Player player = (Player) event.getWhoClicked();
|
||||||
InventoryView clickedInventory = event.getWhoClicked().getOpenInventory();
|
InventoryView clickedInventory = event.getWhoClicked().getOpenInventory();
|
||||||
|
Inventory topInventory = clickedInventory.getTopInventory(); // The shop inventory
|
||||||
|
Inventory clickedSlotInventory = event.getClickedInventory(); // Where the click happened
|
||||||
|
|
||||||
|
// Ensure the clicked inventory is the Block Shop (not player's inventory)
|
||||||
if (clickedInventory.getTitle().equals(ChatColor.GREEN + "Block Shop")) {
|
if (clickedInventory.getTitle().equals(ChatColor.GREEN + "Block Shop")) {
|
||||||
event.setCancelled(true); // Prevent item from being moved
|
event.setCancelled(true); // Prevent item movement
|
||||||
|
|
||||||
|
// Ignore clicks outside the shop inventory
|
||||||
|
if (clickedSlotInventory == null || !clickedSlotInventory.equals(topInventory)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ItemStack clickedItem = event.getCurrentItem();
|
ItemStack clickedItem = event.getCurrentItem();
|
||||||
if (clickedItem != null && clickedItem.getType() != Material.AIR) {
|
if (clickedItem != null && clickedItem.getType() != Material.AIR) {
|
||||||
@ -170,25 +169,26 @@ public class BlockShop implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Create a new ItemStack with the default meta
|
// Create a new ItemStack with the default meta
|
||||||
ItemStack sold = new ItemStack(clickedItem.getType(), clickedItem.getAmount());
|
ItemStack sold = clickedItem.clone();
|
||||||
|
|
||||||
// Reset item meta (default name, no lore)
|
// Only reset the meta if the item is NOT a Knowledge Book
|
||||||
ItemMeta defaultMeta = sold.getItemMeta();
|
if (clickedItem.getType() != Material.KNOWLEDGE_BOOK) {
|
||||||
if (clickedItem.getType() != Material.KNOWLEDGE_BOOK && defaultMeta != null) {
|
ItemMeta defaultMeta = sold.getItemMeta();
|
||||||
defaultMeta.setDisplayName(null); // Reset to default name
|
if (defaultMeta != null) {
|
||||||
defaultMeta.setLore(null); // Remove lore
|
defaultMeta.setDisplayName(null);
|
||||||
sold.setItemMeta(defaultMeta);
|
defaultMeta.setLore(null);
|
||||||
|
sold.setItemMeta(defaultMeta);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if player's inventory has space before proceeding
|
// Check if player's inventory has space before proceeding
|
||||||
HashMap<Integer, ItemStack> leftover = player.getInventory().addItem(sold);
|
HashMap<Integer, ItemStack> leftover = player.getInventory().addItem(sold);
|
||||||
if (!leftover.isEmpty()) {
|
if (!leftover.isEmpty()) {
|
||||||
// If there are leftover items, inventory is full
|
|
||||||
player.sendMessage(ChatColor.RED + getConfigLine("inventory_full", "Your inventory is full! Purchase failed."));
|
player.sendMessage(ChatColor.RED + getConfigLine("inventory_full", "Your inventory is full! Purchase failed."));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If inventory had space, finalize the purchase
|
// Finalize the purchase
|
||||||
economy.withdrawPlayer(player, price);
|
economy.withdrawPlayer(player, price);
|
||||||
player.sendMessage(ColorKey("&aYou bought &b" + clickedItem.getAmount() + " &7" +
|
player.sendMessage(ColorKey("&aYou bought &b" + clickedItem.getAmount() + " &7" +
|
||||||
String.valueOf(sold.getType()).toLowerCase().replace("_", " ") +
|
String.valueOf(sold.getType()).toLowerCase().replace("_", " ") +
|
||||||
@ -197,6 +197,7 @@ public class BlockShop implements Listener {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public String ColorKey(String t) {
|
public String ColorKey(String t) {
|
||||||
char searchChar = '&'; // Character to search for
|
char searchChar = '&'; // Character to search for
|
||||||
char replacementChar = '§'; // Character to replace with
|
char replacementChar = '§'; // Character to replace with
|
||||||
|
@ -20,7 +20,9 @@ import org.bukkit.event.entity.EntityExplodeEvent;
|
|||||||
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
import org.bukkit.event.player.PlayerChangedWorldEvent;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
import org.bukkit.inventory.EquipmentSlot;
|
import org.bukkit.inventory.EquipmentSlot;
|
||||||
|
import org.bukkit.inventory.Inventory;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.ItemMeta;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
@ -33,6 +35,7 @@ import org.bukkit.block.data.type.RespawnAnchor;
|
|||||||
import CoswayUtil.GravityGauntletCommand;
|
import CoswayUtil.GravityGauntletCommand;
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -99,6 +102,36 @@ public final class CoswayUtil extends JavaPlugin implements Listener {
|
|||||||
this.getCommand("gravitygauntlet").setExecutor(new GravityGauntletCommand());
|
this.getCommand("gravitygauntlet").setExecutor(new GravityGauntletCommand());
|
||||||
this.getCommand("getwand").setExecutor(new GiveWandCommand());
|
this.getCommand("getwand").setExecutor(new GiveWandCommand());
|
||||||
getServer().getPluginManager().registerEvents(new LaunchStick(this), this);
|
getServer().getPluginManager().registerEvents(new LaunchStick(this), this);
|
||||||
|
new BukkitRunnable() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||||
|
if(removeCustomKnowledgeBook(player,ChatColor.GREEN+"Gravity Gauntlet")) {
|
||||||
|
ItemStack gauntlet = new ItemStack(Material.NETHERITE_HOE); // You can change this to any item you prefer
|
||||||
|
ItemMeta meta = gauntlet.getItemMeta();
|
||||||
|
|
||||||
|
if (meta != null) {
|
||||||
|
meta.setDisplayName(ChatColor.LIGHT_PURPLE + "Gravity Gauntlet");
|
||||||
|
meta.setLore(Collections.singletonList(ChatColor.GOLD + "Right Click to pull, Shift+Right Click to throw"));
|
||||||
|
meta.setUnbreakable(true);
|
||||||
|
gauntlet.setItemMeta(meta);
|
||||||
|
}
|
||||||
|
|
||||||
|
player.getInventory().addItem(gauntlet);
|
||||||
|
}
|
||||||
|
if(removeCustomKnowledgeBook(player,ChatColor.GREEN+"Launch Stick")) {
|
||||||
|
player.getInventory().addItem(LaunchStick.createLaunchStick());
|
||||||
|
}
|
||||||
|
if(removeCustomKnowledgeBook(player,ChatColor.GREEN+"Levitation Wand")) {
|
||||||
|
player.getInventory().addItem(MobLevitationWand.createWand());
|
||||||
|
}
|
||||||
|
if(removeCustomKnowledgeBook(player,ChatColor.GREEN+"Rapid Fire Bow")) {
|
||||||
|
player.getInventory().addItem(RapidFireBow.createRapidFireBow());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.runTaskTimer(this,0,2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -196,6 +229,21 @@ public final class CoswayUtil extends JavaPlugin implements Listener {
|
|||||||
}
|
}
|
||||||
return sb.toString();
|
return sb.toString();
|
||||||
}
|
}
|
||||||
|
public boolean removeCustomKnowledgeBook(Player player, String customName) {
|
||||||
|
for (ItemStack item : player.getInventory().getContents()) {
|
||||||
|
if (item != null && item.getType() == Material.KNOWLEDGE_BOOK) {
|
||||||
|
ItemMeta meta = item.getItemMeta();
|
||||||
|
//player.sendMessage("you have a knowledge book with name: \n"+ meta.getDisplayName() + "\n"+customName+"\n do they match?");
|
||||||
|
if (meta != null && meta.hasDisplayName() && meta.getDisplayName().equals(customName)) {
|
||||||
|
player.getInventory().remove(item); // Remove the book
|
||||||
|
return true; // Found and removed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false; // No matching book found
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void serverMessage(String msg) {
|
public void serverMessage(String msg) {
|
||||||
getServer().broadcastMessage(prefix()+ColorKey(msg));
|
getServer().broadcastMessage(prefix()+ColorKey(msg));
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,28 @@ shop:
|
|||||||
material: OAK_PLANKS
|
material: OAK_PLANKS
|
||||||
price: 15
|
price: 15
|
||||||
display_name: "Oak Planks"
|
display_name: "Oak Planks"
|
||||||
|
utilities:
|
||||||
|
items:
|
||||||
|
grav:
|
||||||
|
material: KNOWLEDGE_BOOK
|
||||||
|
price: 1000000
|
||||||
|
quantity: 1
|
||||||
|
display_name: "Gravity Gauntlet"
|
||||||
|
wand:
|
||||||
|
material: KNOWLEDGE_BOOK
|
||||||
|
price: 1000000
|
||||||
|
quantity: 1
|
||||||
|
display_name: "Levitation Wand"
|
||||||
|
launch:
|
||||||
|
material: KNOWLEDGE_BOOK
|
||||||
|
price: 1000000
|
||||||
|
quantity: 1
|
||||||
|
display_name: "Launch Stick"
|
||||||
|
bow:
|
||||||
|
material: KNOWLEDGE_BOOK
|
||||||
|
price: 1000000
|
||||||
|
quantity: 1
|
||||||
|
display_name: "Rapid Fire Bow"
|
||||||
poor: "you're poor..."
|
poor: "you're poor..."
|
||||||
inventory_full: "you got too much clutter, cant fit your purchase into that mess..."
|
inventory_full: "you got too much clutter, cant fit your purchase into that mess..."
|
||||||
pagination:
|
pagination:
|
||||||
|
Loading…
Reference in New Issue
Block a user