Skip to content

Commit 9f15929

Browse files
CartographyWindow view mode and refactoring
- Moved MapIcon, MapPatch into CartographyWindow - Property setter for cartography window icons (removed addIcon, removeIcon methods) - Added CartographyWindow view mode
1 parent d15b415 commit 9f15929

7 files changed

Lines changed: 335 additions & 221 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package xyz.xenondevs.invui.window
2+
3+
import xyz.xenondevs.commons.provider.MutableProvider
4+
import xyz.xenondevs.invui.ExperimentalReactiveApi
5+
import xyz.xenondevs.invui.MutablePropertyAdapter
6+
7+
@ExperimentalReactiveApi
8+
fun CartographyWindow.Builder.setIcons(icons: MutableProvider<Set<CartographyWindow.MapIcon>>): CartographyWindow.Builder =
9+
setIcons(MutablePropertyAdapter(icons))
10+
11+
@ExperimentalReactiveApi
12+
fun CartographyWindow.Builder.setView(view: MutableProvider<CartographyWindow.View>): CartographyWindow.Builder =
13+
setView(MutablePropertyAdapter(view))

invui/src/main/java/xyz/xenondevs/invui/internal/menu/CustomCartographyMenu.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77
import net.minecraft.network.protocol.game.ClientboundMapItemDataPacket;
88
import net.minecraft.world.inventory.MenuType;
99
import net.minecraft.world.level.saveddata.maps.*;
10+
import org.bukkit.Material;
1011
import org.bukkit.craftbukkit.inventory.CraftItemStack;
1112
import org.bukkit.entity.Player;
1213
import org.bukkit.inventory.ItemStack;
1314
import org.jspecify.annotations.Nullable;
1415
import xyz.xenondevs.invui.internal.network.PacketListener;
16+
import xyz.xenondevs.invui.internal.util.ItemUtils2;
1517
import xyz.xenondevs.invui.internal.util.MathUtils;
16-
import xyz.xenondevs.invui.util.MapIcon;
17-
import xyz.xenondevs.invui.util.MapPatch;
18+
import xyz.xenondevs.invui.window.CartographyWindow;
19+
import xyz.xenondevs.invui.window.CartographyWindow.MapIcon;
20+
import xyz.xenondevs.invui.window.CartographyWindow.MapPatch;
1821

1922
import java.util.Collection;
2023
import java.util.HashSet;
@@ -32,6 +35,7 @@ public class CustomCartographyMenu extends CustomContainerMenu {
3235
private int mapId = -MathUtils.RANDOM.nextInt(Integer.MAX_VALUE);
3336
private byte[] canvas = new byte[MAP_SIZE * MAP_SIZE];
3437
private final Set<MapDecoration> decorations = new HashSet<>();
38+
private CartographyWindow.View view = CartographyWindow.View.NORMAL;
3539

3640
/**
3741
* Creates a new {@link CustomCartographyMenu} for the specified viewer.
@@ -49,6 +53,14 @@ public void setItem(int slot, @Nullable ItemStack item) {
4953
var clone = item.clone();
5054
clone.setData(DataComponentTypes.MAP_ID, mapId(mapId));
5155
super.setItem(slot, clone);
56+
} else if (slot == 1 && item != null) {
57+
var targetType = switch (view) {
58+
case NORMAL -> Material.STONE;
59+
case SMALL -> Material.PAPER;
60+
case DUPLICATE -> Material.MAP;
61+
case LOCK -> Material.GLASS_PANE;
62+
};
63+
super.setItem(slot, ItemUtils2.asType(item, targetType));
5264
} else {
5365
super.setItem(slot, item);
5466
}
@@ -60,20 +72,9 @@ public void open(Component title) {
6072
sendMapUpdate(new MapItemSavedData.MapPatch(0, 0, MAP_SIZE, MAP_SIZE, canvas), decorations);
6173
}
6274

63-
public void addIcon(MapIcon icon, boolean sendUpdate) {
64-
var decoration = toNmsDecoration(icon);
65-
decorations.add(decoration);
66-
67-
if (sendUpdate)
68-
sendMapUpdate(null, decorations);
69-
}
70-
71-
public void removeIcon(MapIcon icon, boolean sendUpdate) {
72-
var decoration = toNmsDecoration(icon);
73-
decorations.remove(decoration);
74-
75-
if (sendUpdate)
76-
sendMapUpdate(null, decorations);
75+
public void setView(CartographyWindow.View view) {
76+
this.view = view;
77+
setItem(1, CraftItemStack.asCraftMirror(items.get(1)));
7778
}
7879

7980
public void setIcons(Collection<? extends MapIcon> icons, boolean sendUpdate) {

invui/src/main/java/xyz/xenondevs/invui/internal/util/ItemUtils2.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package xyz.xenondevs.invui.internal.util;
22

3+
import io.papermc.paper.datacomponent.DataComponentType;
34
import io.papermc.paper.datacomponent.DataComponentTypes;
45
import net.kyori.adventure.key.Key;
56
import net.minecraft.core.component.DataComponents;
67
import net.minecraft.world.item.component.BundleContents;
78
import org.bukkit.Material;
9+
import org.bukkit.Registry;
810
import org.bukkit.craftbukkit.inventory.CraftItemStack;
911
import org.bukkit.inventory.ItemStack;
1012
import org.jspecify.annotations.Nullable;
@@ -13,6 +15,7 @@
1315

1416
import java.util.ArrayList;
1517
import java.util.List;
18+
import java.util.Objects;
1619
import java.util.stream.Collectors;
1720

1821
import static io.papermc.paper.datacomponent.item.BundleContents.bundleContents;
@@ -248,4 +251,36 @@ public static void setSelectedBundleSlot(ItemStack bundle, int bundleSlot) {
248251
}
249252
}
250253

254+
/**
255+
* Creates a new {@link ItemStack} of the given target type, copying all data components (prototype + patch) to
256+
* the new item stack. This will make the new item stack look exactly like the original item stack, except
257+
* that it is a different type.
258+
*
259+
* @param original the original item stack to copy data components from
260+
* @param targetType the target type of the new item stack
261+
* @return a new item stack of the target type with all data components copied from the original item stack
262+
*/
263+
public static ItemStack asType(ItemStack original, Material targetType) {
264+
if (original.isEmpty())
265+
return ItemStack.empty();
266+
267+
ItemStack result = new ItemStack(targetType, original.getAmount());
268+
for (var type : Registry.DATA_COMPONENT_TYPE) {
269+
if (original.hasData(type)) {
270+
if (type instanceof DataComponentType.Valued<?> valuedType) {
271+
copyDataComponent(valuedType, original, result);
272+
} else if (type instanceof DataComponentType.NonValued nonValuedType) {
273+
result.setData(nonValuedType);
274+
}
275+
} else {
276+
result.unsetData(type);
277+
}
278+
}
279+
return result;
280+
}
281+
282+
private static <T> void copyDataComponent(DataComponentType.Valued<T> type, ItemStack from, ItemStack to) {
283+
to.setData(type, Objects.requireNonNull(from.getData(type)));
284+
}
285+
251286
}

invui/src/main/java/xyz/xenondevs/invui/util/MapIcon.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

invui/src/main/java/xyz/xenondevs/invui/util/MapPatch.java

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)