Skip to content

Commit 2444092

Browse files
committed
Had ChatGPT fix my BiConsumer cause clearly I'm doing SOMETHING wrong. Lets see if this works.
1 parent 6cfc1d9 commit 2444092

File tree

11 files changed

+123
-67
lines changed

11 files changed

+123
-67
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-38 Bytes
Binary file not shown.

src/main/java/dev/arctic/interactivemenuapi/animation/Animation.java

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,32 @@
77
import org.bukkit.scheduler.BukkitRunnable;
88
import org.bukkit.util.Vector;
99

10+
/**
11+
* Represents an animation that can be applied to elements or divisions in the Interactive Menu API.
12+
*/
1013
public class Animation {
1114
private AnimationType type;
1215
private double stepper;
1316
private int duration;
1417

18+
/**
19+
* Constructs an Animation with the specified type, stepper, and tick duration.
20+
*
21+
* @param type The type of animation.
22+
* @param stepper The step value for the animation.
23+
* @param tickDuration The duration of the animation in ticks.
24+
*/
1525
public Animation(AnimationType type, double stepper, int tickDuration) {
1626
this.type = type;
1727
this.stepper = stepper;
1828
this.duration = tickDuration;
1929
}
2030

31+
/**
32+
* Applies the animation and returns the result, which includes the vector change and opacity.
33+
*
34+
* @return The result of the animation, including the vector change and opacity.
35+
*/
2136
public AnimationResult apply() {
2237
Vector vectorChange = new Vector(0, 0, 0);
2338
int opacity = 100; // Default opacity
@@ -38,6 +53,12 @@ public AnimationResult apply() {
3853
return new AnimationResult(type, vectorChange, opacity);
3954
}
4055

56+
/**
57+
* Updates the location of the input division relative to the root menu location.
58+
*
59+
* @param division The division whose location is to be updated.
60+
* @param rootMenuLocation The root location of the menu.
61+
*/
4162
public void updateInputDivisionLocation(Division division, Location rootMenuLocation) {
4263
Plugin plugin = division.getParentMenu().getPlugin();
4364
new BukkitRunnable() {
@@ -55,57 +76,35 @@ public void run() {
5576
if (type != AnimationType.NONE) {
5677
AnimationResult result = apply();
5778
newLocation.add(result.vectorChange());
58-
setDivisionOpacity(division, result.opacity());
59-
}
60-
61-
division.setCurrentLocation(newLocation);
62-
63-
for (Element element : division.getElements()) {
64-
element.updateLocation(newLocation);
6579
}
6680

81+
// Update the division's location
82+
division.updateLocation(newLocation);
6783
ticks++;
6884
}
69-
}.runTaskTimerAsynchronously(plugin, 0, 1);
70-
}
71-
72-
private void setDivisionOpacity(Division division, int opacity) {
73-
for (Element element : division.getElements()) {
74-
element.setOpacity(opacity);
75-
}
85+
}.runTaskTimer(plugin, 0, 1);
7686
}
7787

88+
/**
89+
* Updates the location of the specified element relative to its parent location.
90+
*
91+
* @param element The element whose location is to be updated.
92+
* @param parentLocation The location of the parent.
93+
*/
7894
public void updateElementLocation(Element element, Location parentLocation) {
79-
Plugin plugin = element.getParentMenu().getPlugin();
80-
new BukkitRunnable() {
81-
int ticks = 0;
95+
Location newLocation = parentLocation.clone().add(element.getOffset());
8296

83-
@Override
84-
public void run() {
85-
if (ticks >= duration) {
86-
cancel();
87-
return;
88-
}
89-
90-
Location newLocation = parentLocation.clone().add(element.getOffset());
91-
92-
if (type != AnimationType.NONE) {
93-
AnimationResult result = apply();
94-
newLocation.add(result.vectorChange());
95-
setElementOpacity(element, result.opacity());
96-
}
97-
98-
element.updateLocation(newLocation);
99-
100-
ticks++;
101-
}
102-
}.runTaskTimerAsynchronously(plugin, 0, 1);
103-
}
97+
if (type != AnimationType.NONE) {
98+
AnimationResult result = apply();
99+
newLocation.add(result.vectorChange());
100+
}
104101

105-
private void setElementOpacity(Element element, int opacity) {
106-
element.setOpacity(opacity);
102+
// Update the element's location
103+
element.setLocation(newLocation);
107104
}
108105

109-
public record AnimationResult(AnimationType type, Vector vectorChange, int opacity) {
110-
}
106+
/**
107+
* Represents the result of an animation, including the type, vector change, and opacity.
108+
*/
109+
public record AnimationResult(AnimationType type, Vector vectorChange, int opacity) {}
111110
}
Binary file not shown.

src/main/java/dev/arctic/interactivemenuapi/interfaces/IElement.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import dev.arctic.interactivemenuapi.objects.Division;
44
import dev.arctic.interactivemenuapi.objects.Menu;
55
import org.bukkit.Location;
6-
import org.bukkit.util.Vector;
6+
import org.bukkit.entity.Player;
7+
import java.util.function.BiConsumer;
78

89
/**
910
* Interface representing a generic Element in the Interactive Menu API.
1011
* All specific element types should implement this interface or its sub-interfaces.
1112
*/
1213
public interface IElement {
13-
1414
/**
1515
* Updates the location of the element relative to its parent division's location.
1616
*
@@ -23,16 +23,6 @@ public interface IElement {
2323
*/
2424
void cleanup();
2525

26-
/**
27-
* Handles interactions with the element. The specific behavior of this method is defined by the
28-
* BiConsumer function provided to the element.setOnInteract method.
29-
*
30-
* @param input The input to the interaction. The type of this parameter is defined by the API user
31-
* when they set the BiConsumer function. It's the responsibility of the API user
32-
* to ensure they're inputting and getting the right type of object.
33-
*/
34-
void onInteract(Object input);
35-
3626
/**
3727
* Gets the parent menu of this element.
3828
*
@@ -66,27 +56,27 @@ public interface IElement {
6656
*
6757
* @return The current location.
6858
*/
69-
Location getCurrentLocation();
59+
Location getLocation();
7060

7161
/**
7262
* Sets the current location of this element.
7363
*
74-
* @param currentLocation The current location.
64+
* @param location The current location.
7565
*/
76-
void setCurrentLocation(Location currentLocation);
66+
void setLocation(Location location);
7767

7868
/**
79-
* Gets the offset of this element relative to the parent division's location.
69+
* Sets the action to be performed when this element is interacted with.
8070
*
81-
* @return The offset.
71+
* @param onInteract BiConsumer that takes a Player and an Object (input).
8272
*/
83-
Vector getOffset();
73+
void setOnInteract(BiConsumer<Player, Object> onInteract);
8474

8575
/**
86-
* Sets the offset of this element relative to the parent division's location.
76+
* Executes the action defined by the BiConsumer when this element is interacted with.
8777
*
88-
* @param offset The offset.
78+
* @param player The player interacting with the element.
79+
* @param input The input object related to the interaction.
8980
*/
90-
void setOffset(Vector offset);
81+
void onInteract(Player player, Object input);
9182
}
92-

src/main/java/dev/arctic/interactivemenuapi/objects/Element.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.bukkit.Color;
88
import org.bukkit.Location;
99
import org.bukkit.entity.Interaction;
10+
import org.bukkit.entity.Player;
1011
import org.bukkit.entity.TextDisplay;
1112
import org.bukkit.metadata.FixedMetadataValue;
1213
import org.bukkit.util.Vector;
@@ -15,15 +16,15 @@
1516

1617
@Getter
1718
@Setter
18-
public abstract class Element<T> implements IElement {
19+
public abstract class Element implements IElement {
1920

2021
protected Menu parentMenu;
2122
protected Division parentDivision;
2223
protected Location location;
2324
protected Vector offset;
2425
protected Interaction interactionEntity;
2526
protected TextDisplay textDisplayEntity;
26-
private BiConsumer<Element<T>, T> onInteract;
27+
private BiConsumer<Player, Object> onInteract;
2728

2829
public Element(Menu parentMenu, Division parentDivision, Vector offset) {
2930
this.parentMenu = parentMenu;
@@ -88,9 +89,19 @@ private Vector getAdjustedOffset(Location anchorLocation, Vector offset, float y
8889
}
8990
}
9091

91-
public void setOnInteract(BiConsumer<Element<T>, T> onInteract) {
92+
public void setOnInteract(BiConsumer<Player, Object> onInteract) {
9293
this.onInteract = onInteract;
9394
}
9495

96+
public void onInteract(Player player, Object input) {
97+
if (onInteract != null) {
98+
onInteract.accept(player, input);
99+
}
100+
}
101+
95102
public abstract void onInteract(Object input);
103+
104+
public abstract Location getCurrentLocation();
105+
106+
public abstract void setCurrentLocation(Location currentLocation);
96107
}

0 commit comments

Comments
 (0)