Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save odenforge/98f714d858579271c33c0aebb0f31d8e to your computer and use it in GitHub Desktop.
Save odenforge/98f714d858579271c33c0aebb0f31d8e to your computer and use it in GitHub Desktop.
Shop plugin event handle
package com.odenshop.shop;
import java.util.Arrays;
import java.util.List;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.effect.sound.SoundTypes;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.cause.Cause;
import org.spongepowered.api.event.filter.IsCancelled;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.item.inventory.ChangeInventoryEvent;
import org.spongepowered.api.event.item.inventory.ClickInventoryEvent;
import org.spongepowered.api.event.item.inventory.DropItemEvent;
import org.spongepowered.api.event.item.inventory.InteractInventoryEvent;
import org.spongepowered.api.item.ItemTypes;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.entity.Hotbar;
import org.spongepowered.api.item.inventory.type.GridInventory;
import org.spongepowered.api.service.economy.transaction.ResultType;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.util.Tristate;
import com.odenshop.OdenShops;
import com.odenshop.menulayout.MenuData;
import com.odenshop.menulayout.MenuLayout;
import com.odenshop.messages.Message;
import com.odenshop.messages.MessageTypesAdv;
import com.odenshop.messages.MessageTypesSimple;
import com.odenshop.shop.eco.MoneyHandler;
import com.odenshop.shop.util.ShopUtil;
import com.odenshop.shopkey.sKeyData;
import com.odenshop.shopkey.sKeys;
public class MenuEvents {
@Listener(order = Order.FIRST)
public void onClick(ClickInventoryEvent.Primary event, @First Player player) {
if(isInInventoryMenu(player) && OdenShops.Inst().getDataBase().isSave() == false){
MenuData Menu = OdenShops.Inst().getMenuStorage().get(player);
MenuLayout Layout = Menu.getLayout();
ItemStack Stack = event.getCursorTransaction().getDefault().createStack();
if(Stack.get(sKeys.StringKey).isPresent()){
String StrKey = Stack.get(sKeys.StringKey).orElse("null");
int IntKey = Stack.get(sKeys.IntKey).orElse(-1);
if(Layout == MenuLayout.Modify){
ShopItem iShop = Menu.getiShop();
iShop.setMod(true);
if(StrKey.equalsIgnoreCase("Plus")){
iShop.PlusMin(IntKey);
player.playSound(SoundTypes.BLOCK_NOTE_HAT, player.getLocation().getPosition(), 1);
}
else if(StrKey.equalsIgnoreCase("Minus")){
iShop.PlusMin(-IntKey);
player.playSound(SoundTypes.BLOCK_NOTE_BASEDRUM, player.getLocation().getPosition(), 1);
}
else if(StrKey.equalsIgnoreCase("Buy")){
player.playSound(SoundTypes.BLOCK_WOOD_BUTTON_CLICK_ON, player.getLocation().getPosition(), 1);
iShop.setBuySell(ShopUtil.Buy);
}
else if(StrKey.equalsIgnoreCase("Sell")){
player.playSound(SoundTypes.BLOCK_STONE_BUTTON_CLICK_ON, player.getLocation().getPosition(), 1);
iShop.setBuySell(ShopUtil.Sell);
}
else if(StrKey.equalsIgnoreCase("GiveStock")){
if(iShop.isCreated() == true){
Iterable<Slot> slotIter = player.getInventory().query(Hotbar.class,GridInventory.class).slots();
int messageLimiter=0;
boolean AddedStock = false;
for (Slot slot : slotIter) {
if (slot.contains(iShop.getItem())) {
iShop.setQnt(iShop.getQnt()+slot.getStackSize());
slot.clear();
AddedStock = true;
messageLimiter++;
if((messageLimiter % 2) == 0){
Message.Send(player, MessageTypesSimple.FoundTaking);
}
}
}
if(AddedStock == true){
Message.Send(player, MessageTypesSimple.FoundTaking);
}
else{
Message.Send(player, MessageTypesSimple.NothingFoundToAdd);
}
}else{
Message.Send(player, MessageTypesSimple.ListingNoExist);
}
}
else if(StrKey.equalsIgnoreCase("TakeStock")){
if(iShop.isCreated() == false){
Message.Send(player, MessageTypesSimple.ListingNoExist);
}
else if(iShop.getQnt() == 0){
Message.Send(player, MessageTypesSimple.NoMoreStock);
}
else{
boolean Done = iShop.OfferBack(player);
if(Done == false && iShop.getQnt() != 0){
Message.Send(player, MessageTypesSimple.CanNotGiveBackFull);
}
else if(Done == true && iShop.getQnt() == 0){
Message.Send(player, MessageTypesAdv.GaveAllStock, iShop);
}
}
}
else if(StrKey.equalsIgnoreCase("NotifyOn")){
player.playSound(SoundTypes.BLOCK_WOOD_BUTTON_CLICK_ON, player.getLocation().getPosition(), 1);
iShop.setNotify(true);
}
else if(StrKey.equalsIgnoreCase("NotifyOff")){
player.playSound(SoundTypes.BLOCK_STONE_BUTTON_CLICK_ON, player.getLocation().getPosition(), 1);
iShop.setNotify(false);
}
else if(StrKey.equalsIgnoreCase("RemoveShop")){
iClose(player);
iShop.setMod(false);
Menu.setLayout(MenuLayout.None);
boolean Done = true;
if(iShop.getQnt() != 0){
Done = iShop.OfferBack(player);
}
if(Done == false && iShop.getQnt() != 0){
Message.Send(player, MessageTypesAdv.CanNotRemoveListing, iShop);
}
else if(Done == true && iShop.getQnt() == 0){
if(OdenShops.Inst().getiShop().CheckShopItem(player.getName(),iShop) && OdenShops.Inst().getiShop().RemoveShop(player.getName(),iShop) == true){
Message.Send(player, MessageTypesAdv.RemovedListing,iShop);
}
}
}
else if(StrKey.equalsIgnoreCase("SaveShop")){
if(iShop.getBuyPrice() == 0 && iShop.getSellPrice() == 0){
player.playSound(SoundTypes.ENTITY_VILLAGER_NO, player.getLocation().getPosition(), 1);
}else if(iShop.getSellPrice() != 0 && iShop.getSellPrice() > iShop.getBuyPrice()){
Message.Send(player, MessageTypesSimple.SellMoreThenBuy);
}else{
player.playSound(SoundTypes.ENTITY_VILLAGER_YES, player.getLocation().getPosition(), 1);
OdenShops.Inst().getDataBase().Save(iShop);
iClose(player);
Menu.setLayout(MenuLayout.None);
if(iShop.isCreated() == false){
iShop.setMod(false);
iShop.setCreated(true);
Iterable<Slot> slotIter = player.getInventory().query(Hotbar.class,GridInventory.class).slots();
int M=0;
ItemStack tStack = iShop.getItem().copy();
tStack.setQuantity(1);
boolean fSlot = false;
for (Slot slot : slotIter) {
if (slot.contains(iShop.getItem())) {
if(fSlot == false && slot.getStackSize() > 1){
iShop.setQnt(iShop.getQnt()+slot.getStackSize()-1);
slot.set(tStack);
fSlot = true;
}
else if(fSlot == true || slot.getStackSize() == 1){
iShop.setQnt(iShop.getQnt()+slot.getStackSize());
slot.clear();
}
M++;
if((M % 3) == 0){
Message.Send(player, MessageTypesSimple.FoundTaking);
}
}
}
}else{
iShop.setMod(false);
}
Message.Send(player, MessageTypesAdv.YouAreSelling, iShop);
}
}
Menu.getMenu().query(Menu.Slot(0,0)).set(Menu.Button(Text.of(iShop.getItem().getTranslation().get()),iShop.getItem().copy(),1,Arrays.asList(Text.of(TextColors.GREEN,"BuyPrice: ",TextColors.AQUA,iShop.getBuyPrice()),Text.of(TextColors.GREEN,"SellPrice: ",TextColors.AQUA,iShop.getSellPrice()),Text.of(TextColors.GREEN,"Stock Ammount: ",TextColors.AQUA,iShop.getQnt()),Text.of(TextColors.GREEN,"Click the Diamond/Emerald to Change Buy/Sell"),Text.of(TextColors.GREEN,"Redstone Adds"),Text.of(TextColors.RED,"Coal Subtracts")),true,"Selling",1));
}else if(Layout == MenuLayout.PlayerShop){
String Who = Menu.getWhoShop();
List<ShopItem> iShopList = OdenShops.Inst().getiShop().GetShopWho(Who);
Menu.getpShop().entrySet().forEach(Set -> {
if(Set.getKey().get(Keys.ITEM_LORE).orElse(Arrays.asList(Text.of("Broken"))).equals(Stack.get(Keys.ITEM_LORE).orElse(Arrays.asList(Text.of("I am?"))))){
if(iShopList.contains(Set.getValue())){
if(player.getName().equalsIgnoreCase(Who)){
OdenShops.Inst().getMenuStorage().get(player).LoadModify(Set.getValue());
}else{
OdenShops.Inst().getMenuStorage().get(player).LoadBuySell(Who, Set.getValue());
}
return;
}
}
});
}else if(Layout == MenuLayout.BuySell){
ShopItem iShop = Menu.getiShop();
if(iShop == null){
iClose(player);
}
double bPrice = (iShop.getBuyPrice()*IntKey);
double sPrice = (iShop.getSellPrice()*IntKey);
if(iShop.isMod()){
Message.Send(player, MessageTypesSimple.BeingChanged);
iClose(player);
}else{
if(StrKey.equalsIgnoreCase("Buy")){
if(iShop.getQnt() >= IntKey){
int Max = iShop.getItem().getMaxStackQuantity();
int sMax = 0;
for(Inventory Slot : player.getInventory().query(Hotbar.class,GridInventory.class).slots()){
if(!Slot.peek().isPresent()){
sMax+=Max;
}else if(Slot.peek().isPresent() && Slot.contains(iShop.getItem())){
sMax+=(Max-Slot.peek().get().getQuantity());
}
}
if(sMax>=IntKey){
ResultType R = MoneyHandler.payPlayer(player, iShop.getPlayer(), bPrice, false);
if(R == ResultType.SUCCESS){
ItemStack Item = iShop.getItem().copy();
player.playSound(SoundTypes.BLOCK_DISPENSER_DISPENSE, player.getLocation().getPosition(), 1);
player.sendMessage(Text.of(ShopUtil.OdenS,TextColors.GREEN,"You bought ",TextColors.GOLD,IntKey,TextColors.GREEN," of ",TextColors.AQUA,iShop.getItem().getTranslation(),TextColors.GREEN," for ",TextColors.GOLD,bPrice));
if(iShop.isNotify()){
Sponge.getServer().getPlayer(iShop.getPlayer()).ifPresent(Who -> { Who.sendMessage(Text.of(Text.of(ShopUtil.OdenS,TextColors.YELLOW,player.getName(),TextColors.GREEN," bought ",TextColors.GOLD,IntKey,TextColors.GREEN," of ",TextColors.AQUA,iShop.getItem().getTranslation(),TextColors.GREEN," for ",TextColors.GOLD,bPrice)));});
}
iShop.setQnt(iShop.getQnt()-IntKey);
if(IntKey > Max){
for(int i=0;i<IntKey/Max;i++){
Item.setQuantity(Max);
player.getInventory().query(Hotbar.class,GridInventory.class).offer(Item);
}
Item = iShop.getItem().copy();
Item.setQuantity(IntKey%Max);
player.getInventory().query(Hotbar.class,GridInventory.class).offer(Item);
}else{
Item = iShop.getItem().copy();
Item.setQuantity(IntKey);
player.getInventory().query(Hotbar.class,GridInventory.class).offer(Item);
}
}
}else{
Message.Send(player, MessageTypesSimple.NotEnoughRoom);
}
}else{
Message.Send(player, MessageTypesSimple.OutOfStock);
}
}
else if(StrKey.equalsIgnoreCase("Sell")){
ResultType R = MoneyHandler.payPlayer(player, iShop.getPlayer(), sPrice, true);
ItemStack I = iShop.getItem().copy();
I.setQuantity(1);
if(R == ResultType.SUCCESS){
boolean SellingSuccess = Selling(player,I,IntKey);
if(SellingSuccess == true){
player.playSound(SoundTypes.BLOCK_DISPENSER_DISPENSE, player.getLocation().getPosition(), 1);
player.sendMessage(Text.of(ShopUtil.OdenS,TextColors.GREEN,"You sold ",TextColors.GOLD,IntKey,TextColors.GREEN," of ",TextColors.AQUA,iShop.getItem().getTranslation(),TextColors.GREEN," for ",TextColors.GOLD,sPrice));
if(iShop.isNotify()){
Sponge.getServer().getPlayer(iShop.getPlayer()).ifPresent(Who -> { Who.sendMessage(Text.of(Text.of(ShopUtil.OdenS,TextColors.YELLOW,player.getName(),TextColors.GREEN," sold you ",TextColors.GOLD,IntKey,TextColors.GREEN," of ",TextColors.AQUA,iShop.getItem().getTranslation(),TextColors.GREEN," for ",TextColors.GOLD,sPrice)));});
}
iShop.setQnt(iShop.getQnt()+IntKey);
}else{
MoneyHandler.payPlayer(player, iShop.getPlayer(), sPrice, false);
Message.Send(player, MessageTypesSimple.NoMoreToSell);
}
}
}else if(StrKey.equalsIgnoreCase("Back")){
OdenShops.Inst().getMenuStorage().get(player).LoadPlayer(iShop.getPlayer(), OdenShops.Inst().getiShop().GetShopWho(iShop.getPlayer()));
}else if(StrKey.equalsIgnoreCase("Reload")){
OdenShops.Inst().getMenuStorage().get(player).LoadBuySell(iShop.getPlayer(), iShop);
}
ItemStack Button = iShop.getItem().copy();
Button.offer(Keys.DISPLAY_NAME, Text.of(iShop.getItem().getTranslation()));
Button.offer(Keys.ITEM_LORE, Arrays.asList(
Text.of(TextColors.GREEN,"Please make a Selection"),
Text.of(TextColors.AQUA, "~~~~~~~~~~~~~~~~~~~~~~~"),
Text.of(TextColors.GREEN,"BuyPrice: ",TextColors.AQUA,iShop.getBuyPrice()),
Text.of(TextColors.GREEN,"SellPrice: ",TextColors.AQUA,iShop.getSellPrice()),
Text.of(TextColors.GREEN,"Stock Ammount: ",TextColors.AQUA,iShop.getQnt())));
Button.setQuantity(1);
Menu.getMenu().query(Menu.Slot(0,0)).set(Menu.Button(Text.of(iShop.getItem().getTranslation().get()),iShop.getItem().copy(),1,Arrays.asList(Text.of(TextColors.GREEN,"Please make a Selection"), Text.of(TextColors.AQUA, "~~~~~~~~~~~~~~~~~~~~~~~"), Text.of(TextColors.GREEN,"Buy Price (1): ",TextColors.GOLD,iShop.getBuyPrice()), Text.of(TextColors.GREEN,"Sell Price (1): ",TextColors.GOLD,iShop.getSellPrice()), Text.of(TextColors.GREEN,"Current Stock: ",TextColors.GOLD,iShop.getQnt()), Text.of(TextColors.BLACK,"[OdenShops]")),true,"Listing",0));
}
}else if(Layout == MenuLayout.BuySlots){
if(StrKey.equalsIgnoreCase("BuyShopSlot")){
int Price = ShopUtil.SlotPrice(OdenShops.Inst().getMenuStorage().get(player).getShopSlotSize());
if(Price != 0){
if(OdenShops.Inst().getMenuStorage().get(player).getShopSlotSize() < 44){
ResultType R = MoneyHandler.payServer(player, Price);
if(R == ResultType.SUCCESS){
OdenShops.Inst().getMenuStorage().get(player).setShopSlotSize(OdenShops.Inst().getMenuStorage().get(player).getShopSlotSize()+1);
OdenShops.Inst().getDataBase().UpdateSlotSize(player);
player.playSound(SoundTypes.BLOCK_DISPENSER_DISPENSE, player.getLocation().getPosition(), 1);
player.sendMessage(Text.of(ShopUtil.OdenS,TextColors.YELLOW,"You bought a shop slot for ",TextColors.GOLD,Price));
Sponge.getCommandManager().process(player, "odenshop add");
}
}else{
Message.Send(player, MessageTypesSimple.MaxShopSlot);
iClose(player);
}
}
else{
Menu.getMenu().query(Menu.Slot(1,0)).clear();
Menu.getMenu().query(Menu.Slot(2,0)).set(Menu.Button(Text.of("Maxed Shop Slots"),ItemTypes.ENDER_CHEST,1,Arrays.asList(Text.of(TextColors.GREEN,"You can not buy anymore slots!")),true,"BuyShopSlotMax",1));
}
}else if(StrKey.equalsIgnoreCase("BuyShopSlotMax")){
Message.Send(player, MessageTypesSimple.MaxShopSlot);
iClose(player);
}else if(StrKey.equalsIgnoreCase("BuyShopSlotClose")){
iClose(player);
}
}
}
}
}
@Listener
public void onChange(ChangeInventoryEvent.Held e, @First Player P){
Sweep(P);
}
@Listener(order = Order.FIRST)
public void onNum(ClickInventoryEvent.NumberPress e, @First Player P) {
if(isInInventoryMenu(P)) {
Message.Send(P, MessageTypesSimple.NumPress);
iClose(P);
return;
}
}
@Listener(order = Order.LATE)
@IsCancelled(Tristate.FALSE)
public void onClick(ClickInventoryEvent ev, @First Player player) {
if(isInInventoryMenu(player)) {
ev.setCancelled(true);
}
}
@Listener(order = Order.LATE)
@IsCancelled(Tristate.FALSE)
public void onDrop(DropItemEvent ev, @First Player player) {
if(isInInventoryMenu(player)) {
ev.setCancelled(true);
}
}
@Listener(order = Order.LATE)
public void Close(InteractInventoryEvent.Close E, @First Player P){
iClose(P);
}
@Listener(order = Order.LATE)
public void Open(InteractInventoryEvent.Open E, @First Player P){
iClose(P);
}
private boolean Selling(Player P, ItemStack I, int A) {
ItemStack tmp = I.copy();
int Count = 0;
int Grab = 0;
if(I.getMaxStackQuantity() == 1){
for(Inventory Slot:P.getInventory().query(Hotbar.class,GridInventory.class).slots()){
if(!Slot.contains(I) || !Slot.peek().isPresent()){
Count++;
}
}
if(Count >= A){
for(Inventory Slot:P.getInventory().query(Hotbar.class,GridInventory.class).slots()){
if(Slot.contains(I) && Grab < A){
Slot.clear();
Grab++;
}
if(Grab == A){
return true;
}
}
}else{
return false;
}
}else{
for(Inventory Slot:P.getInventory().query(Hotbar.class,GridInventory.class).slots()){
if(Slot.contains(I)){
Count+=Slot.peek().get().getQuantity();
}
}
if(Count >= A){
for(Inventory Slot:P.getInventory().query(Hotbar.class,GridInventory.class).slots()){
if(Slot.contains(I)){
if(Slot.peek().get().getQuantity() == A){
Slot.clear();
return true;
}else if(Slot.peek().get().getQuantity() >= A){
tmp.setQuantity(Slot.peek().get().getQuantity() - A);
Slot.set(tmp);
return true;
}else if(Slot.peek().get().getQuantity() <= A){
return RemoveTiny(P,I,A);
}
}
}
}
}
return false;
}
boolean RemoveTiny(Player P, ItemStack I, int A){
ItemStack tmp = I.copy();
int Count =0;
for(Inventory Slot:P.getInventory().query(Hotbar.class,GridInventory.class).slots()){
if(Slot.contains(I)){
Count += Slot.peek().get().getQuantity();
if(Count < A){
Slot.clear();
}
else if(Count == A){
Slot.clear();
return true;
}
else if(Count > A){
tmp.setQuantity(Count - A);
Slot.set(tmp);
return true;
}
}
}
return false;
}
public void iClose(Player P){
Sweep(P);
if(!isInInventoryMenu(P)){
P.offer(((DataHolder) P).getOrCreate(sKeyData.class).get().set(sKeys.BoolKey, false));
} else {
if(P.getOpenInventory().isPresent()){
P.closeInventory(Cause.builder().owner(OdenShops.Inst()).build());
}
}
return;
}
private boolean isInInventoryMenu(Player player){
if(player.getOpenInventory().isPresent() && player.getOpenInventory().get().getName().get().equals(OdenShops.Inst().getMenuStorage().shopName) && player.get(sKeys.BoolKey).orElse(false).booleanValue() == true){
return true;
}
return false;
}
void Sweep(Player P){
P.getInventory().query(Hotbar.class,GridInventory.class).slots().forEach(Slot -> {
if(Slot.peek().isPresent()){
if(Slot.peek().get().get(Keys.ITEM_LORE).isPresent()){
List<Text> LoreList = Slot.peek().get().get(Keys.ITEM_LORE).orElse(Arrays.asList(Text.of("Not here!")));
if(LoreList.get(LoreList.size() - 1).toPlain().equals("[OdenShops]")){
Slot.clear();
}
}
if(Slot.peek().orElse(ItemStack.builder().itemType(ItemTypes.BEDROCK).build()).getValue(sKeys.StringKey).isPresent()){
Slot.clear();
}
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment