Skip to content

Commit

Permalink
Use enum for events
Browse files Browse the repository at this point in the history
  • Loading branch information
btarg committed Dec 5, 2023
1 parent e18f003 commit 50b3e4b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/main/java/io/github/btarg/events/CustomEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,35 @@ public void onPlayerInteract(PlayerInteractEvent e) {
switch (e.getAction()) {
case RIGHT_CLICK_AIR:
if (!noItem) {
itemDefinition.executeEvent("onRightClick", player);
itemDefinition.executeEvent("onRightClickAir", player);
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK_AIR.toString(), player);
}
break;

case RIGHT_CLICK_BLOCK:
if (!noBlock)
blockDefinition.executeEvent("onRightClick", player);
blockDefinition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);

if (!noItem) {
itemDefinition.executeEvent("onRightClickBlock", player);
itemDefinition.executeEvent("onRightClick", player);
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK_BLOCK.toString(), player);
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);
}
break;

case LEFT_CLICK_AIR:
if (!noItem) {
itemDefinition.executeEvent("onLeftClick", player);
itemDefinition.executeEvent("onLeftClickAir", player);
itemDefinition.executeEvent(EventNames.ON_LEFT_CLICK.toString(), player);
itemDefinition.executeEvent(EventNames.ON_LEFT_CLICK_AIR.toString(), player);
}

break;

case LEFT_CLICK_BLOCK:
if (!noBlock)
blockDefinition.executeEvent("onLeftClick", player);
blockDefinition.executeEvent(EventNames.ON_LEFT_CLICK.toString(), player);
if (!noItem) {
itemDefinition.executeEvent("onLeftClickBlock", player);
itemDefinition.executeEvent("onLeftClick", player);
itemDefinition.executeEvent(EventNames.ON_LEFT_CLICK_BLOCK.toString(), player);
itemDefinition.executeEvent(EventNames.ON_LEFT_CLICK.toString(), player);
}
break;

Expand Down
33 changes: 33 additions & 0 deletions src/main/java/io/github/btarg/events/EventNames.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.btarg.events;

import java.util.stream.Stream;

public enum EventNames {
ON_RIGHT_CLICK("onRightClick"),
ON_LEFT_CLICK("onLeftClick"),
ON_RIGHT_CLICK_AIR("onRightClickAir"),
ON_LEFT_CLICK_AIR("onLeftClickAir"),
ON_RIGHT_CLICK_BLOCK("onRightClickBlock"),
ON_LEFT_CLICK_BLOCK("onLeftClickBlock"),
ON_PLACED("onPlaced"),
ON_BROKEN("onBroken"),
ON_DAMAGED("onDamaged"),
ON_PUSHED("onPushed");

private final String eventName;

EventNames(String eventName) {
this.eventName = eventName;
}

public static String[] getAllEventNames() {
return Stream.of(EventNames.values())
.map(Enum::toString)
.toArray(String[]::new);
}

@Override
public String toString() {
return eventName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.github.btarg.OrigamiMain;
import io.github.btarg.blockdata.CustomBlockPersistentData;
import io.github.btarg.definitions.CustomBlockDefinition;
import io.github.btarg.events.EventNames;
import io.github.btarg.registry.CustomBlockRegistry;
import io.github.btarg.util.NamespacedKeyHelper;
import io.github.btarg.util.blocks.CustomBlockUtils;
Expand Down Expand Up @@ -114,7 +115,7 @@ public void run() {
}

}
definition.executeEvent("onPlaced", player);
definition.executeEvent(EventNames.ON_PLACED.toString(), player);
}

@EventHandler
Expand Down Expand Up @@ -158,7 +159,7 @@ public void onBlockBroken(BlockBreakEvent e) {
}

// Remove item frame and remove block from database
definition.executeEvent("onBroken", e.getPlayer());
definition.executeEvent(EventNames.ON_BROKEN.toString(), e.getPlayer());
CustomBlockFunctions.onCustomBlockBroken(e.getBlock().getLocation(), definition.breakSound);
linkedItemDisplay.remove();

Expand Down Expand Up @@ -211,7 +212,7 @@ private void onPistonMove(BlockPistonEvent e, List<Block> blocks) {
return;
}

definition.executeEvent("onPushed", null);
definition.executeEvent(EventNames.ON_PUSHED.toString(), null);

// get relative direction from original block of the moved block
Location newLoc = block.getLocation().add(e.getDirection().getDirection()).toBlockLocation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.btarg.listeners.items;

import io.github.btarg.definitions.CustomItemDefinition;
import io.github.btarg.events.EventNames;
import io.github.btarg.registry.RegistryHelper;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
Expand All @@ -21,14 +22,14 @@ public void onItemDamage(PlayerItemDamageEvent event) {
int damage = (int) Math.ceil((double) maxDurability / definition.durability);
event.setDamage(damage);

definition.executeEvent("onDamaged", event.getPlayer());
definition.executeEvent(EventNames.ON_DAMAGED.toString(), event.getPlayer());
}

@EventHandler
public void onItemBroken(PlayerItemBreakEvent event) {
ItemStack itemStack = event.getBrokenItem();
CustomItemDefinition definition = RegistryHelper.getDefinitionFromItemstack(itemStack);
if (definition == null) return;
definition.executeEvent("onBroken", event.getPlayer());
definition.executeEvent(EventNames.ON_BROKEN.toString(), event.getPlayer());
}
}

0 comments on commit 50b3e4b

Please sign in to comment.