#1176: Add InventoryView#setTitle

By: Y2Kwastaken <mwholder2005@gmail.com>
This commit is contained in:
CraftBukkit/Spigot 2023-05-04 18:52:18 +10:00
parent ba817be945
commit dafbbb0363
2 changed files with 51 additions and 2 deletions

View file

@ -49,6 +49,10 @@ public class CraftContainer extends Container {
public CraftContainer(final Inventory inventory, final EntityHuman player, int id) {
this(new InventoryView() {
private final String originalTitle = (inventory instanceof CraftInventoryCustom) ? ((CraftInventoryCustom.MinecraftInventory) ((CraftInventory) inventory).getInventory()).getTitle() : inventory.getType().getDefaultTitle();
private String title = originalTitle;
@Override
public Inventory getTopInventory() {
return inventory;
@ -71,8 +75,20 @@ public class CraftContainer extends Container {
@Override
public String getTitle() {
return inventory instanceof CraftInventoryCustom ? ((CraftInventoryCustom.MinecraftInventory) ((CraftInventory) inventory).getInventory()).getTitle() : inventory.getType().getDefaultTitle();
return title;
}
@Override
public String getOriginalTitle() {
return originalTitle;
}
@Override
public void setTitle(String title) {
CraftInventoryView.sendInventoryTitleChange(this, title);
this.title = title;
}
}, player, id);
}

View file

@ -1,10 +1,15 @@
package org.bukkit.craftbukkit.inventory;
import com.google.common.base.Preconditions;
import net.minecraft.network.protocol.game.PacketPlayOutOpenWindow;
import net.minecraft.server.level.EntityPlayer;
import net.minecraft.world.inventory.Container;
import net.minecraft.world.inventory.Containers;
import org.bukkit.GameMode;
import org.bukkit.craftbukkit.entity.CraftHumanEntity;
import org.bukkit.craftbukkit.util.CraftChatMessage;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
@ -14,12 +19,16 @@ public class CraftInventoryView extends InventoryView {
private final Container container;
private final CraftHumanEntity player;
private final CraftInventory viewing;
private final String originalTitle;
private String title;
public CraftInventoryView(HumanEntity player, Inventory viewing, Container container) {
// TODO: Should we make sure it really IS a CraftHumanEntity first? And a CraftInventory?
this.player = (CraftHumanEntity) player;
this.viewing = (CraftInventory) viewing;
this.container = container;
this.originalTitle = CraftChatMessage.fromComponent(container.getTitle());
this.title = originalTitle;
}
@Override
@ -66,7 +75,18 @@ public class CraftInventoryView extends InventoryView {
@Override
public String getTitle() {
return CraftChatMessage.fromComponent(container.getTitle());
return title;
}
@Override
public String getOriginalTitle() {
return originalTitle;
}
@Override
public void setTitle(String title) {
sendInventoryTitleChange(this, title);
this.title = title;
}
public boolean isInTop(int rawSlot) {
@ -76,4 +96,17 @@ public class CraftInventoryView extends InventoryView {
public Container getHandle() {
return container;
}
public static void sendInventoryTitleChange(InventoryView view, String title) {
Preconditions.checkArgument(view != null, "InventoryView cannot be null");
Preconditions.checkArgument(title != null, "Title cannot be null");
Preconditions.checkArgument(view.getPlayer() instanceof Player, "NPCs are not currently supported for this function");
Preconditions.checkArgument(view.getTopInventory().getType().isCreatable(), "Only creatable inventories can have their title changed");
final EntityPlayer entityPlayer = (EntityPlayer) ((CraftHumanEntity) view.getPlayer()).getHandle();
final int containerId = entityPlayer.containerMenu.containerId;
final Containers<?> windowType = CraftContainer.getNotchInventoryType(view.getTopInventory());
entityPlayer.connection.send(new PacketPlayOutOpenWindow(containerId, windowType, CraftChatMessage.fromString(title)[0]));
((Player) view.getPlayer()).updateInventory();
}
}