-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathImageFilledMapUtils.java
More file actions
105 lines (97 loc) · 3.83 KB
/
ImageFilledMapUtils.java
File metadata and controls
105 lines (97 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2025. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2025. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe.utils;
import com.loohp.imageframe.ImageFrame;
import com.loohp.imageframe.nms.NMS;
import com.loohp.imageframe.objectholders.FilledMapItemInfo;
import com.loohp.imageframe.objectholders.ImageMap;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.MapMeta;
import org.bukkit.map.MapView;
public class ImageFilledMapUtils {
public static void processImageFilledMaps(SlotAccessor slotAccess, int size) {
processImageFilledMaps(slotAccess, 0, size);
}
public static void processImageFilledMaps(SlotAccessor slotAccess, int begin, int size) {
for (int i = begin; i < size; i++) {
ItemStack replacement = processImageFilledMap(slotAccess.get(i));
if (replacement != null) {
slotAccess.set(i, replacement);
}
}
}
public static ItemStack processImageFilledMap(ItemStack itemStack) {
if (itemStack == null) {
return null;
}
if (!itemStack.getType().equals(Material.FILLED_MAP)) {
return null;
}
ItemMeta itemMeta = itemStack.getItemMeta();
if (!(itemMeta instanceof MapMeta)) {
return null;
}
MapMeta mapMeta = (MapMeta) itemMeta;
MapView mapView = getMapViewSafely(mapMeta);
FilledMapItemInfo info = NMS.getInstance().getFilledMapItemInfo(itemStack);
if (info == null) {
if (mapView != null) {
ImageMap imageMap = ImageFrame.imageMapManager.getFromMapView(mapView);
if (imageMap != null) {
int imageIndex = imageMap.getImageIndex();
int partIndex = imageMap.getMapIds().indexOf(mapView.getId());
if (partIndex >= 0) {
return NMS.getInstance().withFilledMapItemInfo(itemStack, new FilledMapItemInfo(imageIndex, partIndex));
}
}
}
} else {
ImageMap imageMap = ImageFrame.imageMapManager.getFromImageId(info.getImageMapIndex());
if (imageMap == null || !imageMap.isValid()) {
return null;
}
int mapPartIndex = info.getMapPartIndex();
if (mapPartIndex < 0 || mapPartIndex >= imageMap.getMapViews().size()) {
return null;
}
MapView correctMapView = imageMap.getMapViews().get(mapPartIndex);
if (correctMapView == null) {
return null;
}
if (mapView == null || correctMapView.getId() != mapView.getId()) {
mapMeta.setMapView(correctMapView);
itemStack.setItemMeta(mapMeta);
}
}
return null;
}
private static MapView getMapViewSafely(MapMeta mapMeta) {
try {
if (!mapMeta.hasMapView()) {
return null;
}
return mapMeta.getMapView();
} catch (IllegalStateException ignored) {
return null;
}
}
}