Make banking functional on 1.8.

This commit is contained in:
Brianna 2020-06-15 14:43:08 -05:00
parent 77d5997d44
commit 7213d16884
3 changed files with 39 additions and 189 deletions

View File

@ -41,7 +41,6 @@ import com.songoda.skyblock.stackable.StackableManager;
import com.songoda.skyblock.structure.StructureManager;
import com.songoda.skyblock.upgrade.UpgradeManager;
import com.songoda.skyblock.usercache.UserCacheManager;
import com.songoda.skyblock.utils.SignMenuFactory;
import com.songoda.skyblock.visit.VisitManager;
import com.songoda.skyblock.visit.VisitTask;
import com.songoda.skyblock.world.WorldManager;

View File

@ -1,10 +1,9 @@
package com.songoda.skyblock.menus;
import be.maximvdw.placeholderapi.internal.utils.ListUtils;
import com.google.common.collect.Lists;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.compatibility.CompatibleSound;
import com.songoda.core.hooks.EconomyManager;
import com.songoda.core.input.ChatPrompt;
import com.songoda.skyblock.SkyBlock;
import com.songoda.skyblock.bank.BankManager;
import com.songoda.skyblock.bank.Transaction;
@ -15,7 +14,6 @@ import com.songoda.skyblock.island.IslandManager;
import com.songoda.skyblock.message.MessageManager;
import com.songoda.skyblock.sound.SoundManager;
import com.songoda.skyblock.utils.NumberUtil;
import com.songoda.skyblock.utils.SignMenuFactory;
import com.songoda.skyblock.utils.item.MenuClickRegistry;
import com.songoda.skyblock.utils.item.nInventoryUtil;
import org.bukkit.Bukkit;
@ -24,7 +22,8 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
import java.util.*;
import java.util.Calendar;
import java.util.Objects;
public class Bank {
@ -34,7 +33,9 @@ public class Bank {
private IslandManager islandManager;
public static Bank getInstance() {return instance == null ? instance = new Bank() : instance;}
public static Bank getInstance() {
return instance == null ? instance = new Bank() : instance;
}
public Bank() {
SkyBlock skyblock = SkyBlock.getInstance();
@ -65,31 +66,27 @@ public class Bank {
//Deposit money
Island island = islandManager.getIslandByPlayer(Bukkit.getOfflinePlayer(player.getUniqueId()));
if (island != null) {
Island finalIsland = island;
SelectInputMethod.getInstance().open(player,"Deposit", (action) -> {
SelectInputMethod.getInstance().open(player, "Deposit", (action) -> {
if (action == InputMethodSelectlistener.InputMethod.CANCELED) {
Bukkit.getScheduler().runTask(skyblock,() -> {this.open(player);});
return;
Bukkit.getScheduler().runTask(skyblock, () ->
this.open(player));
} else if (action == InputMethodSelectlistener.InputMethod.ALL) {
deposit(player,finalIsland,EconomyManager.getBalance(Bukkit.getOfflinePlayer(player.getUniqueId())));
deposit(player, island, EconomyManager.getBalance(Bukkit.getOfflinePlayer(player.getUniqueId())));
} else {
Bukkit.getScheduler().runTaskLater(SkyBlock.getInstance(), () ->
SignMenuFactory.getInstance().newMenu().reopenIfFail()
.response((player1, lines) -> {
if (lines[0] == "") {
return true;
}
ChatPrompt.showPrompt(skyblock, player, (event) -> {
if (event.getMessage().equals(""))
return;
double amount;
try {
amount = Double.parseDouble(lines[0]);
amount = Double.parseDouble(event.getMessage());
} catch (NumberFormatException e1) {
messageManager.sendMessage(player, configLoad.getString("Command.Island.Bank.Short4.Message"));
soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1.0F, 1.0F);
return false;
return;
}
deposit(player,finalIsland,amount);
return true;
}).open(player),10);
deposit(player, island, amount);
});
}
});
}
@ -103,30 +100,27 @@ public class Bank {
island = islandManager.getIslandByPlayer(Bukkit.getOfflinePlayer(player.getUniqueId()));
if (island != null) {
Island finalIsland = island;
SelectInputMethod.getInstance().open(player,"Withdraw", (action) -> {
SelectInputMethod.getInstance().open(player, "Withdraw", (action) -> {
if (action == InputMethodSelectlistener.InputMethod.CANCELED) {
Bukkit.getScheduler().runTask(skyblock,() -> {this.open(player);});
Bukkit.getScheduler().runTask(skyblock, () -> {
this.open(player);
});
return;
} else if (action == InputMethodSelectlistener.InputMethod.ALL) {
withdraw(player,finalIsland,finalIsland.getBankBalance());
withdraw(player, finalIsland, finalIsland.getBankBalance());
} else {
Bukkit.getScheduler().runTaskLater(SkyBlock.getInstance(), () ->
SignMenuFactory.getInstance().newMenu().reopenIfFail()
.response((player1, lines) -> {
if (lines[0] == "") {
return true;
}
double amount;
ChatPrompt.showPrompt(skyblock, player, (event) -> {
if (event.getMessage().equals(""))
return;
double amount = 0;
try {
amount = Double.parseDouble(lines[0]);
amount = Double.parseDouble(event.getMessage());
} catch (NumberFormatException e1) {
messageManager.sendMessage(player, configLoad.getString("Command.Island.Bank.Short4.Message"));
soundManager.playSound(player, CompatibleSound.BLOCK_ANVIL_LAND.getSound(), 1.0F, 1.0F);
return false;
}
withdraw(player,finalIsland,amount);
return true;
}).open(player),10);
withdraw(player, finalIsland, amount);
});
}
});
}
@ -150,7 +144,6 @@ public class Bank {
island = islandManager.getIslandByPlayer(Bukkit.getOfflinePlayer(player.getUniqueId()));
SkyBlock skyblock = SkyBlock.getInstance();
FileManager.Config config = skyblock.getFileManager().getConfig(new File(skyblock.getDataFolder(), "language.yml"));
FileConfiguration configLoad = config.getFileConfiguration();
@ -158,16 +151,16 @@ public class Bank {
if (island == null) {
skyblock.getSoundManager().playSound(player, CompatibleSound.BLOCK_GLASS_BREAK.getSound(), 1.0F, 1.0F);
skyblock.getMessageManager().sendMessage(player,configLoad.getString("Command.Bank.Unknown"));
skyblock.getMessageManager().sendMessage(player, configLoad.getString("Command.Bank.Unknown"));
return;
}
// Glass panes barriers
nInv.addItem(nInv.createItem(CompatibleMaterial.BLACK_STAINED_GLASS_PANE.getItem(), configLoad.getString("Menu.Bank.Item.Barrier.Displayname"), null, null, null, null), 0, 2, 5, 8,
1, 2, 3, 5,6,7,9,11,12,14,15,17);
1, 2, 3, 5, 6, 7, 9, 11, 12, 14, 15, 17);
nInv.addItem(nInv.createItem(CompatibleMaterial.OAK_FENCE_GATE.getItem(), configLoad.getString("Menu.Bank.Item.Exit.Displayname"),
configLoad.getStringList("Menu.Bank.Item.Exit.Lore"), null, null, null), 0,8);
configLoad.getStringList("Menu.Bank.Item.Exit.Lore"), null, null, null), 0, 8);
nInv.addItem(nInv.createItem(CompatibleMaterial.BOOK.getItem(), configLoad.getString("Menu.Bank.Item.Log.Displayname"),
bankManager.getTransactions(player), null, null, null), 4);

View File

@ -1,142 +0,0 @@
package com.songoda.skyblock.utils;
import com.google.common.collect.Lists;
import com.songoda.core.compatibility.CompatibleMaterial;
import com.songoda.core.utils.NMSUtils;
import com.songoda.skyblock.SkyBlock;
import io.netty.channel.Channel;
import io.netty.channel.ChannelDuplexHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import java.util.function.BiPredicate;
public final class SignMenuFactory {
private final Plugin plugin;
private static SignMenuFactory instance;
private Class signPacket = NMSUtils.getNMSClass("PacketPlayInUpdateSign");
private SignMenuFactory(Plugin plugin) {
this.plugin = plugin;
}
public static SignMenuFactory getInstance() {
return instance == null ? instance = new SignMenuFactory(SkyBlock.getInstance()) : instance;
}
public Menu newMenu() {
return new Menu(Lists.newArrayList("","","",""));
}
private void injectPlayer(Player player,Menu m) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, NoSuchFieldException {
ChannelDuplexHandler channelDuplexHandler = new ChannelDuplexHandler(){
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg.getClass().equals(signPacket)) {
String[] lines;
Method getLines = msg.getClass().getMethod("c");
if (getLines == null) {
return;
}
lines = (String[])getLines.invoke(msg);
player.sendBlockChange(m.position, Material.AIR.createBlockData());
boolean success = m.response.test(player, lines);
if (!success && m.opensOnFail()) {
Bukkit.getScheduler().runTaskLater(plugin, () -> m.open(player), 2L);
}
removePlayer(player);
}
super.channelRead(ctx, msg);
}
};
Object handle = NMSUtils.getCraftClass("entity.CraftPlayer").getMethod("getHandle").invoke(player);
Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
Object networkManager = playerConnection.getClass().getField("networkManager").get(playerConnection);
Channel channel = (Channel) networkManager.getClass().getField("channel").get(networkManager);
ChannelPipeline pipeline = channel.pipeline();
pipeline.addBefore("packet_handler",player.getName(),channelDuplexHandler);
}
private void removePlayer(Player player) throws NoSuchFieldException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Object handle = NMSUtils.getCraftClass("entity.CraftPlayer").getMethod("getHandle").invoke(player);
Object playerConnection = handle.getClass().getField("playerConnection").get(handle);
Object networkManager = playerConnection.getClass().getField("networkManager").get(playerConnection);
Channel channel = (Channel) networkManager.getClass().getField("channel").get(networkManager);
ChannelPipeline pipeline = channel.pipeline();
channel.eventLoop().submit(() -> {
pipeline.remove(player.getName());
});
}
public class Menu {
private final List<String> text;
private BiPredicate<Player, String[]> response;
private boolean reopenIfFail;
private Location position;
Menu(List<String> text) {
this.text = text;
}
boolean opensOnFail() {
return this.reopenIfFail;
}
public Menu reopenIfFail() {
this.reopenIfFail = true;
return this;
}
public Menu response(BiPredicate<Player, String[]> response) {
this.response = response;
return this;
}
public void open(Player player) {
Objects.requireNonNull(player, "player");
this.position = player.getLocation().getBlock().getLocation();
player.sendBlockChange(this.position, CompatibleMaterial.OAK_SIGN.getBlockMaterial().createBlockData());
Class openSign = NMSUtils.getNMSClass("PacketPlayOutOpenSignEditor");
Class blockPosition = NMSUtils.getNMSClass("BlockPosition");
Object openSignPacket = null;
try {
Object blockPos = blockPosition.getConstructor(Integer.TYPE, Integer.TYPE, Integer.TYPE).newInstance(position.getBlockX(), position.getBlockY(), position.getBlockZ());
openSignPacket = openSign.getConstructor(blockPosition).newInstance(blockPos);
}catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) {
e.printStackTrace();
}
NMSUtils.sendPacket(player,openSignPacket);
try {
injectPlayer(player,this);
} catch (NoSuchMethodException | IllegalAccessException | NoSuchFieldException | InvocationTargetException e) {
e.printStackTrace();
}
}
public List<String> getText() {
return text;
}
}
}