Skip to content

Commit

Permalink
cancelBaseEvents property
Browse files Browse the repository at this point in the history
Allows for fully working food and blocks with interactive base materials
  • Loading branch information
btarg committed Dec 12, 2023
1 parent f337079 commit 3bba9bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public abstract class BaseCustomDefinition extends AbstractBaseDefinition {
public List<String> lore;
public String model;
public Material baseMaterial;
public Boolean cancelBaseEvents;

@Getter
private List<EventDefinition> events;
Expand All @@ -39,6 +40,7 @@ public BaseCustomDefinition(Map<String, Object> map) {
this.lore = Objects.requireNonNullElse((List<String>) map.get("lore"), new ArrayList<>());
// Deserialize events
this.events = deserializeEvents(map);
this.cancelBaseEvents = (Boolean) map.getOrDefault("cancelBaseEvents", true);
}

public Component getDisplayName() {
Expand Down Expand Up @@ -111,6 +113,7 @@ public ItemStack createCustomItemStack(int count) {
eventsData.add(event.serialize());
}
map.put("events", eventsData);
map.put("cancelBaseEvents", this.cancelBaseEvents);

return map;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,62 @@ public class CustomEventListener implements Listener {
public void onPlayerInteract(PlayerInteractEvent e) {

Block block = e.getClickedBlock();
Player player = e.getPlayer();

CustomBlockDefinition blockDefinition = CustomBlockUtils.getDefinitionFromBlock(block);
boolean noBlock = blockDefinition == null;
if (blockDefinition != null) {
handleBlockClicked(e, blockDefinition);
}
CustomItemDefinition itemDefinition = RegistryHelper.getDefinitionFromItemstack(e.getItem());
boolean noItem = itemDefinition == null;
if (noBlock && noItem) return;
e.setCancelled(true);
if (itemDefinition != null) {
handleItemClicked(e, itemDefinition);
}

}

private void handleBlockClicked(PlayerInteractEvent e, CustomBlockDefinition definition) {
Player player = e.getPlayer();
e.setCancelled(definition.cancelBaseEvents);
switch (e.getAction()) {
case RIGHT_CLICK_AIR:
if (!noItem) {
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);
itemDefinition.executeEvent(EventNames.ON_RIGHT_CLICK_AIR.toString(), player);
}
case LEFT_CLICK_BLOCK:
definition.executeEvent(EventNames.ON_LEFT_CLICK_BLOCK.toString(), player);
definition.executeEvent(EventNames.ON_LEFT_CLICK.toString(), player);
break;

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

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

default:
break;
}
}

private void handleItemClicked(PlayerInteractEvent e, CustomItemDefinition definition) {
Player player = e.getPlayer();
e.setCancelled(definition.cancelBaseEvents);
switch (e.getAction()) {
case LEFT_CLICK_AIR:
if (!noItem) {
itemDefinition.executeEvent(EventNames.ON_LEFT_CLICK.toString(), player);
itemDefinition.executeEvent(EventNames.ON_LEFT_CLICK_AIR.toString(), player);
}
definition.executeEvent(EventNames.ON_LEFT_CLICK_AIR.toString(), player);
definition.executeEvent(EventNames.ON_LEFT_CLICK.toString(), player);
break;

case RIGHT_CLICK_AIR:
definition.executeEvent(EventNames.ON_RIGHT_CLICK_AIR.toString(), player);
definition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);
break;

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

case RIGHT_CLICK_BLOCK:
definition.executeEvent(EventNames.ON_RIGHT_CLICK_BLOCK.toString(), player);
definition.executeEvent(EventNames.ON_RIGHT_CLICK.toString(), player);
break;

default:
e.setCancelled(false);
break;
}
}
Expand Down

0 comments on commit 3bba9bb

Please sign in to comment.