-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dddf091
commit f7755ae
Showing
10 changed files
with
782 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<groupId>xyz.xenondevs.invui</groupId> | ||
<artifactId>invui-parent</artifactId> | ||
<version>1.34</version> | ||
<relativePath>../../pom.xml</relativePath> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>inventory-access-r21</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>21</maven.compiler.source> | ||
<maven.compiler.target>21</maven.compiler.target> | ||
<minecraft.version>1.21.1</minecraft.version> | ||
<spigot.version>${minecraft.version}-R0.1-SNAPSHOT</spigot.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot</artifactId> | ||
<version>${spigot.version}</version> | ||
<classifier>remapped-mojang</classifier> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>xyz.xenondevs.invui</groupId> | ||
<artifactId>inventory-access</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>xyz.xenondevs.string-remapper</groupId> | ||
<artifactId>string-remapper-maven-plugin</artifactId> | ||
<version>1.9</version> | ||
|
||
<executions> | ||
<execution> | ||
<id>remap-spigot</id> | ||
<goals> | ||
<goal>remap</goal> | ||
</goals> | ||
<configuration> | ||
<version>${minecraft.version}</version> | ||
<goal>spigot</goal> | ||
<classesIn>${project.build.directory}/classes</classesIn> | ||
<classesOut>${project.build.directory}/classes-spigot</classesOut> | ||
</configuration> | ||
</execution> | ||
|
||
<execution> | ||
<id>remap-mojang</id> | ||
<goals> | ||
<goal>remap</goal> | ||
</goals> | ||
<configuration> | ||
<version>${minecraft.version}</version> | ||
<goal>mojang</goal> | ||
<classesIn>${project.build.directory}/classes</classesIn> | ||
<classesOut>${project.build.directory}/classes-mojang</classesOut> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-jar-plugin</artifactId> | ||
<version>3.2.2</version> | ||
<executions> | ||
<execution> | ||
<id>default-jar</id> | ||
<phase>none</phase> | ||
</execution> | ||
<execution> | ||
<id>spigot</id> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
<configuration> | ||
<classesDirectory>${project.build.directory}/classes-spigot</classesDirectory> | ||
<archive> | ||
<manifestEntries> | ||
<paperweight-mappings-namespace>spigot</paperweight-mappings-namespace> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<id>mojang</id> | ||
<goals> | ||
<goal>jar</goal> | ||
</goals> | ||
<configuration> | ||
<classesDirectory>${project.build.directory}/classes-mojang</classesDirectory> | ||
<classifier>remapped-mojang</classifier> | ||
<archive> | ||
<manifestEntries> | ||
<paperweight-mappings-namespace>mojang</paperweight-mappings-namespace> | ||
</manifestEntries> | ||
</archive> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
|
||
<plugin> | ||
<groupId>net.md-5</groupId> | ||
<artifactId>specialsource-maven-plugin</artifactId> | ||
<version>2.0.2</version> | ||
<executions> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>remap</goal> | ||
</goals> | ||
<id>remap-obf</id> | ||
<configuration> | ||
<srgIn>org.spigotmc:minecraft-server:${spigot.version}:txt:maps-mojang</srgIn> | ||
<reverse>true</reverse> | ||
<remappedDependencies>org.spigotmc:spigot:${spigot.version}:jar:remapped-mojang</remappedDependencies> | ||
<remappedArtifactAttached>false</remappedArtifactAttached> | ||
<finalName>${project.artifactId}-${project.version}-remapped-obf</finalName> | ||
</configuration> | ||
</execution> | ||
<execution> | ||
<phase>package</phase> | ||
<goals> | ||
<goal>remap</goal> | ||
</goals> | ||
<id>remap-spigot</id> | ||
<configuration> | ||
<inputFile>${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar</inputFile> | ||
<srgIn>org.spigotmc:minecraft-server:${spigot.version}:csrg:maps-spigot</srgIn> | ||
<remappedDependencies>org.spigotmc:spigot:${spigot.version}:jar:remapped-obf</remappedDependencies> | ||
</configuration> | ||
</execution> | ||
</executions> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
188 changes: 188 additions & 0 deletions
188
...entory-access-r21/src/main/java/xyz/xenondevs/inventoryaccess/r21/AnvilInventoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
package xyz.xenondevs.inventoryaccess.r21; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.NonNullList; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket; | ||
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; | ||
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.Container; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.inventory.AbstractContainerMenu; | ||
import net.minecraft.world.inventory.AnvilMenu; | ||
import net.minecraft.world.inventory.ContainerLevelAccess; | ||
import net.minecraft.world.inventory.MenuType; | ||
import net.minecraft.world.item.ItemStack; | ||
import org.bukkit.craftbukkit.v1_21_R1.entity.CraftPlayer; | ||
import org.bukkit.craftbukkit.v1_21_R1.event.CraftEventFactory; | ||
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftInventoryAnvil; | ||
import org.bukkit.craftbukkit.v1_21_R1.inventory.CraftItemStack; | ||
import org.bukkit.craftbukkit.v1_21_R1.inventory.view.CraftAnvilView; | ||
import org.bukkit.event.inventory.PrepareAnvilEvent; | ||
import org.bukkit.inventory.Inventory; | ||
import org.jetbrains.annotations.NotNull; | ||
import xyz.xenondevs.inventoryaccess.abstraction.inventory.AnvilInventory; | ||
import xyz.xenondevs.inventoryaccess.component.ComponentWrapper; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
class AnvilInventoryImpl extends AnvilMenu implements AnvilInventory { | ||
|
||
private final List<Consumer<String>> renameHandlers; | ||
private final CraftAnvilView view; | ||
private final ServerPlayer player; | ||
|
||
private String text; | ||
private boolean open; | ||
|
||
public AnvilInventoryImpl(org.bukkit.entity.Player player, @NotNull ComponentWrapper title, List<Consumer<String>> renameHandlers) { | ||
this(((CraftPlayer) player).getHandle(), InventoryUtilsImpl.createNMSComponent(title), renameHandlers); | ||
} | ||
|
||
public AnvilInventoryImpl(ServerPlayer player, Component title, List<Consumer<String>> renameHandlers) { | ||
super(player.nextContainerCounter(), player.getInventory(), | ||
ContainerLevelAccess.create(player.level(), new BlockPos(0, 0, 0))); | ||
|
||
setTitle(title); | ||
this.renameHandlers = renameHandlers; | ||
this.player = player; | ||
|
||
CraftInventoryAnvil inventory = new CraftInventoryAnvil(access.getLocation(), inputSlots, resultSlots); | ||
this.view = new CraftAnvilView(player.getBukkitEntity(), inventory, this); | ||
} | ||
|
||
public void open() { | ||
open = true; | ||
|
||
// call the InventoryOpenEvent | ||
CraftEventFactory.callInventoryOpenEvent(player, this); | ||
|
||
// set active container | ||
player.containerMenu = this; | ||
|
||
// send open packet | ||
player.connection.send(new ClientboundOpenScreenPacket(containerId, MenuType.ANVIL, getTitle())); | ||
|
||
// send initial items | ||
NonNullList<ItemStack> itemsList = NonNullList.of(ItemStack.EMPTY, getItem(0), getItem(1), getItem(2)); | ||
player.connection.send(new ClientboundContainerSetContentPacket(getActiveWindowId(player), incrementStateId(), itemsList, ItemStack.EMPTY)); | ||
|
||
// init menu | ||
player.initMenu(this); | ||
} | ||
|
||
public void sendItem(int slot) { | ||
player.connection.send(new ClientboundContainerSetSlotPacket(getActiveWindowId(player), incrementStateId(), slot, getItem(slot))); | ||
} | ||
|
||
public void setItem(int slot, ItemStack item) { | ||
if (slot < 2) inputSlots.setItem(slot, item); | ||
else resultSlots.setItem(0, item); | ||
|
||
if (open) sendItem(slot); | ||
} | ||
|
||
private ItemStack getItem(int slot) { | ||
if (slot < 2) return inputSlots.getItem(slot); | ||
else return resultSlots.getItem(0); | ||
} | ||
|
||
private int getActiveWindowId(ServerPlayer player) { | ||
AbstractContainerMenu container = player.containerMenu; | ||
return container == null ? -1 : container.containerId; | ||
} | ||
|
||
@Override | ||
public void setItem(int slot, org.bukkit.inventory.ItemStack itemStack) { | ||
setItem(slot, CraftItemStack.asNMSCopy(itemStack)); | ||
} | ||
|
||
@Override | ||
public @NotNull Inventory getBukkitInventory() { | ||
return view.getTopInventory(); | ||
} | ||
|
||
@Override | ||
public String getRenameText() { | ||
return text; | ||
} | ||
|
||
@Override | ||
public boolean isOpen() { | ||
return open; | ||
} | ||
|
||
// --- AnvilMenu --- | ||
|
||
@Override | ||
public CraftAnvilView getBukkitView() { | ||
return view; | ||
} | ||
|
||
/** | ||
* Called every tick to see if the {@link Player} can still use that container. | ||
* (Used to for checking the distance between the {@link Player} and the container | ||
* and closing the window when the distance gets too big.) | ||
* | ||
* @param player The {@link Player} | ||
* @return If the {@link Player} can still use that container | ||
*/ | ||
@Override | ||
public boolean stillValid(Player player) { | ||
return true; | ||
} | ||
|
||
/** | ||
* Called when the rename text gets changed. | ||
* | ||
* @param s The new rename text | ||
*/ | ||
@Override | ||
public boolean setItemName(String s) { | ||
// save rename text | ||
text = s; | ||
|
||
// call rename handlers | ||
if (renameHandlers != null) | ||
renameHandlers.forEach(handler -> handler.accept(s)); | ||
|
||
// the client expects the item to change to its new name and removes it from the inventory, so it needs to be sent again | ||
sendItem(2); | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Called when the container is closed to give the items back. | ||
* | ||
* @param player The {@link Player} that closed this container | ||
*/ | ||
@Override | ||
public void removed(Player player) { | ||
open = false; | ||
} | ||
|
||
/** | ||
* Called when the container gets closed to put items back into a players | ||
* inventory or drop them in the world. | ||
* | ||
* @param player The {@link Player} that closed this container | ||
* @param container The container | ||
*/ | ||
@Override | ||
protected void clearContainer(Player player, Container container) { | ||
open = false; | ||
} | ||
|
||
/** | ||
* Called when both items in the {@link AnvilMenu#inputSlots} were set to create | ||
* the resulting product, calculate the level cost and call the {@link PrepareAnvilEvent}. | ||
*/ | ||
@Override | ||
public void createResult() { | ||
// empty | ||
} | ||
|
||
} |
Oops, something went wrong.