-
-
Notifications
You must be signed in to change notification settings - Fork 131
/
NMSAlgorithmsImpl.java
177 lines (148 loc) · 6.85 KB
/
NMSAlgorithmsImpl.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.bgsoftware.superiorskyblock.nms.v117;
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.key.Key;
import com.bgsoftware.superiorskyblock.nms.NMSAlgorithms;
import com.bgsoftware.superiorskyblock.nms.algorithms.PaperGlowEnchantment;
import com.bgsoftware.superiorskyblock.nms.algorithms.SpigotGlowEnchantment;
import com.bgsoftware.superiorskyblock.nms.v117.menu.MenuBrewingStandBlockEntity;
import com.bgsoftware.superiorskyblock.nms.v117.menu.MenuDispenserBlockEntity;
import com.bgsoftware.superiorskyblock.nms.v117.menu.MenuFurnaceBlockEntity;
import com.bgsoftware.superiorskyblock.nms.v117.menu.MenuHopperBlockEntity;
import com.bgsoftware.superiorskyblock.nms.v117.world.KeyBlocksCache;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.Container;
import net.minecraft.world.entity.item.FallingBlockEntity;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.craftbukkit.v1_17_R1.CraftServer;
import org.bukkit.craftbukkit.v1_17_R1.CraftWorld;
import org.bukkit.craftbukkit.v1_17_R1.entity.CraftFallingBlock;
import org.bukkit.craftbukkit.v1_17_R1.entity.CraftMinecart;
import org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack;
import org.bukkit.craftbukkit.v1_17_R1.util.CraftChatMessage;
import org.bukkit.craftbukkit.v1_17_R1.util.CraftMagicNumbers;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect;
import java.util.EnumMap;
import java.util.function.BiFunction;
public class NMSAlgorithmsImpl implements NMSAlgorithms {
private static final EnumMap<InventoryType, MenuCreator> MENUS_HOLDER_CREATORS = new EnumMap<>(InventoryType.class);
static {
MENUS_HOLDER_CREATORS.put(InventoryType.DISPENSER, MenuDispenserBlockEntity::new);
MENUS_HOLDER_CREATORS.put(InventoryType.DROPPER, MenuDispenserBlockEntity::new);
MENUS_HOLDER_CREATORS.put(InventoryType.FURNACE, MenuFurnaceBlockEntity::new);
MENUS_HOLDER_CREATORS.put(InventoryType.BREWING, MenuBrewingStandBlockEntity::new);
MENUS_HOLDER_CREATORS.put(InventoryType.HOPPER, MenuHopperBlockEntity::new);
MENUS_HOLDER_CREATORS.put(InventoryType.BLAST_FURNACE, MenuFurnaceBlockEntity::new);
MENUS_HOLDER_CREATORS.put(InventoryType.SMOKER, MenuFurnaceBlockEntity::new);
}
private final SuperiorSkyblockPlugin plugin;
public NMSAlgorithmsImpl(SuperiorSkyblockPlugin plugin) {
this.plugin = plugin;
}
@Override
public void registerCommand(BukkitCommand command) {
((CraftServer) plugin.getServer()).getCommandMap().register("superiorskyblock2", command);
}
@Override
public String parseSignLine(String original) {
return Component.Serializer.toJson(CraftChatMessage.fromString(original)[0]);
}
@Override
public int getCombinedId(Location location) {
org.bukkit.World bukkitWorld = location.getWorld();
if (bukkitWorld == null)
return 0;
ServerLevel serverLevel = ((CraftWorld) bukkitWorld).getHandle();
BlockPos blockPos = new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
BlockState blockState = serverLevel.getBlockState(blockPos);
return Block.getId(blockState);
}
@Override
public int getCombinedId(Material material, byte data) {
BlockState blockState;
if (data == 0) {
Block block = CraftMagicNumbers.getBlock(material);
if (block == null)
return -1;
blockState = block.defaultBlockState();
} else {
blockState = CraftMagicNumbers.getBlock(material, data);
}
return blockState == null ? -1 : Block.getId(blockState);
}
@Override
public int compareMaterials(Material o1, Material o2) {
int firstMaterial = o1.isBlock() ? Block.getId(CraftMagicNumbers.getBlock(o1).defaultBlockState()) : o1.ordinal();
int secondMaterial = o2.isBlock() ? Block.getId(CraftMagicNumbers.getBlock(o2).defaultBlockState()) : o2.ordinal();
return Integer.compare(firstMaterial, secondMaterial);
}
@Override
public Key getBlockKey(int combinedId) {
Block block = Block.stateById(combinedId).getBlock();
return KeyBlocksCache.getBlockKey(block);
}
@Override
public Key getMinecartBlock(org.bukkit.entity.Minecart bukkitMinecart) {
AbstractMinecart minecart = ((CraftMinecart) bukkitMinecart).getHandle();
Block block = minecart.getDisplayBlockState().getBlock();
return KeyBlocksCache.getBlockKey(block);
}
@Override
public Key getFallingBlockType(FallingBlock bukkitFallingBlock) {
FallingBlockEntity fallingBlock = ((CraftFallingBlock) bukkitFallingBlock).getHandle();
Block block = fallingBlock.getBlockState().getBlock();
return KeyBlocksCache.getBlockKey(block);
}
@Override
public void setCustomModel(ItemMeta itemMeta, int customModel) {
itemMeta.setCustomModelData(customModel);
}
@Override
public void addPotion(PotionMeta potionMeta, PotionEffect potionEffect) {
if (!potionMeta.hasCustomEffects())
potionMeta.setColor(potionEffect.getType().getColor());
potionMeta.addCustomEffect(potionEffect, true);
}
@Override
public String getMinecraftKey(ItemStack itemStack) {
return Registry.ITEM.getKey(CraftItemStack.asNMSCopy(itemStack).getItem()).toString();
}
@Override
public Enchantment getGlowEnchant() {
try {
return new PaperGlowEnchantment("superior_glowing_enchant");
} catch (Throwable error) {
return new SpigotGlowEnchantment("superior_glowing_enchant");
}
}
@Override
public int getMaxWorldSize() {
return Bukkit.getMaxWorldSize();
}
@Override
public Object createMenuInventoryHolder(InventoryType inventoryType, InventoryHolder defaultHolder, String title) {
MenuCreator menuCreator = MENUS_HOLDER_CREATORS.get(inventoryType);
return menuCreator == null ? null : menuCreator.apply(defaultHolder, title);
}
@Override
public double getCurrentTps() {
return Bukkit.getTPS()[0];
}
private interface MenuCreator extends BiFunction<InventoryHolder, String, Container> {
}
}