Skip to content
This repository was archived by the owner on Jun 3, 2024. It is now read-only.

Commit 6580bc0

Browse files
committed
Move getCreatorModId impl to mixins of subclasses
1 parent efbc1fd commit 6580bc0

5 files changed

Lines changed: 184 additions & 57 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.mixin.extension;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import net.minecraftforge.common.extensions.IForgeItem;
24+
25+
import net.minecraft.item.EnchantedBookItem;
26+
import net.minecraft.item.ItemStack;
27+
import net.minecraft.nbt.ListTag;
28+
import net.minecraft.util.Identifier;
29+
import net.minecraft.util.registry.Registry;
30+
31+
@Mixin(EnchantedBookItem.class)
32+
public abstract class MixinEnchantedBookItem implements IForgeItem {
33+
@Override
34+
public String getCreatorModId(ItemStack itemStack) {
35+
final Identifier id = Registry.ITEM.getId(itemStack.getItem());
36+
37+
if (!itemStack.isEmpty() && Registry.ITEM.getDefaultId().equals(id)) {
38+
return null;
39+
} else {
40+
final String namespace = id.getNamespace();
41+
42+
if ("minecraft".equals(namespace)) {
43+
final ListTag enchantments = EnchantedBookItem.getEnchantmentTag(itemStack);
44+
45+
if (enchantments.size() == 1) {
46+
final Identifier enchantmentId = Identifier.tryParse(enchantments.getCompoundTag(0).getString("id"));
47+
48+
if (Registry.ENCHANTMENT.getOrEmpty(enchantmentId).isPresent()) {
49+
return enchantmentId.getNamespace();
50+
}
51+
}
52+
}
53+
54+
return namespace;
55+
}
56+
}
57+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.mixin.extension;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import net.minecraftforge.common.extensions.IForgeItem;
24+
25+
import net.minecraft.item.ItemStack;
26+
import net.minecraft.item.PotionItem;
27+
import net.minecraft.item.TippedArrowItem;
28+
import net.minecraft.potion.Potion;
29+
import net.minecraft.potion.PotionUtil;
30+
import net.minecraft.potion.Potions;
31+
import net.minecraft.util.Identifier;
32+
import net.minecraft.util.registry.Registry;
33+
34+
@Mixin({ PotionItem.class, TippedArrowItem.class })
35+
public abstract class MixinPotionItems implements IForgeItem {
36+
@Override
37+
public String getCreatorModId(ItemStack itemStack) {
38+
Identifier id = Registry.ITEM.getId(itemStack.getItem());
39+
40+
if (!itemStack.isEmpty() && Registry.ITEM.getDefaultId().equals(id)) {
41+
return null;
42+
} else {
43+
final String namespace = id.getNamespace();
44+
45+
if ("minecraft".equals(namespace)) {
46+
final Potion potion = PotionUtil.getPotion(itemStack);
47+
48+
if (potion != Potions.EMPTY) {
49+
id = Registry.POTION.getId(potion);
50+
51+
if (Registry.POTION.getDefaultId().equals(id)) {
52+
return namespace;
53+
}
54+
55+
return id.getNamespace();
56+
}
57+
}
58+
59+
return namespace;
60+
}
61+
}
62+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Minecraft Forge, Patchwork Project
3+
* Copyright (c) 2016-2020, 2019-2020
4+
*
5+
* This library is free software; you can redistribute it and/or
6+
* modify it under the terms of the GNU Lesser General Public
7+
* License as published by the Free Software Foundation version 2.1
8+
* of the License.
9+
*
10+
* This library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
* Lesser General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Lesser General Public
16+
* License along with this library; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
package com.patchworkmc.mixin.extension;
21+
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import net.minecraftforge.common.extensions.IForgeItem;
24+
25+
import net.minecraft.entity.EntityType;
26+
import net.minecraft.item.Item;
27+
import net.minecraft.item.ItemStack;
28+
import net.minecraft.item.SpawnEggItem;
29+
import net.minecraft.util.Identifier;
30+
import net.minecraft.util.registry.Registry;
31+
32+
@Mixin(SpawnEggItem.class)
33+
public abstract class MixinSpawnEggItem implements IForgeItem {
34+
@Override
35+
public String getCreatorModId(ItemStack itemStack) {
36+
final Item item = itemStack.getItem();
37+
Identifier id = Registry.ITEM.getId(item);
38+
39+
if (!itemStack.isEmpty() && Registry.ITEM.getDefaultId().equals(id)) {
40+
return null;
41+
} else {
42+
final String namespace = id.getNamespace();
43+
44+
if ("minecraft".equals(namespace)) {
45+
final EntityType<?> type = ((SpawnEggItem) item).getEntityType(null);
46+
id = Registry.ENTITY_TYPE.getId(type);
47+
48+
final Identifier defaultId = Registry.ENTITY_TYPE.getDefaultId();
49+
50+
if (type != Registry.ENTITY_TYPE.get(defaultId) && defaultId.equals(id)) {
51+
return namespace;
52+
}
53+
54+
return id.getNamespace();
55+
}
56+
57+
return namespace;
58+
}
59+
}
60+
}

patchwork-extensions/src/main/java/net/minecraftforge/common/extensions/IForgeItem.java

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,29 +37,20 @@
3737
import net.minecraft.client.render.item.ItemDynamicRenderer;
3838
import net.minecraft.enchantment.Enchantment;
3939
import net.minecraft.entity.Entity;
40-
import net.minecraft.entity.EntityType;
4140
import net.minecraft.entity.EquipmentSlot;
4241
import net.minecraft.entity.ItemEntity;
4342
import net.minecraft.entity.LivingEntity;
4443
import net.minecraft.entity.attribute.EntityAttributeModifier;
4544
import net.minecraft.entity.mob.MobEntity;
4645
import net.minecraft.entity.player.PlayerEntity;
4746
import net.minecraft.item.AxeItem;
48-
import net.minecraft.item.EnchantedBookItem;
4947
import net.minecraft.item.Item;
5048
import net.minecraft.item.ItemGroup;
5149
import net.minecraft.item.ItemPropertyGetter;
5250
import net.minecraft.item.ItemStack;
5351
import net.minecraft.item.ItemUsageContext;
5452
import net.minecraft.item.Items;
55-
import net.minecraft.item.PotionItem;
56-
import net.minecraft.item.SpawnEggItem;
57-
import net.minecraft.item.TippedArrowItem;
5853
import net.minecraft.nbt.CompoundTag;
59-
import net.minecraft.nbt.ListTag;
60-
import net.minecraft.potion.Potion;
61-
import net.minecraft.potion.PotionUtil;
62-
import net.minecraft.potion.Potions;
6354
import net.minecraft.tag.Tag;
6455
import net.minecraft.util.ActionResult;
6556
import net.minecraft.util.Identifier;
@@ -676,7 +667,6 @@ default boolean canContinueUsing(ItemStack oldStack, ItemStack newStack) {
676667
return oldStack.equals(newStack);
677668
}
678669

679-
// TODO: Move contents to ForgeHooks
680670
/**
681671
* Called to get the Mod ID of the mod that *created* the ItemStack, instead of
682672
* the real Mod ID that *registered* it.
@@ -693,53 +683,8 @@ default boolean canContinueUsing(ItemStack oldStack, ItemStack newStack) {
693683
*/
694684
@Nullable
695685
default String getCreatorModId(ItemStack itemStack) {
696-
final Item item = itemStack.getItem();
697-
Identifier id = Registry.ITEM.getId(item);
698-
699-
if (!itemStack.isEmpty() && Registry.ITEM.getDefaultId().equals(id)) {
700-
return null;
701-
} else {
702-
final String namespace = id.getNamespace();
703-
704-
if ("minecraft".equals(namespace)) {
705-
if (item instanceof EnchantedBookItem) {
706-
final ListTag enchantments = EnchantedBookItem.getEnchantmentTag(itemStack);
707-
708-
if (enchantments.size() == 1) {
709-
final Identifier enchantmentId = Identifier.tryParse(enchantments.getCompoundTag(0).getString("id"));
710-
711-
if (Registry.ENCHANTMENT.getOrEmpty(enchantmentId).isPresent()) {
712-
return enchantmentId.getNamespace();
713-
}
714-
}
715-
} else if (item instanceof PotionItem || item instanceof TippedArrowItem) {
716-
final Potion potion = PotionUtil.getPotion(itemStack);
717-
718-
if (potion != Potions.EMPTY) {
719-
id = Registry.POTION.getId(potion);
720-
721-
if (Registry.POTION.getDefaultId().equals(id)) {
722-
return namespace;
723-
}
724-
725-
return id.getNamespace();
726-
}
727-
} else if (item instanceof SpawnEggItem) {
728-
final EntityType<?> type = ((SpawnEggItem) item).getEntityType(null);
729-
id = Registry.ENTITY_TYPE.getId(type);
730-
731-
final Identifier defaultId = Registry.ENTITY_TYPE.getDefaultId();
732-
733-
if (type != Registry.ENTITY_TYPE.get(defaultId) && defaultId.equals(id)) {
734-
return namespace;
735-
}
736-
737-
return id.getNamespace();
738-
}
739-
}
740-
741-
return namespace;
742-
}
686+
final Identifier id = Registry.ITEM.getId(itemStack.getItem());
687+
return !itemStack.isEmpty() && Registry.ITEM.getDefaultId().equals(id) ? null : id.getNamespace();
743688
}
744689

745690
// TODO: Call locations: Patches: ItemStack

patchwork-extensions/src/main/resources/patchwork-extensions.mixins.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
"compatibilityLevel": "JAVA_8",
55
"mixins": [
66
"ItemTagsAccessor",
7+
"MixinEnchantedBookItem",
78
"MixinEntityType",
89
"MixinEntityTypeBuilder",
910
"MixinItem",
1011
"MixinItemSettings",
12+
"MixinPotionItems",
13+
"MixinSpawnEggItem",
1114
"MixinStatusEffect"
1215
],
1316
"client": [

0 commit comments

Comments
 (0)