Enum patch for CraftItemFactory

This commit is contained in:
IzzelAliz 2020-07-05 17:29:40 +08:00
parent 4822b37339
commit 3753906766
10 changed files with 156 additions and 1 deletions

View File

@ -30,6 +30,7 @@ arclight {
bukkitVersion = 'v1_15_R1'
wipeVersion = true
reobfVersion = false
accessTransformer = project.file('bukkit.at')
}
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

28
arclight-common/bukkit.at Normal file
View File

@ -0,0 +1,28 @@
public org/bukkit/craftbukkit/v/inventory/CraftMetaItem
public org/bukkit/craftbukkit/v/inventory/CraftMetaItem/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaArmorStand/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaBanner/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaBlockState/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;Lorg/bukkit/Material;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaBook/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaBookSigned
public org/bukkit/craftbukkit/v/inventory/CraftMetaBookSigned/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaSkull
public org/bukkit/craftbukkit/v/inventory/CraftMetaSkull/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaLeatherArmor
public org/bukkit/craftbukkit/v/inventory/CraftMetaLeatherArmor/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaMap
public org/bukkit/craftbukkit/v/inventory/CraftMetaMap/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaPotion
public org/bukkit/craftbukkit/v/inventory/CraftMetaPotion/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaSpawnEgg/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaEnchantedBook
public org/bukkit/craftbukkit/v/inventory/CraftMetaEnchantedBook/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaFirework
public org/bukkit/craftbukkit/v/inventory/CraftMetaFirework/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaCharge
public org/bukkit/craftbukkit/v/inventory/CraftMetaCharge/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaKnowledgeBook/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaTropicalFishBucket
public org/bukkit/craftbukkit/v/inventory/CraftMetaTropicalFishBucket/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaCrossbow/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V
public org/bukkit/craftbukkit/v/inventory/CraftMetaSuspiciousStew/<init>(Lorg/bukkit/craftbukkit/v/inventory/CraftMetaItem;)V

View File

@ -15,4 +15,6 @@ public interface MaterialBridge {
@Nullable
MaterialPropertySpec bridge$getSpec();
MaterialPropertySpec.MaterialType bridge$getType();
}

View File

@ -0,0 +1,113 @@
package io.izzel.arclight.common.mixin.bukkit;
import com.google.common.collect.ImmutableMap;
import io.izzel.arclight.common.bridge.bukkit.MaterialBridge;
import io.izzel.arclight.common.mod.ArclightMod;
import io.izzel.arclight.i18n.conf.MaterialPropertySpec;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v.inventory.CraftItemFactory;
import org.bukkit.craftbukkit.v.inventory.CraftMetaArmorStand;
import org.bukkit.craftbukkit.v.inventory.CraftMetaBanner;
import org.bukkit.craftbukkit.v.inventory.CraftMetaBlockState;
import org.bukkit.craftbukkit.v.inventory.CraftMetaBook;
import org.bukkit.craftbukkit.v.inventory.CraftMetaBookSigned;
import org.bukkit.craftbukkit.v.inventory.CraftMetaCharge;
import org.bukkit.craftbukkit.v.inventory.CraftMetaCrossbow;
import org.bukkit.craftbukkit.v.inventory.CraftMetaEnchantedBook;
import org.bukkit.craftbukkit.v.inventory.CraftMetaFirework;
import org.bukkit.craftbukkit.v.inventory.CraftMetaItem;
import org.bukkit.craftbukkit.v.inventory.CraftMetaKnowledgeBook;
import org.bukkit.craftbukkit.v.inventory.CraftMetaLeatherArmor;
import org.bukkit.craftbukkit.v.inventory.CraftMetaMap;
import org.bukkit.craftbukkit.v.inventory.CraftMetaPotion;
import org.bukkit.craftbukkit.v.inventory.CraftMetaSkull;
import org.bukkit.craftbukkit.v.inventory.CraftMetaSpawnEgg;
import org.bukkit.craftbukkit.v.inventory.CraftMetaSuspiciousStew;
import org.bukkit.craftbukkit.v.inventory.CraftMetaTropicalFishBucket;
import org.bukkit.craftbukkit.v.util.CraftLegacy;
import org.bukkit.inventory.meta.ItemMeta;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.function.BiFunction;
@Mixin(value = CraftItemFactory.class, remap = false)
public class CraftItemFactoryMixin {
private static final Map<String, BiFunction<Material, CraftMetaItem, ItemMeta>> TYPES = ImmutableMap
.<String, BiFunction<Material, CraftMetaItem, ItemMeta>>builder()
.put("ARMOR_STAND", (a, b) -> new CraftMetaArmorStand(b))
.put("BANNER", (a, b) -> new CraftMetaBanner(b))
.put("TILE_ENTITY", (a, b) -> new CraftMetaBlockState(b, a))
.put("BOOK", (a, b) -> new CraftMetaBook(b))
.put("BOOK_SIGNED", (a, b) -> new CraftMetaBookSigned(b))
.put("SKULL", (a, b) -> new CraftMetaSkull(b))
.put("LEATHER_ARMOR", (a, b) -> new CraftMetaLeatherArmor(b))
.put("MAP", (a, b) -> new CraftMetaMap(b))
.put("POTION", (a, b) -> new CraftMetaPotion(b))
.put("SPAWN_EGG", (a, b) -> new CraftMetaSpawnEgg(b))
.put("ENCHANTED", (a, b) -> new CraftMetaEnchantedBook(b))
.put("FIREWORK", (a, b) -> new CraftMetaFirework(b))
.put("FIREWORK_EFFECT", (a, b) -> new CraftMetaCharge(b))
.put("KNOWLEDGE_BOOK", (a, b) -> new CraftMetaKnowledgeBook(b))
.put("TROPICAL_FISH_BUCKET", (a, b) -> new CraftMetaTropicalFishBucket(b))
.put("CROSSBOW", (a, b) -> new CraftMetaCrossbow(b))
.put("SUSPICIOUS_STEW", (a, b) -> new CraftMetaSuspiciousStew(b))
.put("UNSPECIFIC", (a, b) -> new CraftMetaItem(b))
.build();
@SuppressWarnings("AmbiguousMixinReference")
@Inject(method = "getItemMeta*", require = 0, expect = 0, cancellable = true, at = @At("HEAD"))
private void arclight$getItemMeta(Material material, CraftMetaItem meta, CallbackInfoReturnable<ItemMeta> cir) {
MaterialBridge bridge = (MaterialBridge) (Object) CraftLegacy.fromLegacy(material);
if (bridge.bridge$getType() != MaterialPropertySpec.MaterialType.VANILLA) {
MaterialPropertySpec spec = bridge.bridge$getSpec();
BiFunction<Material, CraftMetaItem, ItemMeta> func;
if (spec == null || spec.itemMetaType == null) {
func = (a, b) -> new CraftMetaItem(b);
} else {
func = TYPES.get(spec.itemMetaType);
if (func == null) {
func = (a, b) -> dynamicCreate(spec.itemMetaType, a, b);
}
}
cir.setReturnValue(func.apply(material, meta));
}
}
private ItemMeta dynamicCreate(String type, Material material, CraftMetaItem meta) {
try {
Class<?> cl = Class.forName(type);
if (!CraftMetaItem.class.isAssignableFrom(cl)) {
throw new IllegalArgumentException("" + cl + " is not assignable from " + CraftMetaItem.class);
}
for (Constructor<?> constructor : cl.getDeclaredConstructors()) {
Class<?>[] parameterTypes = constructor.getParameterTypes();
if (parameterTypes.length == 1) {
if (parameterTypes[0] == Material.class) {
constructor.setAccessible(true);
return (ItemMeta) constructor.newInstance(material);
} else if (CraftMetaItem.class.isAssignableFrom(parameterTypes[0])) {
constructor.setAccessible(true);
return (ItemMeta) constructor.newInstance(meta);
}
} else if (parameterTypes.length == 2) {
if (parameterTypes[0] == Material.class && CraftMetaItem.class.isAssignableFrom(parameterTypes[1])) {
constructor.setAccessible(true);
return (ItemMeta) constructor.newInstance(material, meta);
} else if (parameterTypes[1] == Material.class && CraftMetaItem.class.isAssignableFrom(parameterTypes[0])) {
constructor.setAccessible(true);
return (ItemMeta) constructor.newInstance(meta, material);
}
}
}
} catch (Exception e) {
ArclightMod.LOGGER.warn("Bad itemMetaType {} for {}: {}", type, material, e);
}
return new CraftMetaItem(meta);
}
}

View File

@ -154,6 +154,11 @@ public abstract class MaterialMixin implements MaterialBridge {
return arclight$spec;
}
@Override
public MaterialPropertySpec.MaterialType bridge$getType() {
return arclight$type;
}
@Override
public void bridge$setupBlock(ResourceLocation key, Block block, MaterialPropertySpec spec) {
this.arclight$spec = spec.clone();

View File

@ -12,6 +12,7 @@
"CraftChunkMixin",
"CraftConsoleCommandSenderMixin",
"CraftEventFactoryMixin",
"CraftItemFactoryMixin",
"CraftMagicNumbersMixin",
"CraftServerMixin",
"JavaPluginLoaderMixin",

View File

@ -30,6 +30,7 @@ arclight {
bukkitVersion = 'v1_14_R1'
wipeVersion = true
reobfVersion = true
accessTransformer = project(':arclight-common').file('bukkit.at')
}
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

View File

@ -30,6 +30,7 @@ arclight {
bukkitVersion = 'v1_15_R1'
wipeVersion = true
reobfVersion = true
accessTransformer = project(':arclight-common').file('bukkit.at')
}
sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8'

View File

@ -6,7 +6,7 @@ allprojects {
version '1.0.0-SNAPSHOT'
ext {
agpVersion = '1.6'
agpVersion = '1.7'
}
task cleanBuild {

View File

@ -59,6 +59,9 @@ public class MaterialPropertySpec implements Cloneable {
@Setting("craftingRemainingItem")
public String craftingRemainingItem;
@Setting("itemMetaType")
public String itemMetaType;
@Override
public MaterialPropertySpec clone() {
try {