Skip to content

Commit

Permalink
feat: visualize all table items
Browse files Browse the repository at this point in the history
  • Loading branch information
cerus committed Dec 15, 2024
1 parent 28deeee commit 7962356
Show file tree
Hide file tree
Showing 22 changed files with 125 additions and 42 deletions.
2 changes: 1 addition & 1 deletion api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-16R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-17R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-18R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion bukkit-19R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-19R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-20R1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-20R2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-20R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-20R4/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-20R4</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R1/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-21R1</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-21R2</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion bukkit-21R3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>

<artifactId>bukkit-21R3</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>parent</artifactId>
<groupId>dev.cerus.visualcrafting</groupId>
<version>1.3.6</version>
<version>1.4.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import dev.cerus.visualcrafting.api.config.Config;
import dev.cerus.visualcrafting.api.version.VersionAdapter;
import dev.cerus.visualcrafting.plugin.listener.CraftingListener;
import dev.cerus.visualcrafting.plugin.listener.CancelCraftingListener;
import dev.cerus.visualcrafting.plugin.listener.CraftingInventoryInteractListener;
import dev.cerus.visualcrafting.plugin.listener.PlayerJoinListener;
import dev.cerus.visualcrafting.plugin.listener.PreItemCraftListener;
import dev.cerus.visualcrafting.plugin.texture.TextureCache;
import dev.cerus.visualcrafting.plugin.texture.TextureDownloader;
import dev.cerus.visualcrafting.plugin.visualizer.DisplayVisualizationController;
Expand Down Expand Up @@ -114,9 +116,17 @@ public void onEnable() {
this.getServer().getScheduler().runTask(this, () ->
visualizationController.entityClick(player, integer)));

this.getServer().getPluginManager().registerEvents(new CraftingListener(this, visualizationController), this);
this.getServer().getPluginManager().registerEvents(new CancelCraftingListener(visualizationController), this);
this.getServer().getPluginManager().registerEvents(new PlayerJoinListener(this, versionAdapter), this);

if (getConfig().getBoolean("only-visualize-recipes", false)) {
// Only visualize valid recipes
this.getServer().getPluginManager().registerEvents(new PreItemCraftListener(this, visualizationController), this);
} else {
// Visualize all items
this.getServer().getPluginManager().registerEvents(new CraftingInventoryInteractListener(this, visualizationController), this);
}

this.getLogger().info("Visual Crafting was enabled!");
this.getLogger().info("Using version adapter '%s' and controller '%s'"
.formatted(versionAdapter.getClass().getSimpleName(), visualizationController.getClass().getSimpleName()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package dev.cerus.visualcrafting.plugin.listener;

import dev.cerus.visualcrafting.plugin.visualizer.VisualizationController;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.CraftingInventory;

public class CancelCraftingListener implements Listener {

private final VisualizationController visualizationController;

public CancelCraftingListener(final VisualizationController visualizationController) {
this.visualizationController = visualizationController;
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onInventoryClose(final InventoryCloseEvent event) {
if (!(event.getView().getTopInventory() instanceof final CraftingInventory inv)) {
return;
}
if (inv.getSize() != 10) {
// Not a crafting table
return;
}
if (inv.getLocation() == null) {
return;
}

// Crafting table closed? Cancel crafting
this.visualizationController.craftingCancelled((Player) event.getView().getPlayer(), inv.getLocation().getBlock());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package dev.cerus.visualcrafting.plugin.listener;

import dev.cerus.visualcrafting.plugin.VisualCraftingPlugin;
import dev.cerus.visualcrafting.plugin.visualizer.VisualizationController;
import java.util.Arrays;
import java.util.Objects;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryDragEvent;
import org.bukkit.event.inventory.InventoryInteractEvent;
import org.bukkit.inventory.CraftingInventory;

public class CraftingInventoryInteractListener implements Listener {
private final VisualCraftingPlugin plugin;
private final VisualizationController visualizationController;

public CraftingInventoryInteractListener(VisualCraftingPlugin plugin, VisualizationController visualizationController) {
this.plugin = plugin;
this.visualizationController = visualizationController;
}

@EventHandler
public void onDrag(InventoryDragEvent event) {
onInteract(event);
}

@EventHandler
public void onClick(InventoryClickEvent event) {
onInteract(event);
}

public void onInteract(InventoryInteractEvent event) {
if (!(event.getView().getTopInventory() instanceof CraftingInventory inv)) {
return;
}
Player player = (Player) event.getWhoClicked();
plugin.getServer().getScheduler().runTaskLater(plugin, () -> {
if (!player.isOnline()) {
return;
}

if (Arrays.stream(inv.getMatrix()).allMatch(Objects::isNull) && inv.getResult() == null) {
this.visualizationController.craftingCancelled(player, inv.getLocation().getBlock());
} else {
this.visualizationController.recipeSelected(inv.getMatrix(),
inv.getResult(),
player,
inv.getLocation().getBlock());
}
}, 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@
import dev.cerus.visualcrafting.plugin.visualizer.VisualizationController;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.CraftingInventory;

public class CraftingListener implements Listener {

public class PreItemCraftListener implements Listener {
private final VisualCraftingPlugin plugin;
private final VisualizationController visualizationController;

public CraftingListener(final VisualCraftingPlugin plugin, final VisualizationController visualizationController) {
public PreItemCraftListener(VisualCraftingPlugin plugin, VisualizationController visualizationController) {
this.plugin = plugin;
this.visualizationController = visualizationController;
}
Expand Down Expand Up @@ -43,22 +40,4 @@ public void onPrepareCraft(final PrepareItemCraftEvent event) {
inv.getLocation().getBlock());
}
}

@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
public void onInventoryClose(final InventoryCloseEvent event) {
if (!(event.getView().getTopInventory() instanceof final CraftingInventory inv)) {
return;
}
if (inv.getSize() != 10) {
// Not a crafting table
return;
}
if (inv.getLocation() == null) {
return;
}

// Crafting table closed? Cancel crafting
this.visualizationController.craftingCancelled((Player) event.getView().getPlayer(), inv.getLocation().getBlock());
}

}
4 changes: 4 additions & 0 deletions plugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ adjust-hitbox: false
# Will not affect DISPLAY rendering
enable-packet-listening: true

# If set to true, only fully-formed recipes will be visualized on the crafting table.
# If set to false, all items on the crafting table will be visualized, even if no valid recipe is formed.
only-visualize-recipes: false

permission:
enable: false
perm: "my.cool.permission"
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>dev.cerus.visualcrafting</groupId>
<artifactId>parent</artifactId>
<packaging>pom</packaging>
<version>1.3.6</version>
<version>1.4.0</version>
<modules>
<module>plugin</module>
<module>api</module>
Expand Down

0 comments on commit 7962356

Please sign in to comment.