Skip to content

Commit

Permalink
Fix #1 (run open inventory again in another tick in reopen)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndyNoob committed Aug 4, 2024
1 parent d76fa79 commit 1cfdf46
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public abstract class TextFieldComponent extends StaticComponent {

protected final Map<HumanEntity, Inventory> openAnvils = new HashMap<>();
protected final Map<HumanEntity, Inventory> dontReopen = new HashMap<>();
protected final JavaPlugin plugin;
private boolean isRemoved = false;
private final RegisteredListener onPrepare;
private final RegisteredListener onClick;
private final RegisteredListener onClose;

public TextFieldComponent(@NotNull JavaPlugin plugin, @NotNull Vector2i position) {
super(position);
this.plugin = plugin;
final AtomicReference<ItemStack> resultReference = new AtomicReference<>(null);
this.onPrepare = makeListener(plugin, (l, e) -> handleRenaming((PrepareAnvilEvent) e, resultReference));
this.onClick = makeListener(plugin, (l, e) -> handleFinish((InventoryClickEvent) e, resultReference));
Expand Down
11 changes: 11 additions & 0 deletions testPlugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ plugins {
group = 'comfortable_andy'
version = '1.0.0'

repositories {
maven {
name = 'dmulloy2-repo'
url = 'https://repo.dmulloy2.net/repository/public/'
}
}

dependencies {
compileOnly 'com.comphenix.protocol:ProtocolLib:5.1.0'
implementation rootProject.project(':menu')
}

apply from: rootProject.file('buildSrc/shared.gradle')

runServer {
jvmArgs('-Xms1G', '-Xmx1G')
downloadPlugins {
url 'https://ci.dmulloy2.net/job/ProtocolLib/lastSuccessfulBuild/artifact/build/libs/ProtocolLib.jar'
}
}

shadowJar {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package comfortable_andy.brew.test_plugin;

import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent;
import com.mojang.brigadier.Command;
import com.comphenix.protocol.ProtocolLibrary;
import comfortable_andy.brew.menu.Menu;
import comfortable_andy.brew.menu.componenets.Renderer;
import comfortable_andy.brew.test_plugin.components.SimpleTextFieldComponent;
Expand Down Expand Up @@ -34,6 +39,10 @@ public class BrewTestMain extends JavaPlugin implements Listener {
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
registerCommands();
if (getServer().getPluginManager().isPluginEnabled("ProtocolLib")) {
registerProtocolLib();
getLogger().info("Packet listener enabled.");
} else getLogger().info("Packet listener not enabled.");
}

@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -71,6 +80,15 @@ private void registerCommands() {
});
}

private void registerProtocolLib() {
ProtocolManager manager = ProtocolLibrary.getProtocolManager();
manager.addPacketListener(new PacketAdapter(this, PacketType.Play.Client.WINDOW_CLICK) {
@Override
public void onPacketReceiving(PacketEvent event) {
}
});
}

@EventHandler
public void onClick(InventoryClickEvent event) {
Menu menu = menus.get(event.getWhoClicked());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@ public class SimpleTextFieldComponent extends TextFieldComponent {

@Setter
private ItemStack item;
private final JavaPlugin plugin;
private final BiConsumer<HumanEntity, String> callback;
private final Map<HumanEntity, Inventory> reopens = new HashMap<>();

public SimpleTextFieldComponent(@NotNull JavaPlugin plugin, @NotNull Vector2i position, ItemStack item, BiConsumer<HumanEntity, String> callback) {
super(plugin, position);
this.plugin = plugin;
this.callback = callback;
this.item = item;
getCollisionTable().set(0, 0);
Expand All @@ -51,7 +49,8 @@ protected void onEnterText(HumanEntity entity, String str) {
protected void reopenOriginal(HumanEntity entity) {
Inventory inventory = reopens.remove(entity);
if (inventory != null) {
Bukkit.getScheduler().runTaskLater(plugin, () -> entity.openInventory(inventory), 1);
entity.openInventory(inventory);
Bukkit.getScheduler().runTaskLater(plugin, () -> entity.openInventory(inventory), 0);
}
}
}
4 changes: 3 additions & 1 deletion testPlugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
name: brew-test
main: comfortable_andy.brew.test_plugin.BrewTestMain
version: -100
version: -100
softdepend:
- ProtocolLib

0 comments on commit 1cfdf46

Please sign in to comment.