-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to reference and create items
Signed-off-by: TheSilkMiner <thesilkminer@outlook.com>
- Loading branch information
1 parent
f2cd356
commit 1fa8618
Showing
7 changed files
with
227 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Common/src/main/java/com/blamejared/contenttweaker/vanilla/ContentTweakerVanillaPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.blamejared.contenttweaker.vanilla; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants; | ||
import com.blamejared.contenttweaker.core.api.object.ReferenceFactory; | ||
import com.blamejared.contenttweaker.core.api.plugin.ContentTweakerPlugin; | ||
import com.blamejared.contenttweaker.core.api.plugin.ContentTweakerPluginProvider; | ||
import com.blamejared.contenttweaker.core.api.plugin.CustomBracketRegistration; | ||
import com.blamejared.contenttweaker.core.api.plugin.FactoryMappingRegistration; | ||
import com.blamejared.contenttweaker.core.api.plugin.ObjectTypeRegistration; | ||
import com.blamejared.contenttweaker.core.api.plugin.ReferenceFactoryRegistration; | ||
import com.blamejared.contenttweaker.vanilla.object.VanillaObjectTypes; | ||
import com.blamejared.contenttweaker.vanilla.zen.bracket.ContentTweakerVanillaBrackets; | ||
import com.blamejared.contenttweaker.vanilla.zen.factory.ItemFactory; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
@ContentTweakerPlugin(ContentTweakerConstants.MOD_ID + ":vanilla") | ||
public final class ContentTweakerVanillaPlugin implements ContentTweakerPluginProvider { | ||
@Override | ||
public void registerObjectTypes(final ObjectTypeRegistration registration) { | ||
registration.registerType(VanillaObjectTypes.ITEM); | ||
} | ||
|
||
@Override | ||
public void registerFactoryMappings(final FactoryMappingRegistration registration) { | ||
registration.registerMapping(VanillaObjectTypes.ITEM, ItemFactory.class); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("Convert2Diamond") // This breaks javac (^-^) | ||
public void registerReferenceFactories(final ReferenceFactoryRegistration registration) { | ||
registration.register(VanillaObjectTypes.ITEM, ReferenceFactory.of(new TypeToken<ItemReference>() {}, ItemReference::of)); | ||
} | ||
|
||
@Override | ||
public void registerCustomBrackets(final CustomBracketRegistration registration) { | ||
ContentTweakerVanillaBrackets.register(registration); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...in/java/com/blamejared/contenttweaker/vanilla/api/zen/ContentTweakerVanillaConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.blamejared.contenttweaker.vanilla.api.zen; | ||
|
||
import com.blamejared.contenttweaker.core.api.zen.ContentTweakerZenConstants; | ||
|
||
public final class ContentTweakerVanillaConstants { | ||
public static final String VANILLA_PACKAGE_MARKER = ".vanilla"; | ||
|
||
public static final String VANILLA_BUILDER_PACKAGE = ContentTweakerZenConstants.MAIN_PACKAGE + ".builder" + VANILLA_PACKAGE_MARKER; | ||
public static final String VANILLA_FACTORY_PACKAGE = ContentTweakerZenConstants.FACTORY_PACKAGE + VANILLA_PACKAGE_MARKER; | ||
public static final String VANILLA_NATIVE_PACKAGE = ContentTweakerZenConstants.NATIVE_PACKAGE + VANILLA_PACKAGE_MARKER; | ||
public static final String VANILLA_OBJECT_PACKAGE = ContentTweakerZenConstants.OBJECT_PACKAGE + VANILLA_PACKAGE_MARKER; | ||
public static final String VANILLA_RT_PACKAGE = ContentTweakerZenConstants.RT_PACKAGE + VANILLA_PACKAGE_MARKER; | ||
public static final String VANILLA_UTIL_PACKAGE = ContentTweakerZenConstants.UTIL_PACKAGE + VANILLA_PACKAGE_MARKER; | ||
|
||
public static final String ITEM_BUILDER_PACKAGE = VANILLA_BUILDER_PACKAGE + ".item"; | ||
} |
40 changes: 40 additions & 0 deletions
40
Common/src/main/java/com/blamejared/contenttweaker/vanilla/api/zen/object/ItemReference.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.blamejared.contenttweaker.vanilla.api.zen.object; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants; | ||
import com.blamejared.contenttweaker.core.api.util.ClassArchitect; | ||
import com.blamejared.contenttweaker.core.api.zen.object.Reference; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.ContentTweakerVanillaConstants; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.object.property.ItemProperties; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.object.property.StandardItemProperties; | ||
import com.blamejared.contenttweaker.vanilla.object.VanillaObjectTypes; | ||
import com.blamejared.crafttweaker.api.annotation.ZenRegister; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.world.item.Item; | ||
import org.openzen.zencode.java.ZenCodeType; | ||
|
||
@ZenCodeType.Name(ContentTweakerVanillaConstants.VANILLA_OBJECT_PACKAGE + ".ItemReference") | ||
@ZenRegister(loaders = ContentTweakerConstants.CONTENT_LOADER_ID) | ||
public final class ItemReference extends Reference<Item> { | ||
private static final ClassArchitect<ItemProperties> ITEM_PROPERTIES_ARCHITECT = ClassArchitect.of(ItemReference.class); | ||
|
||
public static final ItemReference AIR = ItemReference.of(new ResourceLocation("air")); | ||
|
||
private ItemReference(final ResourceLocation id) { | ||
super(VanillaObjectTypes.ITEM, id); | ||
} | ||
|
||
@ZenCodeType.Method("of") | ||
public static ItemReference of(final ResourceLocation id) { | ||
return new ItemReference(id); | ||
} | ||
|
||
@ZenCodeType.Getter("properties") | ||
public StandardItemProperties properties() { | ||
return this.findProperties(StandardItemProperties.class); | ||
} | ||
|
||
@ZenCodeType.Method("findProperties") | ||
public <T extends ItemProperties> T findProperties(final Class<T> reifiedT) { | ||
return ITEM_PROPERTIES_ARCHITECT.construct(reifiedT, this); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Common/src/main/java/com/blamejared/contenttweaker/vanilla/object/VanillaObjectTypes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.blamejared.contenttweaker.vanilla.object; | ||
|
||
import com.blamejared.contenttweaker.core.api.object.ObjectType; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.sounds.SoundEvent; | ||
import net.minecraft.world.item.Item; | ||
import net.minecraft.world.level.block.Block; | ||
|
||
public final class VanillaObjectTypes { | ||
public static ObjectType<Block> BLOCK = ObjectType.of(Registry.BLOCK_REGISTRY, Block.class); | ||
public static ObjectType<Item> ITEM = ObjectType.of(Registry.ITEM_REGISTRY, Item.class); | ||
public static ObjectType<SoundEvent> SOUND_EVENT = ObjectType.of(Registry.SOUND_EVENT_REGISTRY, SoundEvent.class); | ||
|
||
private VanillaObjectTypes() {} | ||
} |
27 changes: 27 additions & 0 deletions
27
...java/com/blamejared/contenttweaker/vanilla/zen/bracket/ContentTweakerVanillaBrackets.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.blamejared.contenttweaker.vanilla.zen.bracket; | ||
|
||
import com.blamejared.contenttweaker.core.api.plugin.CustomBracketRegistration; | ||
import com.blamejared.crafttweaker.api.plugin.IBracketParserRegistrationHandler; | ||
import org.openzen.zenscript.parser.BracketExpressionParser; | ||
|
||
import java.util.function.Supplier; | ||
import java.util.stream.Stream; | ||
|
||
public final class ContentTweakerVanillaBrackets { | ||
private ContentTweakerVanillaBrackets() {} | ||
|
||
public static void register(final CustomBracketRegistration registration) { | ||
withCommand(registration, "item", new ItemBracketExpressionParser(), "cot:items", ItemBracketExpressionParser::dump); | ||
registration.registerBracket("tab", new CreativeTabBracketExpressionParser(), CreativeTabBracketExpressionParser::dump); | ||
} | ||
|
||
private static void withCommand( | ||
final CustomBracketRegistration reg, | ||
final String name, | ||
final BracketExpressionParser parser, | ||
final String sub, | ||
final Supplier<Stream<String>> dumper | ||
) { | ||
reg.registerBracket(name, parser, new IBracketParserRegistrationHandler.DumperData(sub, dumper)); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...n/java/com/blamejared/contenttweaker/vanilla/zen/bracket/ItemBracketExpressionParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.blamejared.contenttweaker.vanilla.zen.bracket; | ||
|
||
import com.blamejared.contenttweaker.core.api.zen.bracket.BracketHelper; | ||
import com.blamejared.contenttweaker.core.api.zen.bracket.ReferenceExpression; | ||
import com.blamejared.contenttweaker.vanilla.object.VanillaObjectTypes; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference; | ||
import com.blamejared.crafttweaker.api.util.ParseUtil; | ||
import com.google.gson.reflect.TypeToken; | ||
import net.minecraft.core.Registry; | ||
import net.minecraft.resources.ResourceLocation; | ||
import org.openzen.zencode.shared.CodePosition; | ||
import org.openzen.zenscript.lexer.ParseException; | ||
import org.openzen.zenscript.lexer.ZSTokenParser; | ||
import org.openzen.zenscript.parser.BracketExpressionParser; | ||
import org.openzen.zenscript.parser.expression.ParsedExpression; | ||
|
||
import java.util.stream.Stream; | ||
|
||
public final class ItemBracketExpressionParser implements BracketExpressionParser { | ||
static Stream<String> dump() { | ||
return Registry.ITEM | ||
.keySet() | ||
.stream() | ||
.sorted() | ||
.map(ResourceLocation::toString) | ||
.map("<item:%s>"::formatted); | ||
} | ||
|
||
@Override | ||
public ParsedExpression parse(final CodePosition position, final ZSTokenParser tokens) throws ParseException { | ||
final String contents = ParseUtil.readBracketContent(position, tokens); | ||
if (contents.indexOf(':') == -1) { | ||
throw new ParseException(position, "Expected <item:modid:name>, but found illegal bracket <item:" + contents + ">"); | ||
} | ||
final ResourceLocation id = BracketHelper.locationOrThrow( | ||
position, | ||
contents, | ||
() -> "Unable to convert \"" + contents + "\" to a valid location: expected <item:modid:name>" | ||
); | ||
return new ReferenceExpression<>(position, VanillaObjectTypes.ITEM, new TypeToken<ItemReference>() {}, id); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
Common/src/main/java/com/blamejared/contenttweaker/vanilla/zen/factory/ItemFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.blamejared.contenttweaker.vanilla.zen.factory; | ||
|
||
import com.blamejared.contenttweaker.core.api.ContentTweakerConstants; | ||
import com.blamejared.contenttweaker.core.api.object.ObjectFactory; | ||
import com.blamejared.contenttweaker.core.api.object.ObjectHolder; | ||
import com.blamejared.contenttweaker.core.api.object.ObjectType; | ||
import com.blamejared.contenttweaker.core.api.registry.RegistryButler; | ||
import com.blamejared.contenttweaker.core.api.resource.ResourceManager; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.ContentTweakerVanillaConstants; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.builder.ItemBuilder; | ||
import com.blamejared.contenttweaker.vanilla.object.VanillaObjectTypes; | ||
import com.blamejared.contenttweaker.core.api.util.ClassArchitect; | ||
import com.blamejared.contenttweaker.vanilla.api.zen.object.ItemReference; | ||
import com.blamejared.crafttweaker.api.annotation.ZenRegister; | ||
import net.minecraft.world.item.Item; | ||
import org.openzen.zencode.java.ZenCodeType; | ||
|
||
import java.util.Objects; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Consumer; | ||
|
||
@ZenCodeType.Name(ContentTweakerVanillaConstants.VANILLA_FACTORY_PACKAGE + ".ItemFactory") | ||
@ZenRegister(loaders = ContentTweakerConstants.CONTENT_LOADER_ID) | ||
public final class ItemFactory implements ObjectFactory<Item> { | ||
private static final ClassArchitect<ItemBuilder<?>> ARCHITECT = ClassArchitect.of(BiFunction.class); | ||
private static final BiFunction<ObjectHolder<? extends Item>, Consumer<ResourceManager>, ItemReference> REFERENCE_TO_ITEM_FUNCTION = ItemFactory::convertAndRegister; | ||
|
||
public ItemFactory() {} | ||
|
||
private static <T extends Item> ItemReference convertAndRegister(final ObjectHolder<T> holder, final Consumer<ResourceManager> resourceProvider) { | ||
// TODO("Convert to actions") | ||
Objects.requireNonNull(holder); | ||
Objects.requireNonNull(resourceProvider); | ||
RegistryButler.get().register(holder); | ||
resourceProvider.accept(ResourceManager.get()); | ||
return ItemReference.of(holder.id()); | ||
} | ||
|
||
@Override | ||
public ObjectType<Item> type() { | ||
return VanillaObjectTypes.ITEM; | ||
} | ||
|
||
@ZenCodeType.Method("typed") | ||
public static <T extends ItemBuilder<T>> T typed(final Class<T> reifiedT) { | ||
return ARCHITECT.construct(reifiedT, REFERENCE_TO_ITEM_FUNCTION); | ||
} | ||
} |