Skip to content

Commit

Permalink
new API Events
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiao-MoMi committed Sep 19, 2024
1 parent 8c41e26 commit 5802976
Show file tree
Hide file tree
Showing 11 changed files with 303 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* The ActionManager interface manages custom action types and provides methods for handling actions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
import net.momirealms.customcrops.api.core.world.CustomCropsChunk;
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
import net.momirealms.customcrops.api.core.world.Pos3;
import net.momirealms.customcrops.api.event.DropItemActionEvent;
import net.momirealms.customcrops.api.misc.value.MathValue;
import net.momirealms.customcrops.api.util.EventUtils;
import net.momirealms.customcrops.api.util.PlayerUtils;
import net.momirealms.customcrops.common.util.RandomUtils;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -65,19 +68,28 @@ public ActionDropItem(
@Override
protected void triggerAction(Context<T> context) {
Location location = requireNonNull(context.arg(ContextKeys.LOCATION));
Player player = null;
Player player;
if (context.holder() instanceof Player p) {
player = p;
} else {
player = null;
}
int random = RandomUtils.generateRandomInt((int) min.evaluate(context), (int) max.evaluate(context));
ItemStack itemStack = generateItem(location, player, random);
if (itemStack != null) {
if (toInv && player != null) {
PlayerUtils.giveItem(player, itemStack, random);
} else {
location.getWorld().dropItemNaturally(location, itemStack);
plugin.getScheduler().sync().run(() -> {
DropItemActionEvent actionEvent = new DropItemActionEvent(context, location, item, itemStack);
if (EventUtils.fireAndCheckCancel(actionEvent)) {
return;
}
}
ItemStack itemToDrop = actionEvent.item();
if (itemToDrop != null && itemToDrop.getType() != Material.AIR && itemToDrop.getAmount() > 0) {
if (toInv && player != null) {
PlayerUtils.giveItem(player, itemToDrop, itemToDrop.getAmount());
} else {
location.getWorld().dropItemNaturally(location, itemToDrop);
}
}
}, location);
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import net.momirealms.customcrops.api.core.world.CustomCropsChunk;
import net.momirealms.customcrops.api.core.world.CustomCropsWorld;
import net.momirealms.customcrops.api.core.world.Pos3;
import net.momirealms.customcrops.api.event.QualityCropActionEvent;
import net.momirealms.customcrops.api.misc.value.MathValue;
import net.momirealms.customcrops.api.util.EventUtils;
import net.momirealms.customcrops.api.util.PlayerUtils;
import net.momirealms.customcrops.common.util.RandomUtils;
import org.bukkit.Location;
Expand Down Expand Up @@ -74,17 +76,27 @@ public ActionQualityCrops(
protected void triggerAction(Context<T> context) {
Location location = requireNonNull(context.arg(ContextKeys.LOCATION));
int random = RandomUtils.generateRandomInt((int) min.evaluate(context), (int) max.evaluate(context));
Player player = null;
Player player;
if (context.holder() instanceof Player p) {
player = p;
} else {
player = null;
}
for (ItemStack itemStack : generateItem(location, player, random)) {
if (toInv && player != null) {
PlayerUtils.giveItem(player, itemStack, itemStack.getAmount());
} else {
location.getWorld().dropItemNaturally(location, itemStack);
plugin.getScheduler().sync().run(() -> {
List<ItemStack> itemToDrop = generateItem(location, player, random);
QualityCropActionEvent actionEvent = new QualityCropActionEvent(context, location, qualityLoots.clone(), itemToDrop);
if (EventUtils.fireAndCheckCancel(actionEvent)) {
return;
}
}
for (ItemStack itemStack : itemToDrop) {
if (itemStack == null || itemStack.getAmount() == 0 || itemStack.getType() == Material.AIR) continue;
if (toInv && player != null) {
PlayerUtils.giveItem(player, itemStack, itemStack.getAmount());
} else {
location.getWorld().dropItemNaturally(location, itemStack);
}
}
}, location);
}

public List<ItemStack> generateItem(Location location, @Nullable Player player, int randomAmount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import net.momirealms.customcrops.api.core.world.Pos3;
import net.momirealms.customcrops.api.core.wrapper.WrappedBreakEvent;
import net.momirealms.customcrops.api.core.wrapper.WrappedInteractEvent;
import net.momirealms.customcrops.api.event.ScarecrowBreakEvent;
import net.momirealms.customcrops.api.util.EventUtils;
import net.momirealms.customcrops.api.util.LocationUtils;
import org.bukkit.Location;
import org.bukkit.entity.Player;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.momirealms.customcrops.api.event;

import net.momirealms.customcrops.api.context.Context;
import org.bukkit.Location;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

/**
* An event that is triggered when "drop-item" action is executed
*/
public class DropItemActionEvent extends Event implements Cancellable {

private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private final Context<?> context;
private final Location location;
private final String droppedItemID;
private ItemStack item;

public DropItemActionEvent(Context<?> context, Location location, String droppedItemID, ItemStack itemStack) {
this.cancelled = false;
this.context = context;
this.location = location;
this.item = itemStack;
this.droppedItemID = droppedItemID;
}

/**
* Gets the list of handlers for this event.
*
* @return the static handler list.
*/
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

/**
* Gets the list of handlers for this event instance.
*
* @return the handler list.
*/
@NotNull
@Override
public HandlerList getHandlers() {
return getHandlerList();
}

/**
* Returns whether the event is cancelled.
*
* @return true if the event is cancelled, false otherwise.
*/
@Override
public boolean isCancelled() {
return cancelled;
}

/**
* Sets the cancelled state of the event.
*
* @param cancel true to cancel the event, false otherwise.
*/
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}

/**
* Gets the context related to this event
*
* @return context
*/
public Context<?> context() {
return context;
}

/**
* Get the location of the dropped items
*
* @return location
*/
public Location location() {
return location;
}

/**
* Gets the drop
*
* @return the drop
*/
public ItemStack item() {
return item;
}

/**
* Sets the drop
*
* @param item the drop
*/
public void item(ItemStack item) {
this.item = item;
}

/**
* Gets the dropped item's ID
*
* @return the dropped item's ID
*/
public String droppedItemID() {
return droppedItemID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (C) <2024> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package net.momirealms.customcrops.api.event;

import net.momirealms.customcrops.api.context.Context;
import org.bukkit.Location;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.List;

/**
* An event that is triggered when "quality-crops" action is executed
*/
public class QualityCropActionEvent extends Event implements Cancellable {

private static final HandlerList handlers = new HandlerList();
private boolean cancelled;
private final Context<?> context;
private final Location location;
private final List<ItemStack> items;
private final String[] qualityCrops;

public QualityCropActionEvent(Context<?> context, Location location, String[] qualityCrops, List<ItemStack> itemStacks) {
this.cancelled = false;
this.context = context;
this.location = location;
this.items = itemStacks;
this.qualityCrops = qualityCrops;
}

/**
* Gets the list of handlers for this event.
*
* @return the static handler list.
*/
@NotNull
public static HandlerList getHandlerList() {
return handlers;
}

/**
* Gets the list of handlers for this event instance.
*
* @return the handler list.
*/
@NotNull
@Override
public HandlerList getHandlers() {
return getHandlerList();
}

/**
* Returns whether the event is cancelled.
*
* @return true if the event is cancelled, false otherwise.
*/
@Override
public boolean isCancelled() {
return cancelled;
}

/**
* Sets the cancelled state of the event.
*
* @param cancel true to cancel the event, false otherwise.
*/
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}

/**
* Gets the context related to this event
*
* @return context
*/
public Context<?> context() {
return context;
}

/**
* Get the location of the dropped items
*
* @return location
*/
public Location location() {
return location;
}

/**
* Gets the drops
*
* @return the drops
*/
public List<ItemStack> items() {
return items;
}

/**
* Gets the quality crops' ids
*
* @return the quality crops' ids
*/
public String[] qualityCrops() {
return qualityCrops;
}
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Project settings
# Rule: [major update].[feature update].[bug fix]
project_version=3.6.10
project_version=3.6.11
config_version=41
project_group=net.momirealms

Expand Down Expand Up @@ -28,7 +28,7 @@ mojang_brigadier_version=1.0.18
bstats_version=3.0.2
geantyref_version=1.3.15
caffeine_version=3.1.8
rtag_version=1.5.6
rtag_version=1.5.7
exp4j_version=0.4.8
placeholder_api_version=2.11.6
anti_grief_version=0.13
Expand Down
Loading

0 comments on commit 5802976

Please sign in to comment.