Skip to content

Commit

Permalink
feat(actions): EventOnly mode in bucket related action
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Oct 22, 2023
1 parent 865f36d commit b7f657e
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package org.kunlab.scenamatica.action.actions.player.bucket;

import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerBucketEvent;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kunlab.scenamatica.action.actions.player.AbstractPlayerAction;
import org.kunlab.scenamatica.action.utils.VoxelUtils;
import org.kunlab.scenamatica.commons.utils.BeanUtils;
import org.kunlab.scenamatica.commons.utils.Utils;
import org.kunlab.scenamatica.interfaces.context.Actor;
import org.kunlab.scenamatica.interfaces.scenario.ScenarioEngine;
import org.kunlab.scenamatica.interfaces.scenariofile.misc.BlockBean;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -37,6 +35,43 @@ protected static boolean isEmptyBucket(Material bucket)
return bucket == Material.BUCKET || bucket == Material.LEGACY_BUCKET;
}

@Nullable
protected static Material convertBucketToLiquid(Material bucket)
{
switch (bucket)
{
case COD_BUCKET:
case PUFFERFISH_BUCKET:
case SALMON_BUCKET:
case TROPICAL_FISH_BUCKET:
case WATER_BUCKET:
return Material.WATER;
case LAVA_BUCKET:
case LEGACY_LAVA_BUCKET:
return Material.LAVA;
default:
return null;
}
}

@Nullable
protected static EntityType convertBucketToEntity(Material bucket)
{
switch (bucket)
{
case COD_BUCKET:
return EntityType.COD;
case PUFFERFISH_BUCKET:
return EntityType.PUFFERFISH;
case SALMON_BUCKET:
return EntityType.SALMON;
case TROPICAL_FISH_BUCKET:
return EntityType.TROPICAL_FISH;
default:
return null;
}
}

@SuppressWarnings("deprecation")
protected static boolean isBucketMaterial(Material bucket)
{
Expand All @@ -61,41 +96,30 @@ protected static boolean isBucketMaterial(Material bucket)
}
}

protected static ItemStack getBucket(Actor actor, BucketActionArgument argument)
protected static ItemStack getBucket(Player player, BucketActionArgument argument)
{
ItemStack stack;
if (argument.getBucket() == null)
{
stack = actor.getPlayer().getInventory().getItemInMainHand();
stack = player.getInventory().getItemInMainHand();
if (!isBucketMaterial(stack.getType()))
throw new IllegalArgumentException("No bucket in the main hand, " + "please specify the bucket item implicitly: " + actor.getPlayer().getName());
throw new IllegalArgumentException("No bucket in the main hand, " + "please specify the bucket item implicitly: " + player.getName());
}
else
{
stack = new ItemStack(argument.getBucket());
if (!isBucketMaterial(stack.getType()))
throw new IllegalArgumentException("Item " + argument.getBucket() + " is not a bucket.");
actor.getPlayer().getInventory().setItemInMainHand(stack);
player.getInventory().setItemInMainHand(stack);
}

return stack;
}

protected static Block getPlaceAt(Player player, BucketActionArgument argument, ScenarioEngine engine)
{
BlockBean blockCandidate = null;
if (argument.getBlockClicked() != null)
blockCandidate = argument.getBlockClicked();
else if (argument.getBlock() != null)
blockCandidate = argument.getBlock();

if (blockCandidate != null)
{
Location fixedLoc = Utils.assignWorldToBlockLocation(blockCandidate, engine);
BeanUtils.applyBlockBeanData(blockCandidate, fixedLoc);

return fixedLoc.getBlock();
}
return BeanUtils.applyBlockBeanData(engine, argument.getBlockClicked());

Block block = player.getTargetBlockExact(4);
if (block == null)
Expand Down Expand Up @@ -126,5 +150,4 @@ public boolean checkMatchedBucketEvent(@NotNull A argument, @NotNull ScenarioEng
&& (argument.getBucket() == null || argument.getBucket() == e.getBucket())
&& (argument.getHand() == null || argument.getHand() == e.getHand());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.EquipmentSlot;
import org.jetbrains.annotations.NotNull;
import org.kunlab.scenamatica.action.actions.player.AbstractPlayerActionArgument;
import org.kunlab.scenamatica.commons.utils.MapUtils;
import org.kunlab.scenamatica.enums.ScenarioType;
import org.kunlab.scenamatica.interfaces.scenario.ScenarioEngine;
import org.kunlab.scenamatica.interfaces.scenariofile.BeanSerializer;
import org.kunlab.scenamatica.interfaces.scenariofile.inventory.ItemStackBean;
import org.kunlab.scenamatica.interfaces.scenariofile.misc.BlockBean;
Expand All @@ -25,26 +28,22 @@ public class BucketActionArgument extends AbstractPlayerActionArgument
public static final String KEY_BLOCK_FACE = "blockFace";
public static final String KEY_BUCKET = "bucket";
public static final String KEY_HAND = "hand";
public static final String KEY_EVENT_ONLY = "eventOnly";

ItemStackBean itemStack;
BlockBean block;
BlockBean blockClicked;
BlockFace blockFace;
Material bucket;
EquipmentSlot hand;
boolean eventOnly;

public BucketActionArgument(BucketActionArgument origin)
{
super(origin.getTargetSpecifier());
this.itemStack = origin.itemStack;
this.block = origin.block;
this.blockClicked = origin.blockClicked;
this.blockFace = origin.blockFace;
this.bucket = origin.bucket;
this.hand = origin.hand;
this(origin.getTargetSpecifier(), origin.itemStack, origin.block, origin.blockClicked, origin.blockFace, origin.bucket, origin.hand, origin.eventOnly);
}

public BucketActionArgument(String target, ItemStackBean itemStack, BlockBean block, BlockBean blockClicked, BlockFace blockFace, Material bucket, EquipmentSlot hand)
public BucketActionArgument(String target, ItemStackBean itemStack, BlockBean block, BlockBean blockClicked, BlockFace blockFace, Material bucket, EquipmentSlot hand, boolean eventOnly)
{
super(target);
this.itemStack = itemStack;
Expand All @@ -53,6 +52,7 @@ public BucketActionArgument(String target, ItemStackBean itemStack, BlockBean bl
this.blockFace = blockFace;
this.bucket = bucket;
this.hand = hand;
this.eventOnly = eventOnly;
}

public static BucketActionArgument deserialize(Map<String, Object> map, BeanSerializer serializer)
Expand Down Expand Up @@ -88,7 +88,8 @@ public static BucketActionArgument deserialize(Map<String, Object> map, BeanSeri
blockClicked,
MapUtils.getAsEnumOrNull(map, BucketActionArgument.KEY_BLOCK_FACE, BlockFace.class),
MapUtils.getAsEnumOrNull(map, BucketActionArgument.KEY_BUCKET, Material.class),
MapUtils.getAsEnumOrNull(map, BucketActionArgument.KEY_HAND, EquipmentSlot.class)
MapUtils.getAsEnumOrNull(map, BucketActionArgument.KEY_HAND, EquipmentSlot.class),
MapUtils.getOrDefault(map, BucketActionArgument.KEY_EVENT_ONLY, false)
);
}

Expand All @@ -109,6 +110,15 @@ public boolean isSame(TriggerArgument argument)
&& this.hand == arg.hand;
}

@Override
public void validate(@NotNull ScenarioEngine engine, @NotNull ScenarioType type)
{
super.validate(engine, type);

if (!(this.block == null || this.blockClicked == null))
throw new IllegalArgumentException("Both " + KEY_BLOCK + " and " + KEY_BLOCK_CLICKED + " are specified.");
}

@Override
public String getArgumentString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,20 @@

import lombok.EqualsAndHashCode;
import lombok.Value;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerBucketEmptyEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kunlab.scenamatica.action.utils.PlayerUtils;
import org.kunlab.scenamatica.commons.utils.BeanUtils;
import org.kunlab.scenamatica.enums.ScenarioType;
import org.kunlab.scenamatica.interfaces.action.types.Executable;
import org.kunlab.scenamatica.interfaces.action.types.Watchable;
Expand Down Expand Up @@ -39,21 +44,52 @@ public void execute(@NotNull ScenarioEngine engine, @Nullable Argument argument)
argument = this.requireArgsNonNull(argument);

Player player = argument.getTarget();
Actor actor = PlayerUtils.getActorOrThrow(engine, player);
ItemStack stack = getBucket(actor, argument);
ItemStack stack = getBucket(player, argument);
Block block = getPlaceAt(player, argument, engine);
BlockFace direction = getDirection(player, block, argument);
Actor actor = PlayerUtils.getActorOrThrow(engine, player);

if (isEmptyBucket(stack.getType()))
throw new IllegalArgumentException("The bucket is empty: " + stack.getType() + " held by " + player.getName());

if (argument.isEventOnly())
{
Block blockClicked = null;
if (argument.getBlockClicked() != null)
blockClicked = BeanUtils.applyBlockBeanData(engine, argument.getBlockClicked());
this.doEventOnlyMode(player, block, blockClicked, direction, stack.getType(), stack, argument.getHand());
}

actor.placeItem(
block.getLocation(),
stack,
direction
);
}

private void doEventOnlyMode(Player who, Block block, Block blockClicked, BlockFace blockFace, Material bucket, ItemStack itemInHand, EquipmentSlot hand)
{
PlayerBucketEmptyEvent event = new PlayerBucketEmptyEvent(who, block, blockClicked, blockFace, bucket, itemInHand, hand);
Bukkit.getServer().getPluginManager().callEvent(event);

if (event.isCancelled())
return;

itemInHand = event.getItemStack();

Material liquid = convertBucketToLiquid(bucket);
if (liquid == null)
throw new IllegalArgumentException("Unknown bucket type: " + bucket + ", this action needs a bucket filled with water or lava.");
EntityType entityToSpawn = convertBucketToEntity(bucket);

block.setType(liquid);
if (entityToSpawn != null)
block.getWorld().spawnEntity(block.getLocation(), entityToSpawn);

if (itemInHand != null)
who.getInventory().setItem(hand, itemInHand);
}

@Override
public boolean isFired(@NotNull Argument argument, @NotNull ScenarioEngine engine, @NotNull Event event)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,11 +342,11 @@ public static void applyBlockBeanData(@NotNull BlockBean bean)
applyBlockBeanData(bean, loc);
}

public static void applyBlockBeanData(@NotNull BlockBean blockBean, @Nullable Location location)
public static Block applyBlockBeanData(@NotNull BlockBean blockBean, @Nullable Location location)
{
Location targetLoc = location == null ? blockBean.getLocation(): location;
if (targetLoc == null)
return;
return null;

Block block = targetLoc.getBlock();
if (blockBean.getType() != null)
Expand All @@ -366,6 +366,26 @@ public static void applyBlockBeanData(@NotNull BlockBean blockBean, @Nullable Lo
new FixedMetadataValue(owningPlugin, entry.getValue())
);
}

return block;
}

public static Block applyBlockBeanData(@NotNull ScenarioEngine engine, @NotNull BlockBean block, @Nullable Location location)
{
Location targetLoc;
if (location != null)
targetLoc = Utils.assignWorldToLocation(location, engine);
else if (block.getLocation() != null)
targetLoc = Utils.assignWorldToLocation(block.getLocation(), engine);
else
return null;

return applyBlockBeanData(block, targetLoc);
}

public static Block applyBlockBeanData(@NotNull ScenarioEngine engine, @NotNull BlockBean block)
{
return applyBlockBeanData(engine, block, null);
}

public static boolean isSame(@NotNull EntityBean entityBean, @NotNull Entity entity, boolean strict)
Expand Down

0 comments on commit b7f657e

Please sign in to comment.