Skip to content

Commit

Permalink
#1403, SPIGOT-4288, SPIGOT-6202: Add material rerouting in preparatio…
Browse files Browse the repository at this point in the history
…n for the switch to ItemType and BlockType

This also moves the conversion from and to legacy material to the method
calls of legacy plugins, and no longer allows them directly in the
server.

This has the side effect of fixing some legacy plugin issues, such as
SPIGOT-4288, SPIGOT-6161. Also fixes legacy items sometimes not stacking
in inventory when using addItem, a client disconnect when using legacy
items in recipes and probably some more.
  • Loading branch information
DerFrZocker authored and md-5 committed May 28, 2024
1 parent 94e44ec commit 9608279
Show file tree
Hide file tree
Showing 7 changed files with 729 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static net.minecraft.world.item.ItemStack asNMSCopy(ItemStack original) {
return net.minecraft.world.item.ItemStack.EMPTY;
}

Item item = CraftMagicNumbers.getItem(original.getType(), original.getDurability());
Item item = CraftItemType.bukkitToMinecraft(original.getType());

if (item == null) {
return net.minecraft.world.item.ItemStack.EMPTY;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/bukkit/craftbukkit/legacy/CraftEvil.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.bukkit.block.BlockState;
import org.bukkit.block.data.BlockData;
import org.bukkit.craftbukkit.block.CraftBlock;
import org.bukkit.craftbukkit.block.CraftBlockState;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.MaterialData;

Expand All @@ -38,6 +37,15 @@ private CraftEvil() {
//
}

public static void setDurability(ItemStack itemStack, short durability) {
itemStack.setDurability(durability);
MaterialData materialData = CraftLegacy.toLegacyData(itemStack.getType(), true);

if (materialData.getItemType().getMaxDurability() <= 0) {
itemStack.setType(CraftLegacy.fromLegacy(new MaterialData(materialData.getItemType(), (byte) itemStack.getDurability()), true));
}
}

public static int getBlockTypeIdAt(World world, int x, int y, int z) {
return getId(world.getBlockAt(x, y, z).getType());
}
Expand All @@ -46,14 +54,6 @@ public static int getBlockTypeIdAt(World world, Location location) {
return getId(world.getBlockAt(location).getType());
}

public static Material getType(Block block) {
return CraftLegacy.toLegacyMaterial(((CraftBlock) block).getNMS());
}

public static Material getType(BlockState block) {
return CraftLegacy.toLegacyMaterial(((CraftBlockState) block).getHandle());
}

public static int getTypeId(Block block) {
return getId(block.getType());
}
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/bukkit/craftbukkit/legacy/CraftLegacy.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ public static Material toLegacy(Material material) {
}

public static MaterialData toLegacyData(Material material) {
return toLegacyData(material, false);
}

public static MaterialData toLegacyData(Material material, boolean itemPriority) {
Preconditions.checkArgument(!material.isLegacy(), "toLegacy on legacy Material");
MaterialData mappedData;
MaterialData mappedData = null;

if (material.isBlock()) {
if (itemPriority) {
Item item = CraftMagicNumbers.getItem(material);
mappedData = itemToMaterial.get(item);
}

if (mappedData == null && material.isBlock()) {
Block block = CraftMagicNumbers.getBlock(material);
IBlockData blockData = block.defaultBlockState();

Expand All @@ -84,7 +93,7 @@ public static MaterialData toLegacyData(Material material) {
mappedData = itemToMaterial.get(block.asItem());
}
}
} else {
} else if (!itemPriority) {
Item item = CraftMagicNumbers.getItem(material);
mappedData = itemToMaterial.get(item);
}
Expand Down
Loading

0 comments on commit 9608279

Please sign in to comment.