Skip to content

Commit

Permalink
block break event drops handling upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Jan 19, 2021
1 parent 0deecb6 commit ec4c4fb
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ public static void registerMainEvents() {
ScriptEvent.registerScriptEvent(new ItemSpawnsScriptEvent());

// Player events
ScriptEvent.registerScriptEvent(new AreaEnterExitScriptEvent());
ScriptEvent.registerScriptEvent(new BiomeEnterExitScriptEvent());
ScriptEvent.registerScriptEvent(new BlockDropsItemScriptEvent());
ScriptEvent.registerScriptEvent(new ChatScriptEvent());
ScriptEvent.registerScriptEvent(new AreaEnterExitScriptEvent());
ScriptEvent.registerScriptEvent(new HotbarScrollScriptEvent());
ScriptEvent.registerScriptEvent(new ExperienceBottleBreaksScriptEvent());
ScriptEvent.registerScriptEvent(new PlayerAnimatesScriptEvent());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.denizenscript.denizen.events.player;

import com.denizenscript.denizen.events.BukkitScriptEvent;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import org.bukkit.entity.Item;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockDropItemEvent;

import java.util.ArrayList;
import java.util.List;

public class BlockDropsItemScriptEvent extends BukkitScriptEvent implements Listener {

// <--[event]
// @Events
// block drops item from breaking
// <block> drops <item> from breaking
//
// @Regex ^on [^\s]+ drops [^\s]+ from breaking$
//
// @Group Player
//
// @Location true
//
// @Cancellable true
//
// @Triggers when a player breaks a block.
//
// @Context
// <context.location> returns the LocationTag the block was broken at.
// <context.material> returns the MaterialTag of the block that was broken.
// <context.drop_entities> returns a ListTag of EntityTags of type DROPPED_ITEM.
//
// @Player Always.
//
// -->

public BlockDropsItemScriptEvent() {
instance = this;
}

public static BlockDropsItemScriptEvent instance;
public LocationTag location;
public MaterialTag material;
public BlockDropItemEvent event;

@Override
public boolean couldMatch(ScriptPath path) {
if (!path.eventArgLowerAt(1).equals("drops") || !path.eventArgLowerAt(3).equals("from") || !path.eventArgLowerAt(4).equals("breaking")) {
return false;
}
if (!couldMatchBlock(path.eventArgLowerAt(0))) {
return false;
}
if (!couldMatchItem(path.eventArgLowerAt(2))) {
return false;
}
return true;
}

@Override
public boolean matches(ScriptPath path) {
if (!tryMaterial(material, path.eventArgLowerAt(0))) {
return false;
}
String item = path.eventArgLowerAt(2);
if (!item.equals("item")) {
boolean anyMatch = false;
for (Item itemEnt : event.getItems()) {
if (tryItem(new ItemTag(itemEnt.getItemStack()), item)) {
anyMatch = true;
break;
}
}
if (!anyMatch) {
return false;
}
}
if (!runInCheck(path, location)) {
return false;
}
return super.matches(path);
}

@Override
public String getName() {
return "BlockDropsItem";
}

@Override
public ScriptEntryData getScriptEntryData() {
return new BukkitScriptEntryData(PlayerTag.mirrorBukkitPlayer(event.getPlayer()), null);
}

@Override
public ObjectTag getContext(String name) {
if (name.equals("location")) {
return location;
}
else if (name.equals("material")) {
return material;
}
else if (name.equals("drop_entities")) {
ListTag toRet = new ListTag();
for (Item item : event.getItems()) {
toRet.addObject(new EntityTag(item));
}
return toRet;
}
return super.getContext(name);
}

@EventHandler
public void onBlockDropsItem(BlockDropItemEvent event) {
if (EntityTag.isNPC(event.getPlayer())) {
return;
}
material = new MaterialTag(event.getBlockState());
location = new LocationTag(event.getBlock().getLocation());
List<Item> items = new ArrayList<>(event.getItems());
for (Item item : items) {
EntityTag.rememberEntity(item);
}
this.event = event;
fire(event);
for (Item item : items) {
EntityTag.forgetEntity(item);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntryData;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand Down Expand Up @@ -39,6 +38,7 @@ public class PlayerBreaksBlockScriptEvent extends BukkitScriptEvent implements L
// <context.location> returns the LocationTag the block was broken at.
// <context.material> returns the MaterialTag of the block that was broken.
// <context.xp> returns how much XP will be dropped.
// <context.should_drop_items> returns whether the event will drop items.
//
// @Determine
// "NOTHING" to make the block drop no items.
Expand Down Expand Up @@ -106,8 +106,8 @@ public boolean applyDetermination(ScriptPath path, ObjectTag determinationObj) {
if (determinationObj instanceof ElementTag) {
String lower = CoreUtilities.toLowerCase(determination);
if (lower.equals("nothing")) {
cancelled = true;
block.setType(Material.AIR);
event.setExpToDrop(0);
event.setDropItems(false);
return true;
}
else if (((ElementTag) determinationObj).isInt()) {
Expand All @@ -116,8 +116,7 @@ else if (((ElementTag) determinationObj).isInt()) {
}
}
if (Argument.valueOf(determination).matchesArgumentList(ItemTag.class)) {
cancelled = true;
block.setType(Material.AIR);
event.setDropItems(false);
for (ItemTag newItem : ListTag.valueOf(determination, getTagContext(path)).filter(ItemTag.class, path.container, true)) {
block.getWorld().dropItemNaturally(block.getLocation(), newItem.getItemStack()); // Drop each item
}
Expand All @@ -142,6 +141,9 @@ else if (name.equals("material")) {
else if (name.equals("xp")) {
return new ElementTag(event.getExpToDrop());
}
else if (name.equals("should_drop_items")) {
return new ElementTag(event.isDropItems());
}
return super.getContext(name);
}

Expand Down

0 comments on commit ec4c4fb

Please sign in to comment.