iTzAyhamHD

Untitled

Jun 30th, 2021 (edited)
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.02 KB | None | 0 0
  1. /**
  2.  *   This file is part of Skript.
  3.  *
  4.  *  Skript is free software: you can redistribute it and/or modify
  5.  *  it under the terms of the GNU General Public License as published by
  6.  *  the Free Software Foundation, either version 3 of the License, or
  7.  *  (at your option) any later version.
  8.  *
  9.  *  Skript is distributed in the hope that it will be useful,
  10.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.  *  GNU General Public License for more details.
  13.  *
  14.  *  You should have received a copy of the GNU General Public License
  15.  *  along with Skript.  If not, see <http://www.gnu.org/licenses/>.
  16.  *
  17.  * Copyright Peter Güttinger, SkriptLang team and contributors
  18.  */
  19. package ch.njol.skript.expressions;
  20.  
  21. import java.util.Arrays;
  22. import java.util.Iterator;
  23. import java.util.List;
  24.  
  25. import ch.njol.skript.aliases.ItemType;
  26. import ch.njol.util.coll.CollectionUtils;
  27. import org.bukkit.Bukkit;
  28. import org.bukkit.Material;
  29. import org.bukkit.block.Block;
  30. import org.bukkit.event.Event;
  31. import org.bukkit.event.block.BlockExplodeEvent;
  32. import org.bukkit.event.entity.EntityExplodeEvent;
  33. import org.eclipse.jdt.annotation.Nullable;
  34. import ch.njol.skript.classes.Changer.ChangeMode;
  35.  
  36. import ch.njol.skript.Skript;
  37. import ch.njol.skript.doc.Description;
  38. import ch.njol.skript.doc.Events;
  39. import ch.njol.skript.doc.Examples;
  40. import ch.njol.skript.doc.Name;
  41. import ch.njol.skript.doc.Since;
  42. import ch.njol.skript.lang.Expression;
  43. import ch.njol.skript.lang.ExpressionType;
  44. import ch.njol.skript.lang.SkriptParser.ParseResult;
  45. import ch.njol.skript.lang.util.SimpleExpression;
  46. import ch.njol.util.Kleenean;
  47.  
  48. @Name("Exploded Blocks")
  49. @Description("Get all the blocks that were destroyed in an entity/block explode event (Can be set/removed from/deleted/added to)")
  50. @Examples({"on block explode:",
  51.     "\tadd exploded blocks to {exploded-blocks::*}",
  52.     "\t",
  53.     "\tloop exploded blocks:",
  54.     "\t\tif loop-block is sand:",
  55.     "\t\t\tset loop-block to stone",
  56.     "\t",
  57.     "\tset exploded blocks to blocks in radius 3 around event-location  # this will clear exploded blocks and set it to the specified blocks",
  58.     "\tremove all chests from exploded blocks",
  59.     "\tremove stone from exploded blocks # will remove only one block that equals to stone block type",
  60.     "\tdelete exploded blocks",
  61.     "\tadd blocks in radius 3 around event-location to exploded blocks",
  62.     "\t",
  63.     "on entity explode:",
  64.     "\tremove all stone from exploded blocks",
  65.     "\t",
  66.     "on explode:",
  67.     "\tremove all stone from exploded blocks"})
  68. @Events("explode")
  69. @Since("2.5")
  70. public class ExprExplodedBlocks extends SimpleExpression<Block> {
  71.  
  72.     static {
  73.         Skript.registerExpression(ExprExplodedBlocks.class, Block.class, ExpressionType.SIMPLE, "[the] exploded blocks");
  74.     }
  75.    
  76.     boolean isEntity = false;
  77.    
  78.     @Override
  79.     public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
  80.         Bukkit.broadcastMessage("Current Events: " + Arrays.toString(getParser().getCurrentEvents()));
  81.         if (getParser().isCurrentEvent(EntityExplodeEvent.class)) {
  82.             isEntity = true;
  83.         } else if (!getParser().isCurrentEvent(BlockExplodeEvent.class)) {
  84.             Skript.error("Exploded blocks can only be retrieved from an entity/block explode event.");
  85.             return false;
  86.         }
  87.         return true;
  88.     }
  89.    
  90.     @Nullable
  91.     @Override
  92.     protected Block[] get(Event e) {
  93.         List<Block> blockList = isEntity ? ((EntityExplodeEvent) e).blockList() : ((BlockExplodeEvent) e).blockList();
  94.         return blockList.toArray(new Block[blockList.size()]);
  95.     }
  96.  
  97.     @Nullable
  98.     @Override
  99.     public Iterator<? extends Block> iterator(Event e) {
  100.         return isEntity ? ((EntityExplodeEvent) e).blockList().iterator() : ((BlockExplodeEvent) e).blockList().iterator();
  101.     }
  102.  
  103.     @Override
  104.     @Nullable
  105.     public Class<?>[] acceptChange(final ChangeMode mode) {
  106.         switch (mode) {
  107.             case ADD:
  108.             case SET:
  109.             case DELETE:
  110.                 return CollectionUtils.array(Block[].class);
  111.             case REMOVE:
  112.             case REMOVE_ALL:
  113.                 return CollectionUtils.array(Block[].class, ItemType[].class);
  114.             case RESET:
  115.             default:
  116.                 return null;
  117.         }
  118.     }
  119.    
  120.     @Override
  121.     public void change(Event e, final @Nullable Object[] delta, final ChangeMode mode) {
  122.         List<Block> blocks = isEntity ? ((EntityExplodeEvent) e).blockList() : ((BlockExplodeEvent) e).blockList();
  123.        
  124.         switch (mode) {
  125.             case DELETE:
  126.             case SET:
  127.             case ADD:
  128.                 assert delta != null;
  129.                 if (mode != ChangeMode.ADD)
  130.                     blocks.clear();
  131.  
  132.                 for (Object o : delta) {
  133.                     if (o instanceof ItemType)
  134.                         continue;
  135.  
  136.                     if (((Block) o).getType() != Material.AIR) // Performance
  137.                         blocks.add((Block) o);
  138.                 }
  139.                 break;
  140.  
  141.             case REMOVE:
  142.             case REMOVE_ALL: // ItemType here will allow `remove [all] %itemtypes% from exploded blocks` to remove all/specific amount of specific block type that matches an itemtype, a shortcut for looping
  143.                 assert delta != null;
  144.                 int amountReached = 0;
  145.  
  146.                 for (Object o : delta) {
  147.                     if (o instanceof Block) {
  148.                         blocks.remove((Block) o); // no need to check whether they are equals, since blocks are only passed by reference
  149.  
  150.                     } else if (o instanceof ItemType) {
  151.                         ItemType item = ((ItemType) o);
  152.                         int amount = item.getInternalAmount(); // -1 when using 'all' or 'any' of alias
  153.  
  154.                         if (amount == -1 || mode == ChangeMode.REMOVE_ALL) { // all %itemtype% OR REMOVE_ALL
  155.                             blocks.removeIf(item::isOfType);
  156.                             break;
  157.                         }
  158.  
  159.                         else if (amount > 0)
  160.                             for (Block b : blocks) {
  161.                                 if (amountReached >= amount)
  162.                                     break;
  163.                                 else if (item.isOfType(b)) {
  164.                                     blocks.remove(b);
  165.                                     amountReached++; // only ++ if a matched item was removed
  166.                                 }
  167.                             }
  168.                     }
  169.                 }
  170.                 break;
  171.         }
  172.     }
  173.  
  174.  
  175.     @Override
  176.     public boolean isSingle() {
  177.         return false;
  178.     }
  179.    
  180.     @Override
  181.     public Class<? extends Block> getReturnType() {
  182.         return Block.class;
  183.     }
  184.    
  185.     @Override
  186.     public String toString(@Nullable Event e, boolean d) {
  187.         return isEntity ? "entity" : "block" + " event's exploded blocks";
  188.     }
  189.    
  190. }
  191.  
Add Comment
Please, Sign In to add comment