Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AnvilStoneInventory {
- private Esinia plugin;
- public AnvilStoneInventory(Esinia plugin){
- this.plugin = plugin;
- }
- Map<UUID, Task> tasks = new HashMap<>();
- Map<UUID, Scoreboard> boards = new HashMap<>();
- //Listener for following event
- @Listener
- //Inventory Click event. Filtering based on the root player. Setting inventory variable as event variable.
- public void onInventoryClick(ClickInventoryEvent event, @Root Player player, @Getter("getTargetInventory") Inventory inventory) {
- ConfigurationNode nodeStone = plugin.root.getNode("Stoneworking", player.getUniqueId().toString());
- Integer level = nodeStone.getNode("Level").getInt();
- UUID id = player.getUniqueId();
- //Checks if inventory name contains "Stone Working then proceeds"
- if (inventory.getName().get().contains("Stone Working")){
- //Sponge.getServer().getBroadcastChannel().send(Text.of("1"));
- //Cancels the event
- event.setCancelled(true);
- //Gets result of the cursor clicking on an item. Gets display name of the items. Checks if that display is "Stone Shovel" then proceeds if true
- if (event.getCursorTransaction().getFinal().get(Keys.DISPLAY_NAME).get().equals(Text.of("Stone Shovel"))){
- //Optional itemtype variable called shovelHead is created with the ItemType based off of the itemID if the ItemType exists
- Optional<ItemType> shovelHead_ = Sponge.getGame().getRegistry().getType(ItemType.class, "forgecraft:wootzshovelhead");
- //ItemType variable shovelHead equals shovelHead_
- ItemType shovelHead = shovelHead_.get();
- //boolean set to false
- boolean hasShovelHead = false;
- //boolean set to false
- boolean hasToolHandle = false;
- //Inventory variable titled slot is loops for every item slot in the players inventory that matches the itemstack shovelHead
- for (Inventory slot : player.getInventory().query(shovelHead).slots()){
- //Itemstack Variable stack is set to the itemstack inside the Inventory variable slot
- ItemStack stack = slot.peek().get();
- //Checks if the itemstack has display name "Stone Shovelhead" and if the item doesnt have an itemname it uses the text "EMPTY" instead to check
- if (stack.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("Stone Shovelhead")){
- //boolean set to true
- hasShovelHead = true;
- //broadcasting to the server
- Sponge.getServer().getBroadcastChannel().send(Text.of("hasShovelHead: " + hasShovelHead));
- //breaks out of the loop
- break;
- }
- }
- //Inventory variable titled slot is looped for every item slot in the player inventory that matches ItemType.Stick
- for (Inventory slot : player.getInventory().query(ItemTypes.STICK).slots()){
- //Itemstack variable is set to the itemstack inside the Inventory variable slot
- ItemStack stack = slot.peek().get();
- //Checks if itemstack has the displayname "Tool Handle"
- if (stack.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("Tool Handle")){
- //boolean set to true
- hasToolHandle = true;
- //broadcasting to server
- Sponge.getServer().getBroadcastChannel().send(Text.of("hasToolHandle: " + hasToolHandle));
- //breaks loop
- break;
- }
- }
- //if both booleans are true it proceeds
- if (hasShovelHead && hasToolHandle){
- //broadcast to server
- Sponge.getServer().getBroadcastChannel().send(Text.of("hasShovelHead and hasToolHandle"));
- //integer variable titled quantity equals 0
- int quantity = 0;
- //Inventory variable titled slot is looped for every item slot in the player inventory that matches itemtype variable shovelHead
- for (Inventory slot : player.getInventory().query(shovelHead).slots()) {
- //Itemstack variable set to itemstack inside inventory variable slot
- ItemStack stack = slot.peek().get();
- //Display name checking again
- if (stack.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("Stone Shovelhead")){
- //adds to quantity the size of stack (This happens each loop until looping is complete)
- quantity += stack.getQuantity();
- }
- }
- Sponge.getServer().getBroadcastChannel().send(Text.of("quantity: " + quantity));
- // integer variable titled quantity2 equals 0
- int quantity2 = 0;
- //Inventory variable titled slot is looped for every item slot in the player inventory that matches itemtype.Stick
- for (Inventory slot : player.getInventory().query(ItemTypes.STICK).slots()) {
- //Itemstack variable stack is set to itemstack inside inventory variable slot
- ItemStack stack = slot.peek().get();
- //checking for display name again
- if (stack.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("Tool Handle")){
- //adds to quantity2 the size of stack (This happens each loop until looping is complete)
- quantity2 += stack.getQuantity();
- }
- }
- //broadcasting to server
- Sponge.getServer().getBroadcastChannel().send(Text.of("quantity2: " + quantity2));
- //if quantity is less than 1 it proceeds
- if (quantity < 1) {
- //sends player a message
- player.sendMessage(Text.of(""));
- //task complete
- return;
- }
- //if quantity2 is less than 1 it proceeds
- if (quantity2 < 1) {
- //sends player a message
- player.sendMessage(Text.of(""));
- //task complete
- return;
- }
- //if both quantity and quantity2 are larger than 1 it proceeds
- if (quantity >= 1 && quantity2 >= 1) {
- Objective obj = Objective.builder()
- .criterion(Criteria.DUMMY)
- .name("timer")
- .displayName(Text.of(TextColors.DARK_BLUE, "[ ]"))
- .build();
- Scoreboard board = Scoreboard.builder()
- .objectives(ImmutableList.of(obj))
- .build();
- obj.getOrCreateScore(Text.of(player.getName())).setScore(0);
- player.setScoreboard(board);
- board.updateDisplaySlot(obj, DisplaySlots.SIDEBAR);
- boards.put(player.getUniqueId(), board);
- AtomicInteger integer = new AtomicInteger(10);
- tasks.put(id, Task.builder()
- .delay(1, TimeUnit.SECONDS)
- .interval(1, TimeUnit.SECONDS)
- .execute(() -> {
- player.setScoreboard(board);
- /// Duration dur = Duration.of(integer.get(), ChronoUnit.SECONDS);
- char[] arr = new char[10];
- Arrays.fill(arr, 0, 11 - integer.decrementAndGet(), '|');
- obj.setDisplayName(Text.of(TextColors.DARK_BLUE, "[", String.valueOf(arr), "]"));
- if (integer.get() == 0) {
- obj.setDisplayName(Text.of(TextColors.DARK_BLUE, "[||||||||||]"));
- for (Inventory slot : player.getInventory().query(shovelHead).slots()) {
- ItemStack stack = slot.peek().get();
- if (stack.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("Stone Shovelhead")){
- if (stack.getQuantity() >= 1) {
- slot.poll(1);
- break;
- }
- }
- }
- for (Inventory slot : player.getInventory().query(ItemTypes.STICK).slots()) {
- ItemStack stack = slot.peek().get();
- if (stack.get(Keys.DISPLAY_NAME).orElse(Text.EMPTY).toPlain().equals("Tool Handle")){
- if (stack.getQuantity() >= 1) {
- slot.poll(1);
- break;
- }
- }
- }
- Optional<ItemType> Stoneshovel_ = Sponge.getGame().getRegistry().getType(ItemType.class, "forgecraft:wootzshovel");
- ItemType StoneShovelType = Stoneshovel_.get();
- ItemStack stoneshovel = ItemStack.of(StoneShovelType, 1);
- stoneshovel.offer(Keys.DISPLAY_NAME, Text.of("Stone Shovel"));
- //List<Text> itemShovelLore = new ArrayList<Text>();
- //itemShovelLore.add(Text.of(TextColors.GOLD, "Crafting Requirements:"));
- //itemShovelLore.add(Text.of(TextColors.GOLD, "1 Flat Stone"));
- //itemShovelLore.add(Text.of(TextColors.GOLD, "1 Tool Handle"));
- //stoneshovel.offer(Keys.ITEM_LORE, itemShovelLore);
- //giving player the itemstack stoneshovel
- player.getInventory().offer(stoneshovel);
- //game.getServer().getPlayer(id).ifPresent(pl -> {
- //pl.setLocationSafely(pl.getWorld().getSpawnLocation());
- //});
- tasks.remove(id).cancel();
- boards.remove(id);
- }
- })
- .submit(this));
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment