Skip to content

Commit 7cf948a

Browse files
1.21.4 Support
1 parent b396f34 commit 7cf948a

File tree

10 files changed

+789
-1
lines changed

10 files changed

+789
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# InvUI
88

99
An Inventory API for Minecraft Spigot servers.
10-
Supports all versions from 1.14.0 to 1.21.1.
10+
Supports all versions from 1.14.0 to 1.21.4.
1111

1212
[Documentation](https://xenondevs.xyz/docs/invui/)
1313

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<groupId>xyz.xenondevs.invui</groupId>
7+
<artifactId>invui-parent</artifactId>
8+
<version>1.43</version>
9+
<relativePath>../../pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<artifactId>inventory-access-r22</artifactId>
14+
15+
<properties>
16+
<maven.compiler.source>21</maven.compiler.source>
17+
<maven.compiler.target>21</maven.compiler.target>
18+
<minecraft.version>1.21.4</minecraft.version>
19+
<spigot.version>${minecraft.version}-R0.1-SNAPSHOT</spigot.version>
20+
</properties>
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.spigotmc</groupId>
25+
<artifactId>spigot</artifactId>
26+
<version>${spigot.version}</version>
27+
<classifier>remapped-mojang</classifier>
28+
<scope>provided</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>xyz.xenondevs.invui</groupId>
32+
<artifactId>inventory-access</artifactId>
33+
<version>${project.parent.version}</version>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<plugins>
39+
<plugin>
40+
<groupId>xyz.xenondevs.string-remapper</groupId>
41+
<artifactId>string-remapper-maven-plugin</artifactId>
42+
<version>1.9</version>
43+
44+
<executions>
45+
<execution>
46+
<id>remap-spigot</id>
47+
<goals>
48+
<goal>remap</goal>
49+
</goals>
50+
<configuration>
51+
<version>${minecraft.version}</version>
52+
<goal>spigot</goal>
53+
<classesIn>${project.build.directory}/classes</classesIn>
54+
<classesOut>${project.build.directory}/classes-spigot</classesOut>
55+
</configuration>
56+
</execution>
57+
58+
<execution>
59+
<id>remap-mojang</id>
60+
<goals>
61+
<goal>remap</goal>
62+
</goals>
63+
<configuration>
64+
<version>${minecraft.version}</version>
65+
<goal>mojang</goal>
66+
<classesIn>${project.build.directory}/classes</classesIn>
67+
<classesOut>${project.build.directory}/classes-mojang</classesOut>
68+
</configuration>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
73+
<plugin>
74+
<groupId>org.apache.maven.plugins</groupId>
75+
<artifactId>maven-jar-plugin</artifactId>
76+
<version>3.2.2</version>
77+
<executions>
78+
<execution>
79+
<id>default-jar</id>
80+
<phase>none</phase>
81+
</execution>
82+
<execution>
83+
<id>spigot</id>
84+
<goals>
85+
<goal>jar</goal>
86+
</goals>
87+
<configuration>
88+
<classesDirectory>${project.build.directory}/classes-spigot</classesDirectory>
89+
<archive>
90+
<manifestEntries>
91+
<paperweight-mappings-namespace>spigot</paperweight-mappings-namespace>
92+
</manifestEntries>
93+
</archive>
94+
</configuration>
95+
</execution>
96+
<execution>
97+
<id>mojang</id>
98+
<goals>
99+
<goal>jar</goal>
100+
</goals>
101+
<configuration>
102+
<classesDirectory>${project.build.directory}/classes-mojang</classesDirectory>
103+
<classifier>remapped-mojang</classifier>
104+
<archive>
105+
<manifestEntries>
106+
<paperweight-mappings-namespace>mojang</paperweight-mappings-namespace>
107+
</manifestEntries>
108+
</archive>
109+
</configuration>
110+
</execution>
111+
</executions>
112+
</plugin>
113+
114+
<plugin>
115+
<groupId>net.md-5</groupId>
116+
<artifactId>specialsource-maven-plugin</artifactId>
117+
<version>2.0.2</version>
118+
<executions>
119+
<execution>
120+
<phase>package</phase>
121+
<goals>
122+
<goal>remap</goal>
123+
</goals>
124+
<id>remap-obf</id>
125+
<configuration>
126+
<srgIn>org.spigotmc:minecraft-server:${spigot.version}:txt:maps-mojang</srgIn>
127+
<reverse>true</reverse>
128+
<remappedDependencies>org.spigotmc:spigot:${spigot.version}:jar:remapped-mojang</remappedDependencies>
129+
<remappedArtifactAttached>false</remappedArtifactAttached>
130+
<finalName>${project.artifactId}-${project.version}-remapped-obf</finalName>
131+
</configuration>
132+
</execution>
133+
<execution>
134+
<phase>package</phase>
135+
<goals>
136+
<goal>remap</goal>
137+
</goals>
138+
<id>remap-spigot</id>
139+
<configuration>
140+
<inputFile>${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar</inputFile>
141+
<srgIn>org.spigotmc:minecraft-server:${spigot.version}:csrg:maps-spigot</srgIn>
142+
<remappedDependencies>org.spigotmc:spigot:${spigot.version}:jar:remapped-obf</remappedDependencies>
143+
</configuration>
144+
</execution>
145+
</executions>
146+
</plugin>
147+
</plugins>
148+
</build>
149+
150+
</project>
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package xyz.xenondevs.inventoryaccess.r22;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.core.NonNullList;
5+
import net.minecraft.network.chat.Component;
6+
import net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket;
7+
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
8+
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket;
9+
import net.minecraft.server.level.ServerPlayer;
10+
import net.minecraft.world.Container;
11+
import net.minecraft.world.entity.player.Player;
12+
import net.minecraft.world.inventory.AbstractContainerMenu;
13+
import net.minecraft.world.inventory.AnvilMenu;
14+
import net.minecraft.world.inventory.ContainerLevelAccess;
15+
import net.minecraft.world.inventory.MenuType;
16+
import net.minecraft.world.item.ItemStack;
17+
import org.bukkit.craftbukkit.v1_21_R3.entity.CraftPlayer;
18+
import org.bukkit.craftbukkit.v1_21_R3.event.CraftEventFactory;
19+
import org.bukkit.craftbukkit.v1_21_R3.inventory.CraftInventoryAnvil;
20+
import org.bukkit.craftbukkit.v1_21_R3.inventory.CraftItemStack;
21+
import org.bukkit.craftbukkit.v1_21_R3.inventory.view.CraftAnvilView;
22+
import org.bukkit.event.inventory.PrepareAnvilEvent;
23+
import org.bukkit.inventory.Inventory;
24+
import org.jetbrains.annotations.NotNull;
25+
import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory;
26+
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper;
27+
28+
import java.util.List;
29+
import java.util.function.Consumer;
30+
31+
class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory {
32+
33+
private final List<Consumer<String>> renameHandlers;
34+
private final CraftAnvilView view;
35+
private final ServerPlayer player;
36+
37+
private String text;
38+
private boolean open;
39+
40+
public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List<Consumer<String>> renameHandlers) {
41+
this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers);
42+
}
43+
44+
public AnvilInventoryImpl(ServerPlayer player, Component title, List<Consumer<String>> renameHandlers) {
45+
super(player.nextContainerCounter(), player.getInventory(),
46+
ContainerLevelAccess.create(player.level(), new BlockPos(0, 0, 0)));
47+
48+
setTitle(title);
49+
this.renameHandlers = renameHandlers;
50+
this.player = player;
51+
52+
CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), inputSlots, resultSlots);
53+
this.view = new CraftAnvilView(player.getBukkitEntity(), inventory, this);
54+
}
55+
56+
public void open() {
57+
open = true;
58+
59+
// call the InventoryOpenEvent
60+
CraftEventFactory.callInventoryOpenEvent(player, this);
61+
62+
// set active container
63+
player.containerMenu = this;
64+
65+
// send open packet
66+
player.connection.send(new ClientboundOpenScreenPacket(containerId, MenuType.ANVIL, getTitle()));
67+
68+
// send initial items
69+
NonNullList<ItemStack> itemsList = NonNullList.of(ItemStack.EMPTY, getItem(0), getItem(1), getItem(2));
70+
player.connection.send(new ClientboundContainerSetContentPacket(getActiveWindowId(player), incrementStateId(), itemsList, ItemStack.EMPTY));
71+
72+
// init menu
73+
player.initMenu(this);
74+
}
75+
76+
public void sendItem(int slot) {
77+
player.connection.send(new ClientboundContainerSetSlotPacket(getActiveWindowId(player), incrementStateId(), slot, getItem(slot)));
78+
}
79+
80+
public void setItem(int slot, ItemStack item) {
81+
if (slot < 2) inputSlots.setItem(slot, item);
82+
else resultSlots.setItem(0, item);
83+
84+
if (open) sendItem(slot);
85+
}
86+
87+
private ItemStack getItem(int slot) {
88+
if (slot < 2) return inputSlots.getItem(slot);
89+
else return resultSlots.getItem(0);
90+
}
91+
92+
private int getActiveWindowId(ServerPlayer player) {
93+
AbstractContainerMenu container = player.containerMenu;
94+
return container == null ? -1 : container.containerId;
95+
}
96+
97+
@Override
98+
public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) {
99+
setItem(slot, CraftItemStack.asNMSCopy(itemStack));
100+
}
101+
102+
@Override
103+
public @NotNull Inventory getBukkitInventory() {
104+
return view.getTopInventory();
105+
}
106+
107+
@Override
108+
public String getRenameText() {
109+
return text;
110+
}
111+
112+
@Override
113+
public boolean isOpen() {
114+
return open;
115+
}
116+
117+
// --- AnvilMenu ---
118+
119+
@Override
120+
public CraftAnvilView getBukkitView() {
121+
return view;
122+
}
123+
124+
/**
125+
* Called every tick to see if the {@link Player} can still use that container.
126+
* (Used to for checking the distance between the {@link Player} and the container
127+
* and closing the window when the distance gets too big.)
128+
*
129+
* @param player The {@link Player}
130+
* @return If the {@link Player} can still use that container
131+
*/
132+
@Override
133+
public boolean stillValid(Player player) {
134+
return true;
135+
}
136+
137+
/**
138+
* Called when the rename text gets changed.
139+
*
140+
* @param s The new rename text
141+
*/
142+
@Override
143+
public boolean setItemName(String s) {
144+
// save rename text
145+
text = s;
146+
147+
// call rename handlers
148+
if (renameHandlers != null)
149+
renameHandlers.forEach(handler -> handler.accept(s));
150+
151+
// the client expects the item to change to its new name and removes it from the inventory, so it needs to be sent again
152+
sendItem(2);
153+
154+
return false;
155+
}
156+
157+
/**
158+
* Called when the container is closed to give the items back.
159+
*
160+
* @param player The {@link Player} that closed this container
161+
*/
162+
@Override
163+
public void removed(Player player) {
164+
open = false;
165+
}
166+
167+
/**
168+
* Called when the container gets closed to put items back into a players
169+
* inventory or drop them in the world.
170+
*
171+
* @param player The {@link Player} that closed this container
172+
* @param container The container
173+
*/
174+
@Override
175+
protected void clearContainer(Player player, Container container) {
176+
open = false;
177+
}
178+
179+
/**
180+
* Called when both items in the {@link AnvilMenu#inputSlots} were set to create
181+
* the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}.
182+
*/
183+
@Override
184+
public void createResult() {
185+
// empty
186+
}
187+
188+
}

0 commit comments

Comments
 (0)