Skip to content
This repository has been archived by the owner on Dec 21, 2024. It is now read-only.

Commit

Permalink
Modify the registration of Material to be better compatible with FAWE
Browse files Browse the repository at this point in the history
  • Loading branch information
Mgazul committed Dec 1, 2023
1 parent aff82b2 commit 3ca63f6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 86 deletions.
20 changes: 10 additions & 10 deletions src/main/java/com/mohistmc/forge/ForgeInjectBukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import net.minecraft.entity.item.PaintingType;
import net.minecraft.entity.merchant.villager.VillagerProfession;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.particles.ParticleType;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectInstance;
Expand Down Expand Up @@ -99,14 +100,13 @@ public static void init(){


public static void addEnumMaterialInItems(){
for (Map.Entry<RegistryKey<Item>, Item> entry : ForgeRegistries.ITEMS.getEntries()) {
ResourceLocation resourceLocation = entry.getValue().getRegistryName();
for (Item item : ForgeRegistries.ITEMS) {
ResourceLocation resourceLocation = item.getRegistryName();
if(!resourceLocation.getNamespace().equals(NamespacedKey.MINECRAFT)) {
// inject item materials into Bukkit for FML
String materialName = normalizeName(entry.getKey().toString()).replace("RESOURCEKEYMINECRAFT_ITEM__", "");
Item item = entry.getValue();
String materialName = normalizeName(resourceLocation.toString());
int id = Item.getId(item);
Material material = Material.addMaterial(materialName, id, false, resourceLocation.getNamespace());
Material material = Material.addMaterial(materialName, id, item.getMaxStackSize(), false, true, resourceLocation);
CraftMagicNumbers.ITEM_MATERIAL.put(item, material);
CraftMagicNumbers.MATERIAL_ITEM.put(material, item);
if (material != null) {
Expand All @@ -118,14 +118,14 @@ public static void addEnumMaterialInItems(){


public static void addEnumMaterialsInBlocks(){
for (Map.Entry<RegistryKey<Block>, Block> entry : ForgeRegistries.BLOCKS.getEntries()) {
ResourceLocation resourceLocation = entry.getValue().getRegistryName();
for (Block block : ForgeRegistries.BLOCKS) {
ResourceLocation resourceLocation = block.getRegistryName();
if(!resourceLocation.getNamespace().equals(NamespacedKey.MINECRAFT)) {
// inject block materials into Bukkit for FML
String materialName = normalizeName(entry.getKey().toString()).replace("RESOURCEKEYMINECRAFT_BLOCK__", "");
Block block = entry.getValue();
String materialName = normalizeName(resourceLocation.toString());
int id = Item.getId(block.asItem());
Material material = Material.addMaterial(materialName, id, true, resourceLocation.getNamespace());
Item item = Item.byId(id);
Material material = Material.addMaterial(materialName, id, item.getMaxStackSize(), true, false, resourceLocation);
CraftMagicNumbers.BLOCK_MATERIAL.put(block, material);
CraftMagicNumbers.MATERIAL_BLOCK.put(material, block);
if (material != null) {
Expand Down
88 changes: 12 additions & 76 deletions src/main/java/org/bukkit/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Map;
import java.util.function.Consumer;

import net.minecraft.util.ResourceLocation;
import org.apache.commons.lang.Validate;
import org.bukkit.block.data.Ageable;
import org.bukkit.block.data.AnaloguePowerable;
Expand Down Expand Up @@ -81,6 +82,7 @@
import org.bukkit.block.data.type.TurtleEgg;
import org.bukkit.block.data.type.Wall;
import org.bukkit.block.data.type.WallSign;
import org.bukkit.craftbukkit.v1_16_R3.util.CraftNamespacedKey;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.material.MaterialData;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -3516,36 +3518,25 @@ public enum Material implements Keyed {
public static final String LEGACY_PREFIX = "LEGACY_";

private final int id;
private int blockID;
private final Constructor<? extends MaterialData> ctor;
private static final Map<String, Material> BY_NAME = Maps.newHashMap();
private final int maxStack;
private final short durability;
public final Class<?> data;
private final boolean legacy;
private final NamespacedKey key;

private String modName;
private NamespacedKey keyForge;
public NamespacedKey key;
private boolean isForgeBlock = false;
public boolean isForgeItem = false;

private Material(final int id) {
this(id, 64);
}

// Mohist start - constructor used to set if the Material is a block or not
private Material(final int id, boolean flag) {
this(id, 64);
this.isForgeBlock = flag;
}
// Mohist end

// Mohist start - constructor used to set if block is modded
private Material(final int id, boolean flag, String modName) {
private Material(final int id, final int stack, boolean isForgeBlock, boolean isForgeItem) {
this(id, 64);
this.isForgeBlock = flag;
this.modName = modName;
this.keyForge = new NamespacedKey(modName, this.name().toLowerCase(Locale.ROOT).substring(modName.length() + 1));
this.isForgeBlock = isForgeBlock;
this.isForgeItem = isForgeItem;
}
// Mohist end

Expand All @@ -3567,16 +3558,11 @@ private Material(final int id, final int stack, /*@NotNull*/ final Class<?> data

private Material(final int id, final int stack, final int durability, /*@NotNull*/ final Class<?> data) {
this.id = id;
this.blockID = id;
this.durability = (short) durability;
this.maxStack = stack;
this.data = data;
this.legacy = this.name().startsWith(LEGACY_PREFIX);
this.key = NamespacedKey.minecraft(this.name().toLowerCase(Locale.ROOT));
if (this.modName == null) {
this.modName = "minecraft";
this.keyForge = this.key;
}
// try to cache the constructor for this material
try {
if (MaterialData.class.isAssignableFrom(data)) {
Expand All @@ -3603,32 +3589,6 @@ public int getId() {
return id;
}

/**
* Gets the ForgeBlock ID of this Material
*
* @return ID of this material's Block
*/

public int getBlockID() {
return blockID;
}

/**
* Gets the mod that add the material
* @return the mod name
*/
public String getModName() {
return modName;
}

/**
* Gets the correct NamespacedKey (modName:blockName)
* @return correct NamespacedKey
*/
public NamespacedKey getKeyForge() {
return keyForge;
}

/**
* Do not use for any reason.
*
Expand Down Expand Up @@ -8730,45 +8690,21 @@ public EquipmentSlot getEquipmentSlot() {
}
}

// use a normalize() function to ensure it is accessible after a round-trip
public static String normalizeName(String name) {
return name.toUpperCase(java.util.Locale.ENGLISH).replaceAll("(:|\\s)", "_").replaceAll("\\W", "");
}

public static Material addMaterial(String materialName, int id, boolean isBlock) {
// Forge Blocks
if (isBlock) {
Material material = BY_NAME.get(materialName);
if (material != null){
material.blockID = id;
material.isForgeBlock = true;
}else {
material = (Material) MohistEnumHelper.addEnum(Material.class, materialName, new Class[]{Integer.TYPE, Boolean.TYPE}, new Object[]{id, true});
}
BY_NAME.put(materialName, material);
return material;
} else { // Forge Items
Material material = (Material) MohistEnumHelper.addEnum(Material.class, materialName, new Class[]{Integer.TYPE, Boolean.TYPE}, new Object[]{id, false});
BY_NAME.put(materialName, material);
return material;
}
}

public static Material addMaterial(String materialName, int id, boolean isBlock, String modName) {
// Forge Blocks
public static Material addMaterial(String materialName, int id, int stack, boolean isBlock, boolean isItem, ResourceLocation resourceLocation) {
if (isBlock) {
Material material = BY_NAME.get(materialName);
if (material != null){
material.blockID = id;
material.isForgeBlock = true;
}else {
material = (Material) MohistEnumHelper.addEnum(Material.class, materialName, new Class[]{Integer.TYPE, Boolean.TYPE, String.class}, new Object[]{id, true, modName});
material = MohistEnumHelper.addEnum(Material.class, materialName, new Class[]{Integer.TYPE, Integer.TYPE, Boolean.TYPE, Boolean.TYPE}, new Object[]{id, stack, isBlock, isItem});
}
BY_NAME.put(materialName, material);
material.key = CraftNamespacedKey.fromMinecraft(resourceLocation);
return material;
} else { // Forge Items
Material material = (Material) MohistEnumHelper.addEnum(Material.class, materialName, new Class[]{Integer.TYPE, Boolean.TYPE, String.class}, new Object[]{id, false, modName});
Material material = MohistEnumHelper.addEnum(Material.class, materialName, new Class[]{Integer.TYPE, Integer.TYPE, Boolean.TYPE, Boolean.TYPE}, new Object[]{id, stack, isBlock, isItem});
BY_NAME.put(materialName, material);
material.key = CraftNamespacedKey.fromMinecraft(resourceLocation);
return material;
}
}
Expand Down

0 comments on commit 3ca63f6

Please sign in to comment.