Skip to content

Commit d989287

Browse files
feat: add events for interaction with/by the TrashCans Module (#199)
Co-authored-by: Vincent B <[email protected]>
1 parent af7cfed commit d989287

File tree

5 files changed

+252
-0
lines changed

5 files changed

+252
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package minevalley.core.api.trashcans;
2+
3+
import lombok.Setter;
4+
import minevalley.core.api.Depends;
5+
import org.bukkit.Location;
6+
import org.bukkit.block.Block;
7+
import org.bukkit.inventory.ItemStack;
8+
9+
import javax.annotation.Nullable;
10+
11+
/**
12+
* This manager is handled by another module! Do not change the manager-attribute.
13+
* <br>
14+
* Due to the circumstance that your module might load before this manager's handler, it is necessary not to call any
15+
* of the functions below on module start without using a scheduler!
16+
*/
17+
@Depends("TrashCans")
18+
public class TrashCanManager {
19+
20+
@Setter
21+
private static Manager manager;
22+
23+
/**
24+
* Adds new ItemStack to the trash can. If the trash can is full, the operation will be cancelled.
25+
* <p>
26+
*
27+
* @param block the block of the trash can
28+
* @param itemStack the item to add
29+
* <p><b>Note:</b> If the {@code amount} of the {@code itemStack} is greater than 1, the item will be split into multiple ItemStacks.</p>
30+
* @throws IllegalArgumentException if the block is not a trash can
31+
*/
32+
public static void addItem(Block block, ItemStack itemStack) throws IllegalArgumentException {
33+
manager.addItem(block, itemStack);
34+
}
35+
36+
/**
37+
* Removes an ItemStack from the trash can. If the trash can is empty, the operation will be cancelled.
38+
* <br>
39+
* <p>
40+
* <b>Note:</b> Every replica of the ItemStack will be removed. The order or the amount of the ItemStacks in the TrashCan does not matter.
41+
*
42+
* @throws IllegalArgumentException if the block is not a trash can
43+
*/
44+
public static void removeItem(Block block, ItemStack itemStack) throws IllegalArgumentException {
45+
manager.removeItem(block, itemStack);
46+
}
47+
48+
/**
49+
* Clears the trash can at given block.
50+
*
51+
* @param block the block of the trash can
52+
* @throws IllegalArgumentException if the block is not a trash can
53+
*/
54+
public static void clear(Block block) throws IllegalArgumentException {
55+
manager.clear(block);
56+
}
57+
58+
/**
59+
* Returns the nearest trash can to the given location.
60+
*
61+
* @param location the location to search from
62+
* @return the nearest trash can
63+
*/
64+
public static Block getNearestTrashCan(Location location) {
65+
return manager.getNearestTrashCan(location);
66+
}
67+
68+
/**
69+
* Returns the peak ItemStack of the trash can.
70+
*
71+
* @param block the block of the trash can
72+
* @return the peak ItemStack - if empty {@code null} will be returned.
73+
* @throws IllegalArgumentException if the block is not a trash can
74+
*/
75+
@Nullable
76+
public static ItemStack getPeak(Block block) throws IllegalArgumentException {
77+
return manager.getPeak(block);
78+
}
79+
80+
/**
81+
* Returns whether the given block is a trash can.
82+
*
83+
* @param block the block to check
84+
* @return {@code true} if the block is a trash can, {@code false} otherwise
85+
*/
86+
public static boolean isTrashCan(Block block) {
87+
return manager.isTrashCan(block);
88+
}
89+
90+
/**
91+
* Returns whether the trash can is full.
92+
*
93+
* @param block the block of the trash can
94+
* @return {@code true} if the trash can is full, {@code false} otherwise
95+
* @throws IllegalArgumentException if the block is not a trash can
96+
*/
97+
public static boolean isFull(Block block) throws IllegalArgumentException {
98+
return manager.isFull(block);
99+
}
100+
101+
/**
102+
* Returns whether the trash can is empty.
103+
*
104+
* @param block the block of the trash can
105+
* @return {@code true} if the trash can is empty, {@code false} otherwise
106+
* @throws IllegalArgumentException if the block is not a trash can
107+
*/
108+
public static boolean isEmpty(Block block) throws IllegalArgumentException {
109+
return manager.isEmpty(block);
110+
}
111+
112+
public interface Manager {
113+
114+
void addItem(Block block, ItemStack itemStack);
115+
116+
void removeItem(Block block, ItemStack itemStack);
117+
118+
void clear(Block block);
119+
120+
Block getNearestTrashCan(Location location);
121+
122+
ItemStack getPeak(Block block);
123+
124+
boolean isTrashCan(Block block);
125+
126+
boolean isFull(Block block);
127+
128+
boolean isEmpty(Block block);
129+
130+
}
131+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package minevalley.core.api.trashcans.events;
2+
3+
import minevalley.core.api.users.User;
4+
import org.bukkit.block.Block;
5+
6+
/**
7+
* The {@code TrashCanClearEvent} is triggered when a trash can is cleared by a user.
8+
* This event is never automatically triggered by the system nor by regular players emptying the trash can.
9+
*
10+
* @see UserInteractTrashCanEvent
11+
*/
12+
public class TrashCanClearEvent extends UserInteractTrashCanEvent {
13+
14+
/**
15+
* Constructor for {@code FractionalClearTrashCanEvent.}
16+
*
17+
* @param user the user performing the interaction
18+
* @param block the block of the trash can
19+
*/
20+
public TrashCanClearEvent(User user, Block block) {
21+
super(user, block);
22+
}
23+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package minevalley.core.api.trashcans.events;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import minevalley.core.api.users.User;
6+
import org.bukkit.block.Block;
7+
import org.bukkit.inventory.ItemStack;
8+
9+
/**
10+
* The {@code UserAddItemTrashCanEvent} is triggered when a user adds an item to a trash can.
11+
* This event is only triggered when the action is allowed by the system, for example, when the trash can is not full.
12+
*
13+
* @see UserInteractTrashCanEvent
14+
*/
15+
@Setter
16+
@Getter
17+
public class UserAddItemTrashCanEvent extends UserInteractTrashCanEvent {
18+
19+
private ItemStack itemStack;
20+
21+
/**
22+
* Constructor for {@code UserAddItemTrashCanEvent.}
23+
*
24+
* @param user the user performing the interaction
25+
* @param block the block of the trash can
26+
* @param itemStack the added item
27+
*/
28+
public UserAddItemTrashCanEvent(User user, Block block, ItemStack itemStack) {
29+
super(user, block);
30+
this.itemStack = itemStack;
31+
}
32+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package minevalley.core.api.trashcans.events;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import minevalley.core.api.users.User;
6+
import minevalley.core.api.users.events.UserEvent;
7+
import org.bukkit.block.Block;
8+
import org.bukkit.event.Cancellable;
9+
10+
/**
11+
* The {@code UserInteractTrashCanEvent} is triggered on any interaction with a trash can.
12+
* This event is the base class for all trash can interaction events.
13+
*
14+
* @see minevalley.core.api.trashcans.events.TrashCanClearEvent
15+
* @see minevalley.core.api.trashcans.events.UserAddItemTrashCanEvent
16+
* @see minevalley.core.api.trashcans.events.UserRemoveItemTrashCanEvent
17+
*/
18+
@Setter
19+
@Getter
20+
public abstract class UserInteractTrashCanEvent extends UserEvent implements Cancellable {
21+
22+
private final Block block;
23+
private boolean cancelled;
24+
25+
/**
26+
* Constructor for {@code UserInteractTrashCanEvent.}
27+
*
28+
* @param user The user interacting with the trash can.
29+
*/
30+
public UserInteractTrashCanEvent(User user, Block block) {
31+
super(user);
32+
this.block = block;
33+
}
34+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package minevalley.core.api.trashcans.events;
2+
3+
import lombok.Getter;
4+
import lombok.Setter;
5+
import minevalley.core.api.users.User;
6+
import org.bukkit.block.Block;
7+
import org.bukkit.inventory.ItemStack;
8+
9+
/**
10+
* The {@code UserRemoveItemTrashCanEvent} is triggered when a user removes an item from a trash can.
11+
* This event is only triggered when the action is allowed by the system, for example, when the trash can is not empty.
12+
*
13+
* @see UserInteractTrashCanEvent
14+
*/
15+
@Setter
16+
@Getter
17+
public class UserRemoveItemTrashCanEvent extends UserInteractTrashCanEvent {
18+
19+
private ItemStack itemStack;
20+
21+
/**
22+
* Constructor for {@code UserRemoveItemTrashCanEvent.}
23+
*
24+
* @param user the user performing the interaction
25+
* @param block the block of the trash can
26+
* @param itemStack the removed item
27+
*/
28+
public UserRemoveItemTrashCanEvent(User user, Block block, ItemStack itemStack) {
29+
super(user, block);
30+
this.itemStack = itemStack;
31+
}
32+
}

0 commit comments

Comments
 (0)