From 295a095bdfacdc5663b6c6c95a897a46d201eaa9 Mon Sep 17 00:00:00 2001 From: FirstMegaGame4 Date: Tue, 5 Dec 2023 15:09:29 +0100 Subject: [PATCH] Add EnvRestriction And InternalOf Annotations Change AdvancedItemSettings --- .../AbstractBlockSettingsDuckInterface.java | 3 + .../ducks/BlockEntityTypeDuckInterface.java | 2 + .../ducks/ChunkNoiseSamplerDuckInterface.java | 3 + .../ducks/ClientWorldDuckInterface.java | 3 + .../ducks/EntityDuckInterface.java | 2 + .../FallingBlockEntityDuckInterface.java | 3 + .../ducks/FlowableFluidDuckInterface.java | 3 + .../ducks/GeneratorOptionsDuckInterface.java | 2 + .../ducks/LivingEntityDuckInterface.java | 3 + .../ducks/NetherPortalBlockDuckInterface.java | 3 + .../ducks/PortalForcerDuckInterface.java | 2 + .../QuiltEntityTypeBuilderDuckInterface.java | 2 + .../ducks/ServerPlayerDuckInterface.java | 3 + .../ducks/ServerWorldDuckInterface.java | 2 + .../mmodding_lib/impl/ModMenuApiImpl.java | 2 + .../library/glint/GlintPackView.java | 2 + .../items/settings/AdvancedItemSettings.java | 12 +++- .../library/utils/EnvRestriction.java | 55 +++++++++++++++++++ .../library/utils/InternalOf.java | 16 ++++++ .../mixin/injectors/ItemMixin.java | 2 +- 20 files changed, 121 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/utils/EnvRestriction.java create mode 100644 src/main/java/com/mmodding/mmodding_lib/library/utils/InternalOf.java diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/AbstractBlockSettingsDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/AbstractBlockSettingsDuckInterface.java index 69fd866..1979911 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/AbstractBlockSettingsDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/AbstractBlockSettingsDuckInterface.java @@ -1,7 +1,10 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.blocks.settings.AdvancedBlockSettings; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import org.quiltmc.qsl.base.api.util.TriState; +@InternalOf(targets = AdvancedBlockSettings.class) public interface AbstractBlockSettingsDuckInterface { TriState mmodding_lib$getTranslucent(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/BlockEntityTypeDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/BlockEntityTypeDuckInterface.java index 6cd1564..bbeb83a 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/BlockEntityTypeDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/BlockEntityTypeDuckInterface.java @@ -1,10 +1,12 @@ package com.mmodding.mmodding_lib.ducks; import com.mmodding.mmodding_lib.library.blockentities.BlockEntityTypeBlockSupportCallback; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import org.quiltmc.qsl.base.api.event.Event; +@InternalOf(targets = BlockEntityTypeBlockSupportCallback.class) public interface BlockEntityTypeDuckInterface { Event mmodding_lib$supportBlocksCallback(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/ChunkNoiseSamplerDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/ChunkNoiseSamplerDuckInterface.java index d87d4d4..25e637e 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/ChunkNoiseSamplerDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/ChunkNoiseSamplerDuckInterface.java @@ -1,8 +1,11 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.utils.InternalOf; +import com.mmodding.mmodding_lib.library.worldgen.veins.CustomVeinType; import net.minecraft.util.Holder; import net.minecraft.world.gen.chunk.ChunkGeneratorSettings; +@InternalOf(targets = CustomVeinType.class) public interface ChunkNoiseSamplerDuckInterface { Holder mmodding_lib$getSettingsHolder(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/ClientWorldDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/ClientWorldDuckInterface.java index 0895261..2ab4ad2 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/ClientWorldDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/ClientWorldDuckInterface.java @@ -1,7 +1,10 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.stellar.client.StellarObject; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import com.mmodding.mmodding_lib.states.readable.ReadableStellarStatuses; +@InternalOf(targets = StellarObject.class) public interface ClientWorldDuckInterface { ReadableStellarStatuses mmodding_lib$getStellarStatusesAccess(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/EntityDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/EntityDuckInterface.java index a0e7161..ea364d8 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/EntityDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/EntityDuckInterface.java @@ -2,9 +2,11 @@ import com.mmodding.mmodding_lib.library.portals.squared.CustomSquaredPortal; import com.mmodding.mmodding_lib.library.portals.squared.CustomSquaredPortalBlock; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; +@InternalOf(targets = CustomSquaredPortalBlock.class) public interface EntityDuckInterface { boolean mmodding_lib$isInCustomPortal(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/FallingBlockEntityDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/FallingBlockEntityDuckInterface.java index b526b36..a6f5df7 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/FallingBlockEntityDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/FallingBlockEntityDuckInterface.java @@ -1,7 +1,10 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.blocks.interactions.data.FallingBlockInteractionData; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.block.BlockState; +@InternalOf(targets = FallingBlockInteractionData.class) public interface FallingBlockEntityDuckInterface { BlockState mmodding_lib$getInitialBlockState(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/FlowableFluidDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/FlowableFluidDuckInterface.java index 6d9820c..984d3a8 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/FlowableFluidDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/FlowableFluidDuckInterface.java @@ -1,9 +1,12 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.blocks.CustomFluidBlock; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.fluid.FluidState; import java.util.function.IntFunction; +@InternalOf(targets = CustomFluidBlock.class) public interface FlowableFluidDuckInterface { void mmodding_lib$putCustomStates(FluidState stillState, IntFunction flowingStates); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/GeneratorOptionsDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/GeneratorOptionsDuckInterface.java index 5d462d1..8a28597 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/GeneratorOptionsDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/GeneratorOptionsDuckInterface.java @@ -1,11 +1,13 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import com.mmodding.mmodding_lib.states.persistant.DifferedSeeds; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtList; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.World; +@InternalOf(targets = DifferedSeeds.class) public interface GeneratorOptionsDuckInterface { void mmodding_lib$fillDimensionSeedAddendsNbt(NbtList list); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/LivingEntityDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/LivingEntityDuckInterface.java index 29cb8eb..96f4081 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/LivingEntityDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/LivingEntityDuckInterface.java @@ -1,9 +1,12 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.entities.projectiles.StuckArrowDisplay; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.util.Identifier; import java.util.List; +@InternalOf(targets = StuckArrowDisplay.class) public interface LivingEntityDuckInterface { List mmodding_lib$getStuckArrowTypes(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/NetherPortalBlockDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/NetherPortalBlockDuckInterface.java index bd7cac4..94c4371 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/NetherPortalBlockDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/NetherPortalBlockDuckInterface.java @@ -1,10 +1,13 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.portals.squared.CustomSquaredPortalBlock; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.block.BlockState; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.minecraft.world.WorldAccess; +@InternalOf(targets = CustomSquaredPortalBlock.class) public interface NetherPortalBlockDuckInterface { BlockState mmodding_lib$getAbstractStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/PortalForcerDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/PortalForcerDuckInterface.java index 97a487d..f1fded3 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/PortalForcerDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/PortalForcerDuckInterface.java @@ -1,6 +1,7 @@ package com.mmodding.mmodding_lib.ducks; import com.mmodding.mmodding_lib.library.portals.squared.CustomSquaredPortal; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.util.math.BlockPos; import net.minecraft.util.registry.RegistryKey; import net.minecraft.world.BlockLocating; @@ -9,6 +10,7 @@ import java.util.Optional; +@InternalOf(targets = CustomSquaredPortal.class) public interface PortalForcerDuckInterface { void mmodding_lib$setUseCustomPortalElements(boolean useCustomPortalElements); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/QuiltEntityTypeBuilderDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/QuiltEntityTypeBuilderDuckInterface.java index 9c308c7..b238de8 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/QuiltEntityTypeBuilderDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/QuiltEntityTypeBuilderDuckInterface.java @@ -1,8 +1,10 @@ package com.mmodding.mmodding_lib.ducks; import com.mmodding.mmodding_lib.library.entities.CustomEntityType; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.entity.Entity; +@InternalOf(targets = CustomEntityType.class) public interface QuiltEntityTypeBuilderDuckInterface { CustomEntityType mmodding_lib$buildCustom(); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/ServerPlayerDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/ServerPlayerDuckInterface.java index 2da6614..1555f7b 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/ServerPlayerDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/ServerPlayerDuckInterface.java @@ -1,5 +1,7 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.portals.squared.CustomSquaredPortal; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.world.BlockLocating; @@ -7,6 +9,7 @@ import java.util.Optional; +@InternalOf(targets = CustomSquaredPortal.class) public interface ServerPlayerDuckInterface { Optional mmodding_lib$getCustomPortalRect(ServerWorld destWorld, BlockPos destPos, WorldBorder worldBorder); diff --git a/src/main/java/com/mmodding/mmodding_lib/ducks/ServerWorldDuckInterface.java b/src/main/java/com/mmodding/mmodding_lib/ducks/ServerWorldDuckInterface.java index 9397055..81b2740 100644 --- a/src/main/java/com/mmodding/mmodding_lib/ducks/ServerWorldDuckInterface.java +++ b/src/main/java/com/mmodding/mmodding_lib/ducks/ServerWorldDuckInterface.java @@ -1,8 +1,10 @@ package com.mmodding.mmodding_lib.ducks; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import com.mmodding.mmodding_lib.states.persistant.StellarStatuses; import net.minecraft.nbt.NbtCompound; +@InternalOf(targets = StellarStatuses.class) public interface ServerWorldDuckInterface { StellarStatuses mmodding_lib$createStellarStatuses(); diff --git a/src/main/java/com/mmodding/mmodding_lib/impl/ModMenuApiImpl.java b/src/main/java/com/mmodding/mmodding_lib/impl/ModMenuApiImpl.java index d9786ec..85b70a4 100644 --- a/src/main/java/com/mmodding/mmodding_lib/impl/ModMenuApiImpl.java +++ b/src/main/java/com/mmodding/mmodding_lib/impl/ModMenuApiImpl.java @@ -3,12 +3,14 @@ import com.mmodding.mmodding_lib.MModdingLib; import com.mmodding.mmodding_lib.client.MModdingScreen; import com.mmodding.mmodding_lib.library.config.client.screen.ConfigScreen; +import com.mmodding.mmodding_lib.library.utils.InternalOf; import com.terraformersmc.modmenu.api.ConfigScreenFactory; import com.terraformersmc.modmenu.api.ModMenuApi; import java.util.HashMap; import java.util.Map; +@InternalOf(targets = ConfigScreen.class) public class ModMenuApiImpl implements ModMenuApi { @Override diff --git a/src/main/java/com/mmodding/mmodding_lib/library/glint/GlintPackView.java b/src/main/java/com/mmodding/mmodding_lib/library/glint/GlintPackView.java index f7412ff..17cf3af 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/glint/GlintPackView.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/glint/GlintPackView.java @@ -1,9 +1,11 @@ package com.mmodding.mmodding_lib.library.glint; +import com.mmodding.mmodding_lib.library.utils.EnvRestriction; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; +@EnvRestriction(side = EnvRestriction.Side.SERVER_COMMON) public interface GlintPackView { static GlintPackView of(ItemStack stack) { diff --git a/src/main/java/com/mmodding/mmodding_lib/library/items/settings/AdvancedItemSettings.java b/src/main/java/com/mmodding/mmodding_lib/library/items/settings/AdvancedItemSettings.java index c9c38c6..c5d3245 100644 --- a/src/main/java/com/mmodding/mmodding_lib/library/items/settings/AdvancedItemSettings.java +++ b/src/main/java/com/mmodding/mmodding_lib/library/items/settings/AdvancedItemSettings.java @@ -5,6 +5,7 @@ import net.minecraft.item.FoodComponent; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; +import net.minecraft.item.ItemStack; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Rarity; @@ -12,12 +13,13 @@ import java.util.Collections; import java.util.List; +import java.util.function.Function; public class AdvancedItemSettings extends QuiltItemSettings { public static final CustomItemSetting> NAME_FORMATTINGS = CustomItemSetting.create(Collections::emptyList); public static final CustomItemSetting DESCRIPTION_LINES = CustomItemSetting.create(() -> null); - public static final CustomItemSetting GLINT = CustomItemSetting.create(Boolean.FALSE); + public static final CustomItemSetting> GLINT = CustomItemSetting.create((stack) -> false); public static final CustomItemSetting GLINT_PACK = CustomItemSetting.create(() -> null); public static final CustomItemSetting EATABLE = CustomItemSetting.create(Boolean.FALSE); public static final CustomItemSetting DRINKABLE = CustomItemSetting.create(Boolean.FALSE); @@ -37,8 +39,12 @@ public AdvancedItemSettings descriptionLines(Text... descriptionLines) { return this.customSetting(DESCRIPTION_LINES, descriptionLines); } - public AdvancedItemSettings glint() { - return this.customSetting(GLINT, true); + public AdvancedItemSettings glint(boolean glint) { + return this.glint(stack -> glint); + } + + public AdvancedItemSettings glint(Function glint) { + return this.customSetting(GLINT, glint); } public AdvancedItemSettings glintPack(GlintPackView view) { diff --git a/src/main/java/com/mmodding/mmodding_lib/library/utils/EnvRestriction.java b/src/main/java/com/mmodding/mmodding_lib/library/utils/EnvRestriction.java new file mode 100644 index 0000000..a790577 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/utils/EnvRestriction.java @@ -0,0 +1,55 @@ +package com.mmodding.mmodding_lib.library.utils; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Applies an environment restriction to the element used. + * If the side is not the same as the current one it will crash. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.TYPE_USE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PACKAGE}) +public @interface EnvRestriction { + + Side side() default Side.COMMON; + + enum Side { + + /** + * Means that the annotated member should be only used by the client thread. + */ + CLIENT_THREAD, + + /** + * Means that the annotated member should be only used on the client side. + */ + CLIENT_COMMON, + + /** + * Means that the annotated member can be used an all sides. This is the default value. + */ + COMMON, + + /** + * Means that the annotated member should be only used on the server side. (Integrated or Dedicated, we don't really care). + */ + SERVER_COMMON, + + /** + * Means that the annotated member should be only used by the server thread. + */ + SERVER_THREAD, + + /** + * Means that the annotated member should be only used by an integrated server thread. + */ + INTEGRATED_SERVER_THREAD, + + /** + * Means that the annotated member should be only used by a dedicated server thread. + */ + DEDICATED_SERVER_THREAD + } +} diff --git a/src/main/java/com/mmodding/mmodding_lib/library/utils/InternalOf.java b/src/main/java/com/mmodding/mmodding_lib/library/utils/InternalOf.java new file mode 100644 index 0000000..c2b6b17 --- /dev/null +++ b/src/main/java/com/mmodding/mmodding_lib/library/utils/InternalOf.java @@ -0,0 +1,16 @@ +package com.mmodding.mmodding_lib.library.utils; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * This annotation represents a class, a method or a field which is + */ +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.TYPE, ElementType.TYPE_USE, ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.PACKAGE}) +public @interface InternalOf { + + Class[] targets(); +} diff --git a/src/main/java/com/mmodding/mmodding_lib/mixin/injectors/ItemMixin.java b/src/main/java/com/mmodding/mmodding_lib/mixin/injectors/ItemMixin.java index 9c5b6c9..c4c71e4 100644 --- a/src/main/java/com/mmodding/mmodding_lib/mixin/injectors/ItemMixin.java +++ b/src/main/java/com/mmodding/mmodding_lib/mixin/injectors/ItemMixin.java @@ -35,7 +35,7 @@ public abstract class ItemMixin implements ItemGlintPack, Self { @Inject(method = "hasGlint", at = @At("TAIL"), cancellable = true) private void hasGlint(ItemStack stack, CallbackInfoReturnable cir) { - if (AdvancedItemSettings.GLINT.get(this.getObject())) { + if (AdvancedItemSettings.GLINT.get(this.getObject()).apply(stack)) { cir.setReturnValue(true); } }