Skip to content

Commit

Permalink
Inventory Events
Browse files Browse the repository at this point in the history
  • Loading branch information
GliczDev committed Sep 28, 2024
1 parent c999f89 commit afa0e11
Show file tree
Hide file tree
Showing 21 changed files with 304 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package me.glicz.airflow.api.event;

public interface Cancellable {
boolean isCancelled();

void setCancelled(boolean cancelled);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.glicz.airflow.api.event.inventory;

import me.glicz.airflow.api.entity.living.Humanoid;
import me.glicz.airflow.api.event.Cancellable;
import me.glicz.airflow.api.inventory.Inventory;
import me.glicz.airflow.api.inventory.menu.view.MenuView;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public class InventoryClickEvent extends InventoryEvent implements Cancellable {
private final int slot;
private boolean cancelled;

@ApiStatus.Internal
public InventoryClickEvent(@NotNull Humanoid viewer, @NotNull MenuView menuView, Inventory inventory, int slot) {
super(viewer, menuView, inventory);
this.slot = slot;
}

public int getSlot() {
return slot;
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.glicz.airflow.api.event.inventory;

import me.glicz.airflow.api.entity.living.Humanoid;
import me.glicz.airflow.api.event.inventory.menu.MenuEvent;
import me.glicz.airflow.api.inventory.Inventory;
import me.glicz.airflow.api.inventory.menu.view.MenuView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnknownNullability;

public abstract class InventoryEvent extends MenuEvent {
private final Inventory inventory;

public InventoryEvent(@NotNull Humanoid viewer, @NotNull MenuView menuView, Inventory inventory) {
super(viewer, menuView);
this.inventory = inventory;
}

public @UnknownNullability Inventory getInventory() {
return inventory;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.glicz.airflow.api.event.inventory.menu;

import me.glicz.airflow.api.entity.living.Humanoid;
import me.glicz.airflow.api.inventory.menu.view.MenuView;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public class MenuCloseEvent extends MenuEvent {
@ApiStatus.Internal
public MenuCloseEvent(@NotNull Humanoid viewer, @NotNull MenuView menuView) {
super(viewer, menuView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.glicz.airflow.api.event.inventory.menu;

import me.glicz.airflow.api.entity.living.Humanoid;
import me.glicz.airflow.api.event.Event;
import me.glicz.airflow.api.inventory.menu.view.MenuView;
import org.jetbrains.annotations.NotNull;

public abstract class MenuEvent extends Event {
private final Humanoid viewer;
private final MenuView menuView;

public MenuEvent(@NotNull Humanoid viewer, @NotNull MenuView menuView) {
this.viewer = viewer;
this.menuView = menuView;
}

public @NotNull Humanoid getViewer() {
return viewer;
}

public @NotNull MenuView getMenuView() {
return menuView;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.glicz.airflow.api.event.inventory.menu;

import me.glicz.airflow.api.entity.living.Humanoid;
import me.glicz.airflow.api.event.Cancellable;
import me.glicz.airflow.api.inventory.menu.view.MenuView;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

public class MenuOpenEvent extends MenuEvent implements Cancellable {
private boolean cancelled;

@ApiStatus.Internal
public MenuOpenEvent(@NotNull Humanoid viewer, @NotNull MenuView menuView) {
super(viewer, menuView);
}

@Override
public boolean isCancelled() {
return cancelled;
}

@Override
public void setCancelled(boolean cancelled) {
this.cancelled = cancelled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import me.glicz.airflow.api.entity.living.Player;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerJoinEvent extends PlayerEvent {
private Component message;

@ApiStatus.Internal
public PlayerJoinEvent(Player player, Component message) {
public PlayerJoinEvent(@NotNull Player player, @Nullable Component message) {
super(player);
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import me.glicz.airflow.api.entity.living.Player;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class PlayerQuitEvent extends PlayerEvent {
private Component message;

@ApiStatus.Internal
public PlayerQuitEvent(Player player, Component message) {
public PlayerQuitEvent(@NotNull Player player, @Nullable Component message) {
super(player);
this.message = message;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package me.glicz.airflow.api.inventory;

import org.jetbrains.annotations.NotNull;

import java.util.Collection;

public interface ComposedInventory extends Inventory {
Collection<Inventory> getInventories();
@NotNull Collection<Inventory> getInventories();

@NotNull Inventory getInventoryForSlot(int slot);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package me.glicz.airflow.api.inventory.menu.view;

import me.glicz.airflow.api.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

public interface ItemCombinerView extends MenuView {
Inventory getResultInventory();
@NotNull Inventory getResultInventory();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package me.glicz.airflow.api.inventory.menu.view;

import me.glicz.airflow.api.inventory.Inventory;
import org.jetbrains.annotations.NotNull;

public interface LoomView extends MenuView {
Inventory getOutputInventory();
@NotNull Inventory getOutputInventory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface MenuView {
default @NotNull PlayerInventory getViewerInventory() {
return getViewer().getInventory();
}

@NotNull Inventory getInventoryForSlot(int slot);
}
5 changes: 3 additions & 2 deletions Airflow-Server/patches/0011-Inventory-API.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Subject: [PATCH] Inventory API


diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java
index 9052a3899aedddb7265b8cef7c80e3addd79d3ab..7ad86e7721107065d9c26d98ca4d4ea3f920652e 100644
index 9052a3899aedddb7265b8cef7c80e3addd79d3ab..d6a0cd9c22785edb7e7966141b301c02c456d076 100644
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -1081,8 +1081,13 @@ public class ServerPlayer extends Player {
@@ -1081,8 +1081,14 @@ public class ServerPlayer extends Player {

return OptionalInt.empty();
} else {
+ // Airflow start - Inventory API
+ abstractContainerMenu.getAirMenuView().setTitle(menu.getDisplayName());
+ abstractContainerMenu.getAirMenuView().openScreen();
+ /*
this.connection
.send(new ClientboundOpenScreenPacket(abstractContainerMenu.containerId, abstractContainerMenu.getType(), menu.getDisplayName()));
Expand Down
46 changes: 46 additions & 0 deletions Airflow-Server/patches/0013-Inventory-Events.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: GliczDev <67753196+GliczDev@users.noreply.github.com>
Date: Sat, 28 Sep 2024 17:04:56 +0200
Subject: [PATCH] Inventory Events


diff --git a/net/minecraft/server/level/ServerPlayer.java b/net/minecraft/server/level/ServerPlayer.java
index d6a0cd9c22785edb7e7966141b301c02c456d076..45653fe04137c5eca009b108f04bd2812af54348 100644
--- a/net/minecraft/server/level/ServerPlayer.java
+++ b/net/minecraft/server/level/ServerPlayer.java
@@ -1083,6 +1083,11 @@ public class ServerPlayer extends Player {
} else {
// Airflow start - Inventory API
abstractContainerMenu.getAirMenuView().setTitle(menu.getDisplayName());
+ // Airflow start - Inventory Events
+ me.glicz.airflow.api.event.inventory.menu.MenuOpenEvent event = getServer().getDedicatedServer().airflow.serverEventBus
+ .dispatchMenuOpen(this, abstractContainerMenu);
+ if (event.isCancelled()) return OptionalInt.empty();
+ // Airflow end - Inventory Events
abstractContainerMenu.getAirMenuView().openScreen();
/*
this.connection
@@ -1138,6 +1143,7 @@ public class ServerPlayer extends Player {

@Override
public void doCloseContainer() {
+ getServer().getDedicatedServer().airflow.serverEventBus.dispatchMenuClose(this, this.containerMenu); // Airflow start - Inventory Events
this.containerMenu.removed(this);
this.inventoryMenu.transferState(this.containerMenu);
this.containerMenu = this.inventoryMenu;
diff --git a/net/minecraft/world/inventory/AbstractContainerMenu.java b/net/minecraft/world/inventory/AbstractContainerMenu.java
index 7522c722e65db2e97b926b3e74291512f2d23954..796bac3ac2dbc6caae23b523687321519a17e19a 100644
--- a/net/minecraft/world/inventory/AbstractContainerMenu.java
+++ b/net/minecraft/world/inventory/AbstractContainerMenu.java
@@ -296,6 +296,11 @@ public abstract class AbstractContainerMenu {
public abstract ItemStack quickMoveStack(Player player, int index);

public void clicked(int slotId, int button, ClickType clickType, Player player) {
+ // Airflow start - Inventory Events
+ me.glicz.airflow.api.event.inventory.InventoryClickEvent event = player.getServer().getDedicatedServer().airflow.serverEventBus
+ .dispatchInventoryClick(player, this, slotId);
+ if (event.isCancelled()) return;
+ // Airflow end - Inventory Events
try {
this.doClick(slotId, button, clickType, player);
} catch (Exception var8) {
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@
import me.glicz.airflow.api.event.EventPriority;
import me.glicz.airflow.api.event.bus.ServerEventBus;
import me.glicz.airflow.api.event.command.CommandsRegisterEvent;
import me.glicz.airflow.api.event.inventory.InventoryClickEvent;
import me.glicz.airflow.api.event.inventory.menu.MenuCloseEvent;
import me.glicz.airflow.api.event.inventory.menu.MenuOpenEvent;
import me.glicz.airflow.api.event.packet.ItemStackDecodeEvent;
import me.glicz.airflow.api.event.packet.ItemStackEncodeEvent;
import me.glicz.airflow.api.event.player.PlayerJoinEvent;
import me.glicz.airflow.api.event.player.PlayerQuitEvent;
import me.glicz.airflow.api.inventory.Inventory;
import me.glicz.airflow.inventory.menu.view.AirMenuView;
import me.glicz.airflow.util.MinecraftComponentSerializer;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

Expand Down Expand Up @@ -45,6 +52,22 @@ public void dispatchCommandsRegister(Commands commands) {
dispatch(new CommandsRegisterEvent(commands));
}

public void dispatchMenuClose(Player player, AbstractContainerMenu containerMenu) {
dispatch(new MenuCloseEvent(player.getAirEntity(), containerMenu.getAirMenuView()));
}

public MenuOpenEvent dispatchMenuOpen(Player player, AbstractContainerMenu containerMenu) {
return dispatch(new MenuOpenEvent(player.getAirEntity(), containerMenu.getAirMenuView()));
}

public InventoryClickEvent dispatchInventoryClick(Player player, AbstractContainerMenu containerMenu, int slot) {
AirMenuView menuView = containerMenu.getAirMenuView();
Inventory inventory = slot >= 0 && slot < menuView.getComposedInventory().getSize() + menuView.getViewerInventory().getSize()
? menuView.getInventoryForSlot(slot)
: null;
return dispatch(new InventoryClickEvent(player.getAirEntity(), menuView, inventory, slot));
}

public ItemStackDecodeEvent dispatchItemStackDecode(ItemStack itemStack) {
return dispatch(new ItemStackDecodeEvent(itemStack.airItemStack));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public int getSize() {

@Override
public @NotNull ItemStack getItem(int slot) {
Preconditions.checkArgument(slot < getSize(), "slot >= inventory size");
Preconditions.checkArgument(slot >= 0 && slot < getSize(), "slot < 0 || slot >= inventory size");

Iterator<AirInventory> it = this.inventories.iterator();
int i = 0;
Expand Down Expand Up @@ -99,7 +99,26 @@ public void clear() {
}

@Override
public Collection<Inventory> getInventories() {
public @NotNull Collection<Inventory> getInventories() {
return this.inventories.stream().<Inventory>map(Function.identity()).toList();
}

@Override
public @NotNull Inventory getInventoryForSlot(int slot) {
Preconditions.checkArgument(slot >= 0 && slot < getSize(), "slot < 0 || slot >= inventory size");

Iterator<AirInventory> it = this.inventories.iterator();
int i = 0;

while (it.hasNext()) {
AirInventory inventory = it.next();
if (i <= slot && i + inventory.getSize() > slot) {
return inventory;
}

i += inventory.getSize();
}

throw new RuntimeException(); // shouldn't happen?
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ResultContainer;
import org.jetbrains.annotations.NotNull;

public class AirItemCombinerView extends AirMenuView implements ItemCombinerView {
private final AirSimpleInventory resultInventory;
Expand All @@ -23,7 +24,7 @@ protected AirComposedInventory createComposedInventory() {
}

@Override
public Inventory getResultInventory() {
public @NotNull Inventory getResultInventory() {
return this.resultInventory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import org.jetbrains.annotations.NotNull;

public class AirLoomView extends AirMenuView implements LoomView {
private final AirSimpleInventory outputContainer;
Expand All @@ -22,7 +23,7 @@ protected AirComposedInventory createComposedInventory() {
}

@Override
public Inventory getOutputInventory() {
public @NotNull Inventory getOutputInventory() {
return this.outputContainer;
}
}
Loading

0 comments on commit afa0e11

Please sign in to comment.