Skip to content

Commit 2abc1af

Browse files
committed
feat(handler): Added a Hit handler.
1 parent b89ab62 commit 2abc1af

File tree

6 files changed

+291
-2
lines changed

6 files changed

+291
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package fr.hashtek.spigot.hashgui.handler.hit;
2+
3+
import fr.hashtek.spigot.hashitem.HashItem;
4+
import org.bukkit.inventory.meta.ItemMeta;
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
10+
public class HashGuiHit
11+
{
12+
13+
private final HashMap<String, ArrayList<HitHandler>> hitHandlers;
14+
15+
16+
/**
17+
* Creates a new instance of HashGUIInteraction.
18+
*/
19+
public HashGuiHit()
20+
{
21+
this.hitHandlers = new HashMap<String, ArrayList<HitHandler>>();
22+
}
23+
24+
25+
/**
26+
* Adds a hit handler for a certain title.
27+
*
28+
* @param title Title
29+
* @param handler Interaction handler
30+
* @return Returns itself.
31+
*/
32+
private HashGuiHit addHitHandler(String title, HitHandler handler)
33+
{
34+
this.hitHandlers.computeIfAbsent(title, k -> new ArrayList<HitHandler>());
35+
36+
for (HitHandler h : this.hitHandlers.get(title))
37+
if (handler.equals(h))
38+
return this;
39+
40+
this.hitHandlers.get(title).add(handler);
41+
return this;
42+
}
43+
44+
/**
45+
* Adds a hit handler for an item.
46+
*
47+
* @param item Item
48+
* @return Returns itself.
49+
*/
50+
public HashGuiHit addHitHandler(HashItem item)
51+
{
52+
List<HitHandler> hitHandlers = item.getHitHandlers();
53+
54+
if (hitHandlers == null || hitHandlers.isEmpty())
55+
return this;
56+
57+
final ItemMeta meta = item.getItemStack().getItemMeta();
58+
final String itemName = meta.getDisplayName();
59+
60+
for (HitHandler handler: item.getHitHandlers())
61+
this.addHitHandler(itemName, handler);
62+
63+
return this;
64+
}
65+
66+
67+
/**
68+
* @return Registered interact handlers
69+
*/
70+
public HashMap<String, ArrayList<HitHandler>> getHitHandlers()
71+
{
72+
return this.hitHandlers;
73+
}
74+
75+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package fr.hashtek.spigot.hashgui.handler.hit;
2+
3+
import org.bukkit.entity.Player;
4+
import org.bukkit.inventory.ItemStack;
5+
6+
public interface HitAction
7+
{
8+
9+
/**
10+
* Function called when item is used in a hit.
11+
*
12+
* @param attacker Player who hit
13+
* @param victim Player who got hit
14+
* @param item Used item
15+
*/
16+
void execute(
17+
Player attacker,
18+
Player victim,
19+
ItemStack item
20+
);
21+
22+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package fr.hashtek.spigot.hashgui.handler.hit;
2+
3+
public class HitHandler
4+
{
5+
6+
private HitAction hitAction;
7+
private boolean isOnlyKill;
8+
9+
10+
/**
11+
* Creates an empty instance of InteractHandler.
12+
*/
13+
public HitHandler()
14+
{
15+
this.isOnlyKill = false;
16+
}
17+
18+
19+
/**
20+
* Makes that the action is fired only when the hit results
21+
* in a kill. Or not.
22+
*
23+
* @param isOnlyKill Only kill
24+
* @return Returns itself.
25+
*/
26+
public HitHandler setOnlyKill(boolean isOnlyKill)
27+
{
28+
this.isOnlyKill = isOnlyKill;
29+
return this;
30+
}
31+
32+
/**
33+
* Sets handler's hit action. (on hit, then do...)
34+
*
35+
* @param hitAction Interact action
36+
* @return Returns itself.
37+
*/
38+
public HitHandler setHitAction(HitAction hitAction)
39+
{
40+
this.hitAction = hitAction;
41+
return this;
42+
}
43+
44+
/**
45+
* @param handler Handler to compare
46+
* @return Returns true if both handlers contains the same instructions
47+
*/
48+
public boolean equals(HitHandler handler)
49+
{
50+
return
51+
this.hitAction.equals(handler.hitAction) &&
52+
this.isOnlyKill == handler.isOnlyKill();
53+
}
54+
55+
56+
/**
57+
* @return True if the handler fired only when the hit results in a kill
58+
*/
59+
public boolean isOnlyKill()
60+
{
61+
return this.isOnlyKill;
62+
}
63+
64+
/**
65+
* @return Assigned action
66+
*/
67+
public HitAction getHitAction()
68+
{
69+
return this.hitAction;
70+
}
71+
72+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package fr.hashtek.spigot.hashgui.listener;
2+
3+
import fr.hashtek.spigot.hashgui.handler.hit.HashGuiHit;
4+
import fr.hashtek.spigot.hashgui.handler.hit.HitHandler;
5+
import org.bukkit.entity.Player;
6+
import org.bukkit.event.EventHandler;
7+
import org.bukkit.event.Listener;
8+
import org.bukkit.event.entity.EntityDamageByEntityEvent;
9+
import org.bukkit.inventory.ItemStack;
10+
import org.bukkit.inventory.meta.ItemMeta;
11+
12+
import java.util.ArrayList;
13+
14+
public class HashGuiHitListener implements Listener
15+
{
16+
17+
private final HashGuiHit hitManager;
18+
19+
20+
/**
21+
* Creates a new instance of HashGuiKillListener, with
22+
* a kill manager for kill handling.
23+
*
24+
* @param hitManager Hit manager
25+
*/
26+
public HashGuiHitListener(HashGuiHit hitManager)
27+
{
28+
this.hitManager = hitManager;
29+
}
30+
31+
32+
/**
33+
* Executes the kill actions linked to the used item.
34+
*
35+
* @param attacker Player who hit
36+
* @param victim Player who got hit
37+
* @param item Used item
38+
*/
39+
private void processHit(
40+
Player attacker,
41+
Player victim,
42+
ItemStack item,
43+
boolean isKill
44+
)
45+
{
46+
final ItemMeta meta = item.getItemMeta();
47+
final String itemDisplayName = meta.getDisplayName();
48+
final ArrayList<HitHandler> hitHandlers = this.hitManager.getHitHandlers().get(itemDisplayName);
49+
50+
if (hitHandlers == null || hitHandlers.isEmpty())
51+
return;
52+
53+
hitHandlers.stream()
54+
.filter((HitHandler hitHandler) ->
55+
isKill == hitHandler.isOnlyKill())
56+
.forEach((HitHandler handler) ->
57+
handler.getHitAction().execute(attacker, victim, item));
58+
}
59+
60+
/**
61+
* Hit handling
62+
*/
63+
@EventHandler
64+
public void onInteract(EntityDamageByEntityEvent event)
65+
{
66+
if (!(event.getDamager() instanceof Player && event.getEntity() instanceof Player))
67+
return;
68+
69+
final Player attacker = (Player) event.getDamager();
70+
final Player victim = (Player) event.getEntity();
71+
final ItemStack itemUsed = attacker.getInventory().getItemInHand();
72+
73+
this.processHit(
74+
attacker,
75+
victim,
76+
itemUsed,
77+
victim.getHealth() - event.getFinalDamage() <= 0
78+
);
79+
}
80+
81+
}

src/main/java/fr/hashtek/spigot/hashgui/manager/HashGuiManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package fr.hashtek.spigot.hashgui.manager;
22

33
import fr.hashtek.spigot.hashgui.handler.hold.HashGuiHold;
4+
import fr.hashtek.spigot.hashgui.handler.hit.HashGuiHit;
45
import fr.hashtek.spigot.hashgui.listener.HashGuiHoldListener;
6+
import fr.hashtek.spigot.hashgui.listener.HashGuiHitListener;
57
import org.bukkit.plugin.PluginManager;
68
import org.bukkit.plugin.java.JavaPlugin;
79

@@ -19,6 +21,7 @@ public class HashGuiManager
1921
private HashGuiClick clickManager;
2022
private HashGuiInteraction interactionManager;
2123
private HashGuiHold holdManager;
24+
private HashGuiHit hitManager;
2225

2326

2427
/**
@@ -47,10 +50,12 @@ public HashGuiManager setup()
4750
this.clickManager = new HashGuiClick();
4851
this.interactionManager = new HashGuiInteraction();
4952
this.holdManager = new HashGuiHold();
53+
this.hitManager = new HashGuiHit();
5054

5155
this.pluginManager.registerEvents(new HashGuiClickListener(this.clickManager), this.plugin);
5256
this.pluginManager.registerEvents(new HashGuiInteractListener(this.interactionManager), this.plugin);
5357
this.pluginManager.registerEvents(new HashGuiHoldListener(this.holdManager), this.plugin);
58+
this.pluginManager.registerEvents(new HashGuiHitListener(this.hitManager), this.plugin);
5459

5560
return this;
5661
}
@@ -80,4 +85,12 @@ public HashGuiHold getHoldManager()
8085
return this.holdManager;
8186
}
8287

88+
/**
89+
* @return Hit manager
90+
*/
91+
public HashGuiHit getHitManager()
92+
{
93+
return this.hitManager;
94+
}
95+
8396
}

src/main/java/fr/hashtek/spigot/hashitem/HashItem.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import fr.hashtek.spigot.hashgui.handler.hold.HoldHandler;
7+
import fr.hashtek.spigot.hashgui.handler.hit.HitHandler;
78
import org.bukkit.ChatColor;
89
import org.bukkit.Material;
910
import org.bukkit.enchantments.Enchantment;
@@ -24,6 +25,7 @@ public class HashItem
2425
private List<ClickHandler> clickHandlers;
2526
private List<InteractHandler> interactHandlers;
2627
private List<HoldHandler> holdHandlers;
28+
private List<HitHandler> hitHandlers;
2729

2830

2931
/**
@@ -111,13 +113,14 @@ public static HashItem separator(Byte glassColor, HashGuiManager guiManager)
111113
*
112114
* @return Returns itself.
113115
*/
114-
public HashItem build(HashGuiManager guiManager) throws IllegalArgumentException
116+
public HashItem build(HashGuiManager guiManager)
115117
{
116118
this.itemStack.setItemMeta(this.itemMeta);
117119

118120
guiManager.getClickManager().addClickHandler(this);
119121
guiManager.getInteractionManager().addInteractHandler(this);
120122
guiManager.getHoldManager().addHoldHandler(this);
123+
guiManager.getHitManager().addHitHandler(this);
121124
return this;
122125
}
123126

@@ -126,7 +129,7 @@ public HashItem build(HashGuiManager guiManager) throws IllegalArgumentException
126129
*
127130
* @return Returns itself.
128131
*/
129-
public HashItem build() throws IllegalArgumentException
132+
public HashItem build()
130133
{
131134
this.itemStack.setItemMeta(this.itemMeta);
132135
return this;
@@ -433,6 +436,21 @@ public HashItem addHoldHandler(HoldHandler holdHandler)
433436
this.holdHandlers.add(holdHandler);
434437
return this;
435438
}
439+
440+
/**
441+
* Adds a hit handler to the item.
442+
*
443+
* @param hitHandler Hit handler.
444+
* @return Returns itself.
445+
*/
446+
public HashItem addHitHandler(HitHandler hitHandler)
447+
{
448+
if (this.hitHandlers == null)
449+
this.hitHandlers = new ArrayList<HitHandler>();
450+
451+
this.hitHandlers.add(hitHandler);
452+
return this;
453+
}
436454

437455
/**
438456
* @return Item's click handlers.
@@ -458,4 +476,12 @@ public List<HoldHandler> getHoldHandlers()
458476
return this.holdHandlers;
459477
}
460478

479+
/**
480+
* @return Item's hit handlers.
481+
*/
482+
public List<HitHandler> getHitHandlers()
483+
{
484+
return this.hitHandlers;
485+
}
486+
461487
}

0 commit comments

Comments
 (0)