diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d409c8..dd61b12 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,4 +5,3 @@ Changelog First Ogden version! Keep in mind that many things are still in development. - Added default bullets that can be illuminated and adapted to multiple ammunition types - Added ammunition configuration -- Added firearm ammunition configuration diff --git a/README.md b/README.md index e6f3c14..7587487 100644 --- a/README.md +++ b/README.md @@ -8,14 +8,13 @@ More to come... - Firearm data packing - Firearm action script - - Key handling - Boilerplate - Start of action - End of action - NBT modification - NBT querying - Functions - - Firearm action properties + - Firearm action properties - Boilerplate - Firearm damage properties - Boilerplate @@ -41,7 +40,3 @@ More to come... - Damage mods compat - First Aid - - -- Documentation for content creation - diff --git a/Scripting-Documentation.md b/Scripting-Documentation.md deleted file mode 100644 index 0071485..0000000 --- a/Scripting-Documentation.md +++ /dev/null @@ -1,192 +0,0 @@ -_NOTE: this is temporary. A move to a Github wiki is planned for the future._ - -### Notation -`(A)` - Array \ -`(Z)` - Boolean \ -`(D)` - Decimal (double/float) \ -`(I)` - Integer \ -`(O)` - Object \ -`(S)` - String \ -`(*)` - Varies - ---- - -### Script formatting -__Main script__ -``` -The firearm action script (script_type: "main") -├── parent (S) - A parent script from which to inherit states. -├── equip_state (S) - The state visited on equipping the item. This must be a valid state in *states*. Child script -│ states are invalid. If *parent* is present, this value is not necessary; if specified, it overrides the parent -│ *equip_state*. -└── states (O) - A key-value map of firearm states. If *parent* is present, any overla - └── "" (S) - A state object. - ├── location (S) - A resource location pointing to the script file. - └── instructions (A) - A list of instruction objects to execute each tick during the state. Each - instruction listed is executed sequentially. - └── (O) An instruction object. -``` - -__State script__ -``` -The state script -└── instructions (A) - A list of instruction objects to execute each tick during the state. Each - instruction listed is executed sequentially. - └── (O) An instruction object. -``` - ---- - -### Instruction object format -``` -Instruction object -├── instruction (S) - An id referring to a firearm script instruction type. Must be a registered instruction type. -└── args (O) - An object with instruction arguments passed to the instruction constructor. - └── "" (*) - A value to be passed to the instruction. Nested instruction return values are also supported. -``` - ---- - -### Default instructions - -__Mathematical operators__ \ -`ogden:add` \ -`ogden:subtract` \ -`ogden:multiply` \ -`ogden:divide` \ -`ogden:divide_int` \ -`ogden:modulo` \ -`ogden:square` \ -`ogden:cube` \ -`ogden:pow` \ -`ogden:square_root` \ -`ogden:cube_root` \ -`ogden:nth_root` \ -`ogden:exp` \ -`ogden:ln` \ -`ogden:log10` \ -`ogden:log_ab` \ -`ogden:floor` \ -`ogden:ceil` \ -`ogden:round` \ -`ogden:abs` - -__Random operations__ \ -`ogden:random_int` \ -`ogden:random_float` \ -`ogden:random_boolean` \ -`ogden:random_gaussian` - -__Bitwise operators__ \ -`ogden:bitwise_not` \ -`ogden:bitwise_or` \ -`ogden:bitwise_and` \ -`ogden:bitwise_xor` \ -`ogden:bitwise_left_shift` \ -`ogden:bitwise_right_shift` - -__Logical boolean operators__ \ -`ogden:not` \ -`ogden:or` \ -`ogden:and` \ -`ogden:equals` \ -`ogden:inequals` \ -`ogden:less_than` \ -`ogden:less_than_or_equal_to` \ -`ogden:greater_than` \ -`ogden:greater_than_or_equal_to` - -__String operations__ \ -`ogden:insert_string` \ -`ogden:substring` - -__List operations__ \ -`ogden:create_list` \ -`ogden:remove_list_elements` \ -`ogden:add_list_element` \ -`ogden:add_list_to_list` \ -`ogden:sub_list` \ -`ogden:list_length` - -__Object operations__ \ -`ogden:create_object` \ -`ogden:set_object_property` \ -`ogden:get_object_property` \ -`ogden:remove_object_property` - -__Item stack operations__ \ -`ogden:create_item_stack` \ -`ogden:copy_item_stack` \ -`ogden:set_item_stack_count` \ -`ogden:shrink_item_stack` \ -`ogden:grow_item_stack` \ -`ogden:split_item_stack` \ -`ogden:set_item_stack_damage` \ -`ogden:get_item_stack_item` \ -`ogden:get_item_stack_count` \ -`ogden:get_item_stack_damage` \ -`ogden:is_same_item` - -__Control structures__ \ -`ogden:if` \ -`ogden:for` \ -`ogden:while` - -__Variables and data__ \ -`ogden:set_variable` \ -`ogden:get_variable` \ -`ogden:get_property` - -__State actions__ \ -`ogden:change_state` \ -`ogden:has_action_time_passed` \ -`ogden:sync_animation` \ -`ogden:update_action_timer` - -__Ammunition management__ \ -`ogden:count_available_ammo` \ -`ogden:get_matching_ammo` - -__Input__ \ -`ogden:input_down` \ -`ogden:input_up` \ -`ogden:input_pressed` - -__Combat__ \ -`ogden:spawn_bullet` \ -`ogden:damage_item` \ -`ogden:melee_swing` - -__Camera manipulation__ \ -`ogden:sway_camera` \ -`ogden:recoil_camera` \ -`ogden:shake_camera` - -__NBT modification__ \ -`ogden:add_index_to_path` \ -`ogden:remove_nbt` \ -`ogden:copy_nbt` \ -`ogden:merge_nbt` \ -`ogden:insert_nbt` \ -`ogden:get_nbt_int` \ -`ogden:get_nbt_byte` \ -`ogden:get_nbt_short` \ -`ogden:get_nbt_long` \ -`ogden:get_nbt_float` \ -`ogden:get_nbt_double` \ -`ogden:get_nbt_boolean` \ -`ogden:get_nbt_string` \ -`ogden:get_nbt_array` \ -`ogden:get_nbt_object` \ -`ogden:get_nbt_item_stack` \ -`ogden:set_nbt_int` \ -`ogden:set_nbt_byte` \ -`ogden:set_nbt_short` \ -`ogden:set_nbt_long` \ -`ogden:set_nbt_float` \ -`ogden:set_nbt_double` \ -`ogden:set_nbt_boolean` \ -`ogden:set_nbt_string` \ -`ogden:set_nbt_array` \ -`ogden:set_nbt_object` \ -`ogden:set_nbt_item_stack` diff --git a/settings.gradle b/settings.gradle index 65b3efe..2a5f21e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -12,7 +12,7 @@ pluginManagement { } plugins { - id 'org.gradle.toolchains.foojay-resolver-convention' version '0.8.0' + id 'org.gradle.toolchains.foojay-resolver-convention' version '0.7.0' } rootProject.name = 'Ogden' diff --git a/src/main/java/rbasamoyai/ogden/OgdenClient.java b/src/main/java/rbasamoyai/ogden/OgdenClient.java index 4cad3b0..6097ef9 100644 --- a/src/main/java/rbasamoyai/ogden/OgdenClient.java +++ b/src/main/java/rbasamoyai/ogden/OgdenClient.java @@ -1,10 +1,6 @@ package rbasamoyai.ogden; -import net.minecraft.client.Minecraft; -import net.minecraft.world.InteractionHand; -import net.minecraft.world.item.ItemStack; import net.minecraftforge.client.event.EntityRenderersEvent; -import net.minecraftforge.client.event.InputEvent; import net.minecraftforge.eventbus.api.IEventBus; import rbasamoyai.ogden.entities.OgdenBulletRenderer; import rbasamoyai.ogden.index.OgdenEntityTypes; @@ -13,20 +9,10 @@ public class OgdenClient { public static void init(IEventBus modBus, IEventBus forgeBus) { modBus.addListener(OgdenClient::setupRenderers); - - forgeBus.addListener(OgdenClient::onInput); } public static void setupRenderers(final EntityRenderersEvent.RegisterRenderers evt) { evt.registerEntityRenderer(OgdenEntityTypes.BULLET.get(), OgdenBulletRenderer::new); } - public static void onInput(final InputEvent evt) { - Minecraft mc = Minecraft.getInstance(); - if (mc.player == null) return; - - ItemStack stack = mc.player.getUseItem(); - InteractionHand hand = mc.player.getUsedItemHand(); - } - } diff --git a/src/main/java/rbasamoyai/ogden/OgdenMod.java b/src/main/java/rbasamoyai/ogden/OgdenMod.java index 824acdb..fcb099b 100644 --- a/src/main/java/rbasamoyai/ogden/OgdenMod.java +++ b/src/main/java/rbasamoyai/ogden/OgdenMod.java @@ -7,39 +7,24 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerPlayer; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.monster.Monster; -import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.CreativeModeTab; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.LevelAccessor; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.AddReloadListenerEvent; import net.minecraftforge.event.OnDatapackSyncEvent; -import net.minecraftforge.event.entity.living.LivingGetProjectileEvent; -import net.minecraftforge.event.world.WorldEvent; import net.minecraftforge.eventbus.api.IEventBus; import net.minecraftforge.fml.DistExecutor; import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.event.config.ModConfigEvent; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; -import net.minecraftforge.registries.NewRegistryEvent; import rbasamoyai.ogden.ammunition.AmmunitionPropertiesHandler; -import rbasamoyai.ogden.ammunition.clips.ClipPropertiesHandler; -import rbasamoyai.ogden.firearms.OgdenFirearmItem; -import rbasamoyai.ogden.firearms.config.FirearmAcceptedAmmunitionHandler; -import rbasamoyai.ogden.firearms.config.FirearmAmmoPredicate; -import rbasamoyai.ogden.firearms.scripting.FirearmScriptHandler; import rbasamoyai.ogden.index.OgdenAmmoGroup; import rbasamoyai.ogden.index.OgdenBaseGroup; import rbasamoyai.ogden.index.OgdenEntityTypes; import rbasamoyai.ogden.index.OgdenFirearmsGroup; import rbasamoyai.ogden.index.OgdenItems; import rbasamoyai.ogden.index.OgdenProjectilePropertiesRegistry; -import rbasamoyai.ogden.index.OgdenRegistries; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; import rbasamoyai.ogden.network.OgdenNetwork; @Mod(OgdenMod.MOD_ID) @@ -57,15 +42,12 @@ public OgdenMod() { IEventBus modBus = FMLJavaModLoadingContext.get().getModEventBus(); IEventBus forgeBus = MinecraftForge.EVENT_BUS; - modBus.addListener(this::onNewRegistry); modBus.addListener(this::commonSetup); modBus.addListener(this::onLoadConfig); modBus.addListener(this::onReloadConfig); forgeBus.addListener(this::registerResourceListeners); forgeBus.addListener(this::onDatapackSync); - forgeBus.addListener(this::onLoadLevel); - forgeBus.addListener(this::getProjectileItem); OgdenConfigs.registerConfigs(); @@ -75,12 +57,6 @@ public OgdenMod() { DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> OgdenClient.init(modBus, forgeBus)); } - private void onNewRegistry(final NewRegistryEvent evt) { - OgdenRegistries.initRegistries(); - - OgdenScriptInstructionTypes.register(); - } - private void commonSetup(final FMLCommonSetupEvent evt) { evt.enqueueWork(() -> { OgdenNetwork.init(); @@ -90,36 +66,15 @@ private void commonSetup(final FMLCommonSetupEvent evt) { private void registerResourceListeners(final AddReloadListenerEvent evt) { evt.addListener(AmmunitionPropertiesHandler.ReloadListener.INSTANCE); - evt.addListener(ClipPropertiesHandler.AcceptedAmmoReloadListener.INSTANCE); - evt.addListener(ClipPropertiesHandler.CapacityReloadListener.INSTANCE); - evt.addListener(FirearmAcceptedAmmunitionHandler.ReloadListener.INSTANCE); - evt.addListener(FirearmScriptHandler.ReloadListener.INSTANCE);} - - public void loadTags() { - ClipPropertiesHandler.loadTags(); - FirearmAcceptedAmmunitionHandler.loadTags(); } public void onDatapackSync(final OnDatapackSyncEvent evt) { if (evt.getPlayer() == null) { - this.loadTags(); - MinecraftServer server = evt.getPlayerList().getServer(); - AmmunitionPropertiesHandler.syncToAll(server); - ClipPropertiesHandler.syncToAll(server); - FirearmAcceptedAmmunitionHandler.syncToAll(server); + AmmunitionPropertiesHandler.syncToServer(server); } else { ServerPlayer player = evt.getPlayer(); AmmunitionPropertiesHandler.syncToPlayer(player); - ClipPropertiesHandler.syncToPlayer(player); - FirearmAcceptedAmmunitionHandler.syncToPlayer(player); - } - } - - public void onLoadLevel(final WorldEvent.Load evt) { - LevelAccessor level = evt.getWorld(); - if (level.getServer() != null && !level.isClientSide() && level.getServer().overworld() == level) { - this.loadTags(); } } @@ -131,27 +86,6 @@ public void onReloadConfig(final ModConfigEvent.Reloading evt) { OgdenConfigs.onModConfigReload(evt.getConfig()); } - public void getProjectileItem(final LivingGetProjectileEvent evt) { - ItemStack shootable = evt.getProjectileWeaponItemStack(); - LivingEntity entity = evt.getEntityLiving(); - ItemStack projectile = evt.getProjectileItemStack(); - if (shootable.getItem() instanceof OgdenFirearmItem firearm) { - FirearmAmmoPredicate predicate = firearm.getFirearmPredicate(); - if (predicate.test(projectile)) - return; - ItemStack creativeStack = predicate.getCreativeItemStack(); - // TODO registry - if (entity instanceof Monster monster) { - evt.setProjectileItemStack(creativeStack); - return; - } - if (entity instanceof Player player) { - evt.setProjectileItemStack(player.isCreative() ? creativeStack : ItemStack.EMPTY); - return; - } - } - } - public static ResourceLocation resource(String path) { return new ResourceLocation(MOD_ID, path); } } diff --git a/src/main/java/rbasamoyai/ogden/ammunition/AmmoUtils.java b/src/main/java/rbasamoyai/ogden/ammunition/AmmoUtils.java deleted file mode 100644 index bfed19f..0000000 --- a/src/main/java/rbasamoyai/ogden/ammunition/AmmoUtils.java +++ /dev/null @@ -1,41 +0,0 @@ -package rbasamoyai.ogden.ammunition; - -import net.minecraft.world.Container; -import net.minecraft.world.SimpleContainer; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.entity.player.Player; -import rbasamoyai.ogden.firearms.config.FirearmAmmoPredicate; - -public class AmmoUtils { - - public static int countAvailableAmmo(Container container, FirearmAmmoPredicate predicate) { - int count = 0; - int sz = container.getContainerSize(); - for (int i = 0 ; i < sz; ++i) - count += predicate.countAmmo(container.getItem(i)); - return count; - } - - public static int countAvailableClips(Container container, FirearmAmmoPredicate predicate) { - int count = 0; - int sz = container.getContainerSize(); - for (int i = 0; i < sz; ++i) { - if (predicate.testClip(container.getItem(i))) - ++count; - } - return count; - } - - private static final Container EMPTY_CONTAINER = new SimpleContainer(1); - - // TODO add registry instead of hardcoding - public static Container getEntityInventory(LivingEntity entity) { - if (entity instanceof Player player) - return player.getInventory(); - return EMPTY_CONTAINER; - } - - private AmmoUtils() { - } - -} diff --git a/src/main/java/rbasamoyai/ogden/ammunition/AmmunitionPropertiesHandler.java b/src/main/java/rbasamoyai/ogden/ammunition/AmmunitionPropertiesHandler.java index dbb2bc6..637da8c 100644 --- a/src/main/java/rbasamoyai/ogden/ammunition/AmmunitionPropertiesHandler.java +++ b/src/main/java/rbasamoyai/ogden/ammunition/AmmunitionPropertiesHandler.java @@ -9,12 +9,12 @@ import com.google.gson.Gson; import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; import com.google.gson.JsonSyntaxException; import com.mojang.logging.LogUtils; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2ReferenceOpenHashMap; +import net.minecraft.core.Registry; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; @@ -27,7 +27,6 @@ import net.minecraft.world.item.ItemStack; import net.minecraftforge.network.NetworkEvent; import net.minecraftforge.network.PacketDistributor; -import rbasamoyai.ogden.base.OgdenRegistryUtils; import rbasamoyai.ogden.entities.AmmunitionPropertiesEntity; import rbasamoyai.ogden.network.OgdenNetwork; import rbasamoyai.ogden.network.StandardPacket; @@ -49,12 +48,11 @@ protected void apply(Map map, ResourceManager man PROPERTIES.clear(); for (Map.Entry entry : map.entrySet()) { + JsonElement el = entry.getValue(); + if (!el.isJsonObject()) continue; + ResourceLocation loc = entry.getKey(); try { - ResourceLocation loc = entry.getKey(); - JsonElement el = entry.getValue(); - if (!el.isJsonObject()) - throw new JsonParseException("Expected ammunition properties for '" + loc + "' to be a JSON object"); - Item item = OgdenRegistryUtils.getOptionalItemFromId(loc).orElseThrow(() -> { + Item item = Registry.ITEM.getOptional(loc).orElseThrow(() -> { return new JsonSyntaxException("Unknown item '" + loc + "'"); }); AmmunitionPropertiesSerializer ser = SERIALIZERS.get(item); @@ -71,7 +69,7 @@ protected void apply(Map map, ResourceManager man public static

> S registerSerializer( EntityType> type, Item item, S ser) { if (SERIALIZERS.containsKey(item)) - throw new IllegalStateException("Serializer for item " + OgdenRegistryUtils.getItemId(item) + " already registered"); + throw new IllegalStateException("Serializer for item " + Registry.ITEM.getKey(item) + " already registered"); SERIALIZERS.put(item, ser); return ser; } @@ -83,7 +81,7 @@ public static

entry : PROPERTIES.entrySet()) { - buf.writeResourceLocation(OgdenRegistryUtils.getItemId(entry.getKey())); + buf.writeResourceLocation(Registry.ITEM.getKey(entry.getKey())); toNetworkCasted(buf, entry.getKey(), entry.getValue()); } } @@ -95,16 +93,15 @@ private static void toNetworkCasted(FriendlyByt } public static void readBuf(FriendlyByteBuf buf) { - PROPERTIES.clear(); int sz = buf.readVarInt(); for (int i = 0; i < sz; ++i) { ResourceLocation loc = buf.readResourceLocation(); - Item item = OgdenRegistryUtils.getItemFromId(loc); + Item item = Registry.ITEM.get(loc); PROPERTIES.put(item, SERIALIZERS.get(item).fromNetwork(loc, buf)); } } - public static void syncToAll(MinecraftServer server) { + public static void syncToServer(MinecraftServer server) { OgdenNetwork.INSTANCE.send(PacketDistributor.ALL.noArg(), new ClientboundSyncAmmunitionPropertiesPacket()); } diff --git a/src/main/java/rbasamoyai/ogden/ammunition/clips/ClipPropertiesHandler.java b/src/main/java/rbasamoyai/ogden/ammunition/clips/ClipPropertiesHandler.java deleted file mode 100644 index f1f5a12..0000000 --- a/src/main/java/rbasamoyai/ogden/ammunition/clips/ClipPropertiesHandler.java +++ /dev/null @@ -1,177 +0,0 @@ -package rbasamoyai.ogden.ammunition.clips; - -import java.util.Map; -import java.util.function.Supplier; - -import javax.annotation.Nullable; - -import org.slf4j.Logger; - -import com.google.common.collect.Multimap; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; -import com.mojang.logging.LogUtils; - -import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap; -import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; -import net.minecraft.util.GsonHelper; -import net.minecraft.util.profiling.ProfilerFiller; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraftforge.network.NetworkEvent; -import net.minecraftforge.network.PacketDistributor; -import rbasamoyai.ogden.base.OgdenItemPredicate; -import rbasamoyai.ogden.base.OgdenJsonResourceReloadListener; -import rbasamoyai.ogden.base.OgdenRegistryUtils; -import rbasamoyai.ogden.network.OgdenNetwork; -import rbasamoyai.ogden.network.StandardPacket; - -public class ClipPropertiesHandler { - - private static final Map CLIP_CAPACITY = new Reference2IntOpenHashMap<>(); - private static final Map ACCEPTED_AMMO = new Reference2ObjectOpenHashMap<>(); - - public static class CapacityReloadListener extends SimpleJsonResourceReloadListener { - private static final Gson GSON = new Gson(); - private static final Logger LOGGER = LogUtils.getLogger(); - public static final CapacityReloadListener INSTANCE = new CapacityReloadListener(); - - CapacityReloadListener() { super(GSON, "clip_properties/capacity"); } - - @Override - protected void apply(Map map, ResourceManager manager, ProfilerFiller profiler) { - CLIP_CAPACITY.clear(); - - for (Map.Entry entry : map.entrySet()) { - try { - ResourceLocation loc = entry.getKey(); - JsonElement el = entry.getValue(); - if (!el.isJsonObject()) - throw new JsonParseException("Expected clip properties for '" + loc + "' to be a JSON object"); - JsonObject obj = el.getAsJsonObject(); - Item item = OgdenRegistryUtils.getOptionalItemFromId(loc).orElseThrow(() -> { - return new JsonSyntaxException("Unknown item '" + loc + "'"); - }); - CLIP_CAPACITY.put(item, GsonHelper.getAsInt(obj, "capacity")); - } catch (Exception e) { - LOGGER.error(e.toString()); - } - } - } - } - - public static int getAmmoCapacity(ItemStack itemStack) { return getAmmoCapacity(itemStack.getItem()); } - - public static int getAmmoCapacity(Item item) { return CLIP_CAPACITY.getOrDefault(item, -1); } - - public static class AcceptedAmmoReloadListener extends OgdenJsonResourceReloadListener { - private static final Gson GSON = new Gson(); - private static final Logger LOGGER = LogUtils.getLogger(); - public static final AcceptedAmmoReloadListener INSTANCE = new AcceptedAmmoReloadListener(); - - AcceptedAmmoReloadListener() { super(GSON, "clip_properties/accepted_ammunition"); } - - @Override - protected void apply(Multimap multimap, ResourceManager manager, ProfilerFiller profiler) { - ACCEPTED_AMMO.clear(); - - for (Map.Entry entry : multimap.entries()) { - try { - ResourceLocation loc = entry.getKey(); - JsonElement el = entry.getValue(); - if (!el.isJsonObject()) - throw new JsonParseException("Expected clip ammunition properties for '" + loc + "' to be a JSON object"); - JsonObject obj = el.getAsJsonObject(); - - Item item = OgdenRegistryUtils.getOptionalItemFromId(loc).orElseThrow(() -> { - return new JsonSyntaxException("Unknown item '" + loc + "'"); - }); - JsonArray arr = GsonHelper.getAsJsonArray(obj, "ammunition"); - OgdenItemPredicate pred = OgdenItemPredicate.fromJson(arr); - if (GsonHelper.getAsBoolean(obj, "replace", false) || !ACCEPTED_AMMO.containsKey(item)) { - ACCEPTED_AMMO.put(item, pred); - } else { - ACCEPTED_AMMO.put(item, ACCEPTED_AMMO.get(item).merge(pred)); - } - } catch (Exception e) { - LOGGER.error(e.toString()); - } - } - } - } - - public static OgdenItemPredicate getAmmoPredicate(ItemStack itemStack) { return getAmmoPredicate(itemStack.getItem()); } - public static OgdenItemPredicate getAmmoPredicate(Item item) { return ACCEPTED_AMMO.getOrDefault(item, OgdenItemPredicate.EMPTY); } - - public static void loadTags() { - for (OgdenItemPredicate pred : ACCEPTED_AMMO.values()) - pred.loadTags(); - } - - public static void writeBuf(FriendlyByteBuf buf) { - buf.writeVarInt(CLIP_CAPACITY.size()); - for (Map.Entry entry : CLIP_CAPACITY.entrySet()) { - buf.writeResourceLocation(OgdenRegistryUtils.getItemId(entry.getKey())) - .writeVarInt(entry.getValue()); - } - buf.writeVarInt(ACCEPTED_AMMO.size()); - for (Map.Entry entry : ACCEPTED_AMMO.entrySet()) { - buf.writeResourceLocation(OgdenRegistryUtils.getItemId(entry.getKey())); - entry.getValue().toNetwork(buf); - } - } - - public static void readBuf(FriendlyByteBuf buf) { - CLIP_CAPACITY.clear(); - int clipSz = buf.readVarInt(); - for (int i = 0; i < clipSz; ++i) { - ResourceLocation loc = buf.readResourceLocation(); - Item item = OgdenRegistryUtils.getItemFromId(loc); - CLIP_CAPACITY.put(item, buf.readVarInt()); - } - ACCEPTED_AMMO.clear(); - int ammoSz = buf.readVarInt(); - for (int i = 0; i < ammoSz; ++i) { - ResourceLocation loc = buf.readResourceLocation(); - Item item = OgdenRegistryUtils.getItemFromId(loc); - ACCEPTED_AMMO.put(item, OgdenItemPredicate.fromNetwork(buf)); - } - } - - public static void syncToAll(MinecraftServer server) { - OgdenNetwork.INSTANCE.send(PacketDistributor.ALL.noArg(), new ClientboundSyncClipPropertiesPacket()); - } - - public static void syncToPlayer(ServerPlayer player) { - OgdenNetwork.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new ClientboundSyncClipPropertiesPacket()); - } - - public record ClientboundSyncClipPropertiesPacket(@Nullable FriendlyByteBuf buf) implements StandardPacket { - public ClientboundSyncClipPropertiesPacket() { this(null); } - - public static ClientboundSyncClipPropertiesPacket copyOf(FriendlyByteBuf buf) { - return new ClientboundSyncClipPropertiesPacket(new FriendlyByteBuf(buf.copy())); - } - - @Override public void encode(FriendlyByteBuf buf) { writeBuf(buf); } - - @Override - public void handle(Supplier ctx) { - ctx.get().enqueueWork(() -> { - if (this.buf != null) readBuf(this.buf); - }); - ctx.get().setPacketHandled(true); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/ammunition/clips/IClipItem.java b/src/main/java/rbasamoyai/ogden/ammunition/clips/IClipItem.java deleted file mode 100644 index 4909bf2..0000000 --- a/src/main/java/rbasamoyai/ogden/ammunition/clips/IClipItem.java +++ /dev/null @@ -1,111 +0,0 @@ -package rbasamoyai.ogden.ammunition.clips; - -import java.util.ArrayList; -import java.util.List; - -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.Tag; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.base.OgdenItemPredicate; - -public interface IClipItem { - - IClipItem NO_OVERRIDES = new IClipItem() {}; - - default List getStoredAmmo(ItemStack itemStack) { - return getStoredAmmoBase(itemStack); - } - - default int addAmmo(ItemStack clipItemStack, ItemStack ammoItemStack, boolean simulate) { - return addAmmoBase(clipItemStack, ammoItemStack, simulate); - } - - default int addAmmo(ItemStack clipItemStack, List ammoItemStacks, boolean simulate) { - return addAmmoBase(clipItemStack, ammoItemStacks, simulate); - } - - default int getAmmoCapacity(ItemStack itemStack) { - return getAmmoCapacityBase(itemStack); - } - - default OgdenItemPredicate getAmmoPredicate(ItemStack itemStack) { - return getAmmoPredicateBase(itemStack); - } - - static List getStoredAmmoBase(ItemStack itemStack) { - CompoundTag tag = itemStack.getOrCreateTag(); - if (!tag.contains("StoredAmmo", Tag.TAG_LIST)) - tag.put("StoredAmmo", new ListTag()); - ListTag storedAmmoTag = tag.getList("StoredAmmo", Tag.TAG_COMPOUND); - List list = new ArrayList<>(); - for (int i = 0; i < storedAmmoTag.size(); ++i) - list.add(ItemStack.of(storedAmmoTag.getCompound(i))); - return list; - } - - static int addAmmoBase(ItemStack clipItemStack, ItemStack ammoItemStack, boolean simulate) { - OgdenItemPredicate ammoPredicate = clipItemStack.getItem() instanceof IClipItem iClipItem - ? iClipItem.getAmmoPredicate(clipItemStack) - : getAmmoPredicateBase(clipItemStack); - if (!ammoPredicate.test(ammoItemStack)) - return 0; - CompoundTag tag = clipItemStack.getOrCreateTag(); - if (!tag.contains("StoredAmmo", Tag.TAG_LIST)) - tag.put("StoredAmmo", new ListTag()); - ListTag storedAmmoTag = tag.getList("StoredAmmo", Tag.TAG_COMPOUND); - int ammoCapacity = asClipItem(clipItemStack).getAmmoCapacity(clipItemStack); - int tryInsertCount = Math.min(ammoItemStack.getCount(), ammoCapacity - storedAmmoTag.size()); - if (tryInsertCount < 1) - return 0; - if (!simulate) { - ItemStack split = ammoItemStack.split(tryInsertCount); - split.setCount(1); - for (int i = 0 ; i < tryInsertCount; ++i) - storedAmmoTag.add(split.save(new CompoundTag())); - } - return tryInsertCount; - } - - static int addAmmoBase(ItemStack clipItemStack, List ammoItemStacks, boolean simulate) { - CompoundTag tag = clipItemStack.getOrCreateTag(); - if (!tag.contains("StoredAmmo", Tag.TAG_LIST)) - tag.put("StoredAmmo", new ListTag()); - ListTag storedAmmoTag = tag.getList("StoredAmmo", Tag.TAG_COMPOUND); - IClipItem clip = asClipItem(clipItemStack); - int ammoCapacity = clip.getAmmoCapacity(clipItemStack); - int canInsertCount = ammoCapacity - storedAmmoTag.size(); - if (canInsertCount < 1) - return 0; - OgdenItemPredicate ammoPredicate = clip.getAmmoPredicate(clipItemStack); - int counter = 0; - for (ItemStack ammoItemStack : ammoItemStacks) { - if (!ammoPredicate.test(ammoItemStack)) - return counter; - int canInsert = Math.min(ammoItemStack.getCount(), canInsertCount - counter); - if (canInsert < 1) - return counter; - counter += canInsert; - if (!simulate) { - ItemStack split = ammoItemStack.split(canInsert); - split.setCount(1); - for (int i = 0 ; i < canInsert; ++i) - storedAmmoTag.add(split.save(new CompoundTag())); - } - } - return counter; - } - - static int getAmmoCapacityBase(ItemStack itemStack) { - return ClipPropertiesHandler.getAmmoCapacity(itemStack); - } - - static OgdenItemPredicate getAmmoPredicateBase(ItemStack itemStack) { - return ClipPropertiesHandler.getAmmoPredicate(itemStack); - } - - static IClipItem asClipItem(ItemStack itemStack) { - return itemStack.getItem() instanceof IClipItem clip ? clip : NO_OVERRIDES; - } - -} diff --git a/src/main/java/rbasamoyai/ogden/base/LazyDataLoader.java b/src/main/java/rbasamoyai/ogden/base/LazyDataLoader.java deleted file mode 100644 index 532a755..0000000 --- a/src/main/java/rbasamoyai/ogden/base/LazyDataLoader.java +++ /dev/null @@ -1,34 +0,0 @@ -package rbasamoyai.ogden.base; - -import java.util.Map; - -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; - -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import net.minecraft.resources.ResourceLocation; - -public abstract class LazyDataLoader { - - private final Map jsonLoc = new Object2ObjectOpenHashMap<>(); - private final Map objLoc = new Object2ObjectOpenHashMap<>(); - - public void holdElement(ResourceLocation loc, JSON jsonEl) { - this.jsonLoc.put(loc, jsonEl); - } - - public OBJ loadObject(ResourceLocation id) throws JsonParseException { - if (!this.objLoc.containsKey(id)) { - if (!this.jsonLoc.containsKey(id)) - throw new JsonParseException("Referenced JSON element " + id + " not loaded"); - this.objLoc.put(id, this.parseJson(this.jsonLoc.get(id), id)); - } - return this.objLoc.get(id); - } - - protected abstract OBJ parseJson(JSON jsonEl, ResourceLocation id) throws JsonParseException; - - public int getFoundElementsCount() { return this.jsonLoc.size(); } - public int getLoadedObjectsCount() { return this.objLoc.size(); } - -} diff --git a/src/main/java/rbasamoyai/ogden/base/OgdenItemPredicate.java b/src/main/java/rbasamoyai/ogden/base/OgdenItemPredicate.java deleted file mode 100644 index 4852b5a..0000000 --- a/src/main/java/rbasamoyai/ogden/base/OgdenItemPredicate.java +++ /dev/null @@ -1,118 +0,0 @@ -package rbasamoyai.ogden.base; - -import java.util.HashSet; -import java.util.Iterator; -import java.util.Optional; -import java.util.Set; -import java.util.function.Predicate; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet; -import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; -import net.minecraft.core.Holder; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.TagKey; -import net.minecraft.util.GsonHelper; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; - -public class OgdenItemPredicate implements Predicate { - - public static final OgdenItemPredicate EMPTY = new OgdenItemPredicate(Set.of(), Set.of()); - - private final Set items; - private final Set> tags; - private ItemStack modalItem = null; - - public OgdenItemPredicate(Set items, Set> tags) { - this.items = items; - this.tags = tags; - } - - public static OgdenItemPredicate fromJson(JsonArray arr) { - // Adapted from Tag$Builder#parseEntry - Set items = new ReferenceLinkedOpenHashSet<>(); - Set> tags = new HashSet<>(); - for (JsonElement el1 : arr) { - String s; - boolean flag; - if (el1.isJsonObject()) { - JsonObject obj1 = el1.getAsJsonObject(); - s = GsonHelper.getAsString(obj1, "id"); - flag = GsonHelper.getAsBoolean(obj1, "required", true); - } else { - s = GsonHelper.convertToString(el1, "id"); - flag = true; - } - if (s.startsWith("#")) { - tags.add(OgdenRegistryUtils.makeItemTag(new ResourceLocation(s.substring(1)))); - } else { - ResourceLocation loc = new ResourceLocation(s); - Optional op = OgdenRegistryUtils.getOptionalItemFromId(loc); - if (flag && op.isEmpty()) - throw new JsonParseException("Unknown item '" + loc + "'"); - op.ifPresent(items::add); - } - } - return new OgdenItemPredicate(items, tags); - } - - public OgdenItemPredicate merge(OgdenItemPredicate other) { - Set newItems = new ReferenceOpenHashSet<>(); - newItems.addAll(this.items); - newItems.addAll(other.items); - Set> newTags = new HashSet<>(this.tags); - newTags.addAll(other.tags); - return new OgdenItemPredicate(newItems, newTags); - } - - public void loadTags() { - for (TagKey tag : this.tags) { - for (Holder holder : OgdenRegistryUtils.getItemsOfTag(tag)) { - this.items.add(holder.value()); - } - } - } - - public ItemStack getModalItem() { - if (this.modalItem == null) { - Iterator iter = this.items.iterator(); - this.modalItem = iter.hasNext() ? new ItemStack(iter.next()) : ItemStack.EMPTY; - } - return this.modalItem; - } - - // Technically not necessary to write tags, but may be useful somehow for information in the future --ritchie - public void toNetwork(FriendlyByteBuf buf) { - buf.writeVarInt(this.items.size()); - for (Item item : this.items) { - buf.writeResourceLocation(OgdenRegistryUtils.getItemId(item)); - } - buf.writeVarInt(this.tags.size()); - for (TagKey tag : this.tags) { - buf.writeResourceLocation(tag.location()); - } - } - - public static OgdenItemPredicate fromNetwork(FriendlyByteBuf buf) { - Set items = new ReferenceOpenHashSet<>(); - int ammoItemsSz = buf.readVarInt(); - for (int i = 0; i < ammoItemsSz; ++i) { - items.add(OgdenRegistryUtils.getItemFromId(buf.readResourceLocation())); - } - Set> tags = new HashSet<>(); - int tagsSz = buf.readVarInt(); - for (int i = 0; i < tagsSz; ++i) { - tags.add(OgdenRegistryUtils.makeItemTag(buf.readResourceLocation())); - } - return new OgdenItemPredicate(items, tags); - } - - @Override public boolean test(ItemStack itemStack) { return this.items.contains(itemStack.getItem()); } - -} diff --git a/src/main/java/rbasamoyai/ogden/base/OgdenJsonResourceReloadListener.java b/src/main/java/rbasamoyai/ogden/base/OgdenJsonResourceReloadListener.java deleted file mode 100644 index 91417ae..0000000 --- a/src/main/java/rbasamoyai/ogden/base/OgdenJsonResourceReloadListener.java +++ /dev/null @@ -1,100 +0,0 @@ -package rbasamoyai.ogden.base; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.nio.charset.StandardCharsets; - -import org.slf4j.Logger; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonParseException; -import com.mojang.logging.LogUtils; - -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.resources.Resource; -import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.server.packs.resources.SimplePreparableReloadListener; -import net.minecraft.util.GsonHelper; -import net.minecraft.util.profiling.ProfilerFiller; - -/** - * Copied from SimpleJsonResourceReloadListener, with some tweaks to support Multimap; using similar to tag loading - */ -public abstract class OgdenJsonResourceReloadListener extends SimplePreparableReloadListener> { - - private static final Logger LOGGER = LogUtils.getLogger(); - private static final int PATH_SUFFIX_LENGTH = ".json".length(); - private final Gson gson; - private final String directory; - - public OgdenJsonResourceReloadListener(Gson gson, String directory) { - this.gson = gson; - this.directory = directory; - } - - @Override - protected Multimap prepare(ResourceManager resourceManager, ProfilerFiller profiler) { - Multimap map = HashMultimap.create(); - int i = this.directory.length() + 1; - - for(ResourceLocation loco : resourceManager.listResources(this.directory, path -> path.endsWith(".json"))) { - String string = loco.getPath(); - ResourceLocation loc2 = new ResourceLocation(loco.getNamespace(), string.substring(i, string.length() - PATH_SUFFIX_LENGTH)); - try { - Resource resource = resourceManager.getResource(loco); - try { - InputStream inputStream = resource.getInputStream(); - try { - Reader reader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); - try { - JsonElement jsonElement = GsonHelper.fromJson(this.gson, reader, JsonElement.class); - if (jsonElement != null) { - map.put(loc2, jsonElement); - } else { - LOGGER.error("Couldn't load data file {} from {} as it's null or empty", loc2, loco); - } - } catch (Throwable var17) { - try { - reader.close(); - } catch (Throwable var16) { - var17.addSuppressed(var16); - } - throw var17; - } - reader.close(); - } catch (Throwable var18) { - if (inputStream != null) { - try { - inputStream.close(); - } catch (Throwable var15) { - var18.addSuppressed(var15); - } - } - throw var18; - } - if (inputStream != null) inputStream.close(); - } catch (Throwable var19) { - if (resource != null) { - try { - resource.close(); - } catch (Throwable var14) { - var19.addSuppressed(var14); - } - } - throw var19; - } - if (resource != null) resource.close(); - } catch (IllegalArgumentException | IOException | JsonParseException var20) { - LOGGER.error("Couldn't parse data file {} from {}", loc2, loco, var20); - } - } - return map; - } - -} diff --git a/src/main/java/rbasamoyai/ogden/base/OgdenRegistryUtils.java b/src/main/java/rbasamoyai/ogden/base/OgdenRegistryUtils.java deleted file mode 100644 index a2ca0f1..0000000 --- a/src/main/java/rbasamoyai/ogden/base/OgdenRegistryUtils.java +++ /dev/null @@ -1,28 +0,0 @@ -package rbasamoyai.ogden.base; - -import java.util.Optional; - -import net.minecraft.core.Holder; -import net.minecraft.core.Registry; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.tags.TagKey; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; - -public class OgdenRegistryUtils { - - public static ResourceLocation getItemId(ItemStack stack) { return getItemId(stack.getItem()); } - - public static ResourceLocation getItemId(Item item) { return Registry.ITEM.getKey(item); } - - public static Item getItemFromId(ResourceLocation loc) { return Registry.ITEM.get(loc); } - public static Optional getOptionalItemFromId(ResourceLocation loc) { return Registry.ITEM.getOptional(loc); } - - public static Iterable> getItemsOfTag(TagKey tag) { return Registry.ITEM.getTagOrEmpty(tag); } - - public static TagKey makeItemTag(ResourceLocation loc) { return TagKey.create(Registry.ITEM_REGISTRY, loc); } - - private OgdenRegistryUtils() { - } - -} diff --git a/src/main/java/rbasamoyai/ogden/entities/OgdenBullet.java b/src/main/java/rbasamoyai/ogden/entities/OgdenBullet.java index 36e5a47..7eb6a40 100644 --- a/src/main/java/rbasamoyai/ogden/entities/OgdenBullet.java +++ b/src/main/java/rbasamoyai/ogden/entities/OgdenBullet.java @@ -4,6 +4,7 @@ import com.mojang.math.Constants; +import net.minecraft.core.Registry; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.Tag; import net.minecraft.network.syncher.EntityDataAccessor; @@ -19,11 +20,11 @@ import net.minecraft.world.item.Item; import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; +import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.Vec3; import rbasamoyai.ogden.OgdenConfigs; import rbasamoyai.ogden.OgdenMod; import rbasamoyai.ogden.ammunition.AmmunitionPropertiesHandler; -import rbasamoyai.ogden.base.OgdenRegistryUtils; import rbasamoyai.ogden.index.OgdenEntityTypes; public class OgdenBullet extends OgdenProjectile implements AmmunitionPropertiesEntity { @@ -39,18 +40,12 @@ public class OgdenBullet extends OgdenProjectile implemen public OgdenBullet(EntityType entityType, Level level) { super(entityType, level); } - public OgdenBullet(Level level, Item ammunitionItem, Item firearmItem, double posX, double posY, double posZ) { + public OgdenBullet(Level level, Item ammunitionItem, Item firearmItem) { super(OgdenEntityTypes.BULLET.get(), level); - this.setPos(posX, posY, posZ); this.ammunitionItem = ammunitionItem; this.firearmItem = firearmItem; } - public OgdenBullet(Level level, Item ammunitionItem, Item firearmItem, LivingEntity living) { - this(level, ammunitionItem, firearmItem, living.getX(), living.getEyeY() - 0.1d, living.getZ()); - this.setOwner(living); - } - @Override protected void defineSynchedData() { super.defineSynchedData(); @@ -77,15 +72,15 @@ protected void readAdditionalSaveData(CompoundTag tag) { @Override public void writeProjectileSyncData(CompoundTag tag) { super.writeProjectileSyncData(tag); - tag.putString("Bullet", OgdenRegistryUtils.getItemId(this.ammunitionItem).toString()); - tag.putString("Firearm", OgdenRegistryUtils.getItemId(this.firearmItem).toString()); + tag.putString("Bullet", Registry.ITEM.getKey(this.ammunitionItem).toString()); + tag.putString("Firearm", Registry.ITEM.getKey(this.firearmItem).toString()); } @Override public void readProjectileSyncData(CompoundTag tag) { super.readProjectileSyncData(tag); - this.ammunitionItem = tag.contains("Bullet", Tag.TAG_STRING) ? OgdenRegistryUtils.getItemFromId(new ResourceLocation(tag.getString("Bullet"))) : Items.AIR; - this.firearmItem = tag.contains("Firearm", Tag.TAG_STRING) ? OgdenRegistryUtils.getItemFromId(new ResourceLocation(tag.getString("Firearm"))) : Items.AIR; + this.ammunitionItem = tag.contains("Bullet", Tag.TAG_STRING) ? Registry.ITEM.get(new ResourceLocation(tag.getString("Bullet"))) : Items.AIR; + this.firearmItem = tag.contains("Firearm", Tag.TAG_STRING) ? Registry.ITEM.get(new ResourceLocation(tag.getString("Firearm"))) : Items.AIR; } public void setTracer(boolean tracer) { this.entityData.set(IS_TRACER, tracer); } @@ -111,9 +106,9 @@ protected void onTickRotate() { } @Override - protected void onHitEntity(Entity entity, double hitTime) { + protected void onHitEntity(Entity entity) { //if (this.getProjectileMass() <= 0) return; - if (this.isToBeRemoved()) + if (this.isRemoved()) return; if (!this.level.isClientSide) { OgdenBulletProperties properties = this.getAmmunitionProperties(); @@ -121,8 +116,15 @@ protected void onHitEntity(Entity entity, double hitTime) { DamageSource source = this.getEntityDamage(); // TODO better bullet compat for other mods -- first aid and others - Vec3 hitPos = this.position().add(this.getDeltaMovement().scale(hitTime)); - float damage = this.getDamage(entity, hitPos, hitTime); + Vec3 start = this.position(); + Vec3 end = start.add(this.getDeltaMovement()); + float w = this.getBbWidth() * 0.55f; + float h = this.getBbHeight() * 0.55f; + AABB box = entity.getBoundingBox().inflate(w, h, w); + + Vec3 hitPos = box.clip(start, end).orElse(entity.position()); + + float damage = this.getDamage(entity, hitPos); float entityHealth = entity instanceof LivingEntity living ? living.getHealth() : 0; if (properties == null || properties.ignoresEntityArmor()) entity.invulnerableTime = 0; @@ -133,12 +135,12 @@ protected void onHitEntity(Entity entity, double hitTime) { float damageResisted = damage - entityHealth; this.penetrationDamage += damage + damageResisted; if (properties == null || this.penetrationDamage >= properties.penetration()) { - this.markForFutureRemoval(); + this.discard(); } } } - protected float getDamage(Entity target, Vec3 hitPos, double hitTime) { + protected float getDamage(Entity target, Vec3 hitPos) { float damage = 0; OgdenBulletProperties properties = this.getAmmunitionProperties(); if (properties != null) damage += properties.damage(); diff --git a/src/main/java/rbasamoyai/ogden/entities/OgdenBulletRenderer.java b/src/main/java/rbasamoyai/ogden/entities/OgdenBulletRenderer.java index 4958316..fd6445d 100644 --- a/src/main/java/rbasamoyai/ogden/entities/OgdenBulletRenderer.java +++ b/src/main/java/rbasamoyai/ogden/entities/OgdenBulletRenderer.java @@ -25,28 +25,32 @@ public class OgdenBulletRenderer extends EntityRenderer { @Override public void render(OgdenBullet entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffers, int packedLight) { - Vec3 start = new Vec3(entity.xOld, entity.yOld, entity.zOld); - Vec3 diff = entity.position().subtract(start); - double dlSqr = diff.lengthSqr(); - boolean isFastButNotTeleported = 1e-4d <= dlSqr && dlSqr <= entity.getDeltaMovement().lengthSqr() * 4; - double diffLength = isFastButNotTeleported ? diff.length() : 0; - double displacement = entity.getDisplacement() - diffLength * (1 - partialTicks); - - int removalTicks = Math.max(0, entity.getRemovalTicks() - 1); - float subTick = entity.getCollisionSubTick(); - boolean hasCollided = 0 <= subTick && subTick <= 1 && partialTicks + removalTicks > subTick; boolean isTracer = entity.isTracer(); - float tickDiff = partialTicks + removalTicks - subTick; - if (hasCollided) { - if (tickDiff > subTick) return; - diffLength *= 1 - tickDiff; - } + Vec3 start = new Vec3(entity.xOld, entity.yOld, entity.zOld); + Vec3 totalDiff = entity.position().subtract(start); + if (totalDiff.lengthSqr() < 1e-4d) totalDiff = Vec3.ZERO; + double displacement = entity.getDisplacement() + totalDiff.length() * partialTicks; + boolean isTeleported = totalDiff.lengthSqr() > entity.getDeltaMovement().lengthSqr() * 4; + + float yaw = entity.getViewYRot(partialTicks); + float pitch = entity.getViewXRot(partialTicks); + Quaternion q = Vector3f.YP.rotationDegrees(yaw + 180.0f); + Quaternion q1 = Vector3f.XP.rotationDegrees(pitch); + q.mul(q1); + + poseStack.pushPose(); + poseStack.mulPose(q); + poseStack.translate(0, entity.getBbHeight() / 2, 0); + float length; if (isTracer) { - length = (float) Math.min(diffLength, displacement); + length = (float) Math.min(isTeleported ? 0 : totalDiff.length(), displacement); } else { length = 0.125f; } + PoseStack.Pose lastPose = poseStack.last(); + Matrix4f pose = lastPose.pose(); + Matrix3f normal = lastPose.normal(); int packedTracerColor = entity.getPackedTracerColor(); int r = isTracer ? (packedTracerColor >> 16) & 255 : 128; @@ -62,20 +66,6 @@ public void render(OgdenBullet entity, float entityYaw, float partialTicks, Pose float y2 = thickness; float z2 = length + thickness; - float yaw = entity.getViewYRot(partialTicks); - float pitch = entity.getViewXRot(partialTicks); - Quaternion q = Vector3f.YP.rotationDegrees(yaw + 180.0f); - Quaternion q1 = Vector3f.XP.rotationDegrees(pitch); - q.mul(q1); - - poseStack.pushPose(); - poseStack.mulPose(q); - poseStack.translate(0, entity.getBbHeight() / 2, 0); - - PoseStack.Pose lastPose = poseStack.last(); - Matrix4f pose = lastPose.pose(); - Matrix3f normal = lastPose.normal(); - VertexConsumer builder = buffers.getBuffer(QUAD); // Right diff --git a/src/main/java/rbasamoyai/ogden/entities/OgdenProjectile.java b/src/main/java/rbasamoyai/ogden/entities/OgdenProjectile.java index 192f996..789965d 100644 --- a/src/main/java/rbasamoyai/ogden/entities/OgdenProjectile.java +++ b/src/main/java/rbasamoyai/ogden/entities/OgdenProjectile.java @@ -1,16 +1,8 @@ package rbasamoyai.ogden.entities; -import java.util.Map; -import java.util.Optional; - import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; -import net.minecraft.network.syncher.EntityDataAccessor; -import net.minecraft.network.syncher.EntityDataSerializers; -import net.minecraft.network.syncher.SynchedEntityData; import net.minecraft.server.level.ServerLevel; -import net.minecraft.util.Mth; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.projectile.Projectile; @@ -32,9 +24,6 @@ public abstract class OgdenProjectile extends Projectile implements AmmunitionPropertiesEntity, PreciseProjectile { - private static final EntityDataAccessor COLLISION_SUBTICK = SynchedEntityData.defineId(OgdenProjectile.class, EntityDataSerializers.FLOAT); - private static final EntityDataAccessor REMOVE_TICK_COUNTER = SynchedEntityData.defineId(OgdenProjectile.class, EntityDataSerializers.INT); - protected double displacement = 0; protected OgdenProjectile(EntityType> entityType, Level level) { @@ -43,8 +32,6 @@ protected OgdenProjectile(EntityType> entityType, L @Override protected void defineSynchedData() { - this.entityData.define(COLLISION_SUBTICK, -1.0f); - this.entityData.define(REMOVE_TICK_COUNTER, 0); } @Override @@ -59,17 +46,12 @@ public void onAddedToWorld() { protected void addAdditionalSaveData(CompoundTag tag) { super.addAdditionalSaveData(tag); this.writeProjectileSyncData(tag); - float subTick = this.getCollisionSubTick(); - if (0 <= subTick && subTick <= 1) tag.putFloat("CollisionSubTick", this.getCollisionSubTick()); - if (this.getRemovalTicks() > 0) tag.putInt("RemovalTicks", this.getRemovalTicks()); } @Override protected void readAdditionalSaveData(CompoundTag tag) { super.readAdditionalSaveData(tag); this.readProjectileSyncData(tag); - this.setCollisionSubTick(tag.contains("CollisionSubTick", Tag.TAG_FLOAT) ? tag.getFloat("CollisionSubTick") : -1.0f); - if (tag.contains("RemovalTicks", Tag.TAG_INT)) this.entityData.set(REMOVE_TICK_COUNTER, Mth.clamp(tag.getInt("RemovalTicks"), 0, 3)); } public void writeProjectileSyncData(CompoundTag tag) { @@ -81,15 +63,6 @@ public void readProjectileSyncData(CompoundTag tag) { } public void tick() { - boolean toBeRemoved = this.isToBeRemoved(); - if (toBeRemoved) { - int removalTicks = this.getRemovalTicks(); - this.entityData.set(REMOVE_TICK_COUNTER, ++removalTicks); - if (removalTicks == 3) { - this.discard(); - return; - } - } ChunkPos cpos = new ChunkPos(this.blockPosition()); if (this.level.isClientSide || this.level.hasChunk(cpos.x, cpos.z)) { if (this.level instanceof ServerLevel slevel) { @@ -99,7 +72,7 @@ public void tick() { } super.tick(); - if (!toBeRemoved) this.clipAndDamage(); + this.clipAndDamage(); this.onTickRotate(); @@ -135,7 +108,7 @@ public double getDrag() { } public double getGravity() { - return -0.025f; // TODO: how to config this? + return -0.05f; // TODO: how to config this? } protected void clipAndDamage() { @@ -149,11 +122,11 @@ protected void clipAndDamage() { double t = 1; int MAX_ITER = 20; + boolean hitBlock = false; for (int p = 0; p < MAX_ITER; ++p) { boolean breakEarly = false; Vec3 scaledVel = vel.scale(t); - double scaledLength = scaledVel.length(); - if (scaledLength < 1e-2d) break; + if (scaledVel.lengthSqr() < 1e-4d) break; Vec3 end = start.add(scaledVel); BlockHitResult bResult = this.level.clip(new ClipContext(start, end, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this)); @@ -161,26 +134,15 @@ protected void clipAndDamage() { AABB currentMovementRegion = this.getBoundingBox().expandTowards(end.subtract(start)).inflate(1).move(start.subtract(pos)); + Vec3 finalStart = start; + Vec3 finalEnd = end; AABB thisBB = this.getBoundingBox(); for (Entity target : this.level.getEntities(this, currentMovementRegion, e -> { - return !projCtx.hasHitEntity(e) && this.canHitEntity(e); + if (projCtx.hasHitEntity(e) || !this.canHitEntity(e)) return false; + AABB bb = e.getBoundingBox(); + return bb.intersects(thisBB) || bb.inflate(reach).clip(finalStart, finalEnd).isPresent(); })) { - AABB bb = target.getBoundingBox(); - if (bb.intersects(thisBB)) { - projCtx.addEntity(target, 0); - projCtx.setFinalHitTime(0); - } else { - Optional op = bb.inflate(reach).clip(start, end); - if (op.isPresent()) { - Vec3 hitLoc = op.get(); - Vec3 disp = hitLoc.subtract(start); - double subTime = disp.length() / scaledLength; - double currentTime = 1 - t + subTime * t; - projCtx.addEntity(target, currentTime); - if (currentTime < projCtx.getFinalHitTime()) // TODO: rules on penetrating multiple entities - projCtx.setFinalHitTime(currentTime); - } - } + projCtx.addEntity(target); } Vec3 hitLoc = end; @@ -188,57 +150,33 @@ protected void clipAndDamage() { BlockPos bpos = bResult.getBlockPos().immutable(); BlockState state = this.level.getChunkAt(bpos).getBlockState(bpos); - boolean flag1 = state.isAir(); - //boolean flag1 = projCtx.getLastState().isAir(); + boolean flag1 = projCtx.getLastState().isAir(); if (!flag1) { projCtx.setLastState(state); state.onProjectileHit(this.level, state, bResult, this); breakEarly = true; + hitBlock = true; // TODO: block breaking - - Vec3 disp = hitLoc.subtract(start); - double subTime = disp.length() / scaledLength; - double currentTime = 1 - t + subTime * t; - if (currentTime < projCtx.getFinalHitTime()) - projCtx.setFinalHitTime(currentTime); } } Vec3 disp = hitLoc.subtract(start); start = hitLoc; if (this.onClip(projCtx, start)) break; if (breakEarly) break; - t -= disp.length() / scaledLength; - if (t <= 1e-2d) break; + t -= disp.length() / scaledVel.length(); + if (t < 0) break; } - double finalHitTime = projCtx.getFinalHitTime(); - for (Map.Entry entry : projCtx.hitEntities().entrySet()) { - if (entry.getValue() <= finalHitTime) - this.onHitEntity(entry.getKey(), entry.getValue()); - } - if (0 <= finalHitTime && finalHitTime <= 1) { - this.markForFutureRemoval(); - this.setCollisionSubTick((float) finalHitTime); - } - } + for (Entity e : projCtx.hitEntities()) this.onHitEntity(e); - @Override - public boolean ignoreExplosion() { - return this.isToBeRemoved() || super.ignoreExplosion(); + if (hitBlock) this.discard(); } protected boolean onClip(ProjectileContext ctx, Vec3 pos) { return false; } - protected abstract void onHitEntity(Entity entity, double hitTime); - - public void setCollisionSubTick(float subTick) { this.entityData.set(COLLISION_SUBTICK, 0 <= subTick && subTick <= 1 ? subTick : -1); } - public float getCollisionSubTick() { return this.entityData.get(COLLISION_SUBTICK); } - - protected void markForFutureRemoval() { this.entityData.set(REMOVE_TICK_COUNTER, 1); } - public boolean isToBeRemoved() { return this.isRemoved() || this.getRemovalTicks() > 0; } - public int getRemovalTicks() { return this.entityData.get(REMOVE_TICK_COUNTER); } + protected abstract void onHitEntity(Entity entity); } diff --git a/src/main/java/rbasamoyai/ogden/entities/ProjectileContext.java b/src/main/java/rbasamoyai/ogden/entities/ProjectileContext.java index 1fc0d7c..63129c0 100644 --- a/src/main/java/rbasamoyai/ogden/entities/ProjectileContext.java +++ b/src/main/java/rbasamoyai/ogden/entities/ProjectileContext.java @@ -1,8 +1,9 @@ package rbasamoyai.ogden.entities; import java.util.HashMap; -import java.util.LinkedHashMap; +import java.util.LinkedHashSet; import java.util.Map; +import java.util.Set; import net.minecraft.core.BlockPos; import net.minecraft.world.entity.Entity; @@ -14,11 +15,8 @@ public class ProjectileContext { private BlockState lastState = Blocks.AIR.defaultBlockState(); private final CollisionContext collisionContext; - private final Map hitEntities = new LinkedHashMap<>(); + private final Set hitEntities = new LinkedHashSet<>(); //private final CBCCfgMunitions.GriefState griefState; TODO: config this instead of Create Big Cannons - - private double finalHitTime = Double.POSITIVE_INFINITY; - private final Map queuedExplosions = new HashMap<>(); public ProjectileContext(OgdenProjectile projectile/*, CBCCfgMunitions.GriefState griefState*/) { @@ -31,12 +29,9 @@ public ProjectileContext(OgdenProjectile projectile/*, CBCCfgMunitions.GriefS public CollisionContext collisionContext() { return this.collisionContext; } //public CBCCfgMunitions.GriefState griefState() { return this.griefState; } - public boolean hasHitEntity(Entity entity) { return this.hitEntities.containsKey(entity); } - public void addEntity(Entity entity, double hitTime) { this.hitEntities.put(entity, hitTime); } - public Map hitEntities() { return this.hitEntities; } - - public void setFinalHitTime(double hitTime) { this.finalHitTime = hitTime; } - public double getFinalHitTime() { return this.finalHitTime; } + public boolean hasHitEntity(Entity entity) { return this.hitEntities.contains(entity); } + public void addEntity(Entity entity) { this.hitEntities.add(entity); } + public Set hitEntities() { return this.hitEntities; } public void queueExplosion(BlockPos pos, float power) { this.queuedExplosions.put(pos, power); } public Map getQueuedExplosions() { return this.queuedExplosions; } diff --git a/src/main/java/rbasamoyai/ogden/firearms/OgdenFirearmItem.java b/src/main/java/rbasamoyai/ogden/firearms/OgdenFirearmItem.java deleted file mode 100644 index 449ffec..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/OgdenFirearmItem.java +++ /dev/null @@ -1,44 +0,0 @@ -package rbasamoyai.ogden.firearms; - -import java.util.function.Predicate; - -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.ProjectileWeaponItem; -import rbasamoyai.ogden.firearms.config.FirearmAcceptedAmmunitionHandler; -import rbasamoyai.ogden.firearms.config.FirearmAmmoPredicate; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; - -public class OgdenFirearmItem extends ProjectileWeaponItem { - - public OgdenFirearmItem(Properties properties) { - super(properties); - } - - @Override - public int getDefaultProjectileRange() { - return 0; // TODO data pack config -- "ai_projectile_range" - } - - @Override public int getUseDuration(ItemStack pStack) { return 0; } - - @Override public Predicate getAllSupportedProjectiles() { return this.getFirearmPredicate(); } - - public FirearmAmmoPredicate getFirearmPredicate() { - return FirearmAcceptedAmmunitionHandler.getAmmoPredicate(this); - } - - public float getPassedActionTime(ScriptContext context) { - return 1f; - } - - public void syncFirearmAnimation(ScriptValue animationData, ScriptContext context) { - - } - - public ItemStack getCreativeAmmo(LivingEntity entity, ItemStack weapon) { - return this.getFirearmPredicate().getCreativeItemStack(); - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/config/FirearmAcceptedAmmunitionHandler.java b/src/main/java/rbasamoyai/ogden/firearms/config/FirearmAcceptedAmmunitionHandler.java deleted file mode 100644 index d569763..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/config/FirearmAcceptedAmmunitionHandler.java +++ /dev/null @@ -1,121 +0,0 @@ -package rbasamoyai.ogden.firearms.config; - -import java.util.Map; -import java.util.function.Supplier; - -import javax.annotation.Nullable; - -import org.slf4j.Logger; - -import com.google.common.collect.Multimap; -import com.google.gson.Gson; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonSyntaxException; -import com.mojang.logging.LogUtils; - -import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.level.ServerPlayer; -import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.util.GsonHelper; -import net.minecraft.util.profiling.ProfilerFiller; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraftforge.network.NetworkEvent; -import net.minecraftforge.network.PacketDistributor; -import rbasamoyai.ogden.base.OgdenJsonResourceReloadListener; -import rbasamoyai.ogden.base.OgdenRegistryUtils; -import rbasamoyai.ogden.network.OgdenNetwork; -import rbasamoyai.ogden.network.StandardPacket; - -public class FirearmAcceptedAmmunitionHandler { - - private static final Map PREDICATES = new Reference2ObjectOpenHashMap<>(); - - public static class ReloadListener extends OgdenJsonResourceReloadListener { - private static final Gson GSON = new Gson(); - private static final Logger LOGGER = LogUtils.getLogger(); - public static final ReloadListener INSTANCE = new ReloadListener(); - - ReloadListener() { super(GSON, "firearm_properties/ammunition"); } - - @Override - protected void apply(Multimap multimap, ResourceManager manager, ProfilerFiller profiler) { - PREDICATES.clear(); - - for (Map.Entry entry : multimap.entries()) { - JsonElement el = entry.getValue(); - if (!el.isJsonObject()) continue; - JsonObject obj = el.getAsJsonObject(); - ResourceLocation loc = entry.getKey(); - try { - Item item = OgdenRegistryUtils.getOptionalItemFromId(loc).orElseThrow(() -> { - return new JsonSyntaxException("Unknown item '" + loc + "'"); - }); - FirearmAmmoPredicate pred = FirearmAmmoPredicate.fromJson(obj); - if (GsonHelper.getAsBoolean(obj, "replace", false) || !PREDICATES.containsKey(item)) { - PREDICATES.put(item, pred); - } else { - PREDICATES.put(item, PREDICATES.get(item).merge(pred)); - } - } catch (Exception e) { - LOGGER.error(e.toString()); - } - } - } - } - - public static void loadTags() { - for (FirearmAmmoPredicate col : PREDICATES.values()) - col.loadTags(); - } - - public static FirearmAmmoPredicate getAmmoPredicate(ItemStack stack) { return getAmmoPredicate(stack.getItem()); } - public static FirearmAmmoPredicate getAmmoPredicate(Item item) { return PREDICATES.getOrDefault(item, FirearmAmmoPredicate.EMPTY); } - - public static void syncToAll(MinecraftServer server) { - OgdenNetwork.INSTANCE.send(PacketDistributor.ALL.noArg(), new ClientboundSyncFirearmAcceptedAmmunitionPacket()); - } - - public static void syncToPlayer(ServerPlayer player) { - OgdenNetwork.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new ClientboundSyncFirearmAcceptedAmmunitionPacket()); - } - - public static void writeBuf(FriendlyByteBuf buf) { - buf.writeVarInt(PREDICATES.size()); - for (Map.Entry entry : PREDICATES.entrySet()) { - buf.writeResourceLocation(OgdenRegistryUtils.getItemId(entry.getKey())); - entry.getValue().toNetwork(buf); - } - } - - public static void readBuf(FriendlyByteBuf buf) { - PREDICATES.clear(); - int sz = buf.readVarInt(); - for (int i = 0; i < sz; ++i) { - PREDICATES.put(OgdenRegistryUtils.getItemFromId(buf.readResourceLocation()), FirearmAmmoPredicate.fromNetwork(buf)); - } - } - - public record ClientboundSyncFirearmAcceptedAmmunitionPacket(@Nullable FriendlyByteBuf buf) implements StandardPacket { - public ClientboundSyncFirearmAcceptedAmmunitionPacket() { this(null); } - - public static ClientboundSyncFirearmAcceptedAmmunitionPacket copyOf(FriendlyByteBuf buf) { - return new ClientboundSyncFirearmAcceptedAmmunitionPacket(new FriendlyByteBuf(buf.copy())); - } - - @Override public void encode(FriendlyByteBuf buf) { writeBuf(buf); } - - @Override - public void handle(Supplier ctx) { - ctx.get().enqueueWork(() -> { - if (this.buf != null) readBuf(this.buf); - }); - ctx.get().setPacketHandled(true); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/config/FirearmAmmoPredicate.java b/src/main/java/rbasamoyai/ogden/firearms/config/FirearmAmmoPredicate.java deleted file mode 100644 index 07962e6..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/config/FirearmAmmoPredicate.java +++ /dev/null @@ -1,130 +0,0 @@ -package rbasamoyai.ogden.firearms.config; - -import java.util.List; -import java.util.function.Predicate; - -import javax.annotation.Nullable; - -import com.google.gson.JsonArray; -import com.google.gson.JsonObject; - -import net.minecraft.network.FriendlyByteBuf; -import net.minecraft.util.GsonHelper; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.ammunition.clips.IClipItem; -import rbasamoyai.ogden.base.OgdenItemPredicate; - -public class FirearmAmmoPredicate implements Predicate { - - public static final FirearmAmmoPredicate EMPTY = new FirearmAmmoPredicate(false, OgdenItemPredicate.EMPTY, OgdenItemPredicate.EMPTY); - - @Nullable private final Boolean needsClip; - private final OgdenItemPredicate ammoPredicate; - private final OgdenItemPredicate clipPredicate; - - public FirearmAmmoPredicate(@Nullable Boolean needsClip, OgdenItemPredicate ammoPredicate, OgdenItemPredicate clipPredicate) { - this.needsClip = needsClip; - this.ammoPredicate = ammoPredicate; - this.clipPredicate = clipPredicate; - } - - public static FirearmAmmoPredicate fromJson(JsonObject obj) { - Boolean needsClip = GsonHelper.isBooleanValue(obj, "needs_clip") ? GsonHelper.getAsBoolean(obj, "needs_clip") : null; - - JsonArray ammoArr = GsonHelper.getAsJsonArray(obj, "ammunition"); - OgdenItemPredicate ammoPredicate = OgdenItemPredicate.fromJson(ammoArr); - OgdenItemPredicate clipPredicate; - if (GsonHelper.isArrayNode(obj, "clips")) { - JsonArray clipArr = GsonHelper.getAsJsonArray(obj, "clips"); - clipPredicate = OgdenItemPredicate.fromJson(clipArr); - } else { - clipPredicate = OgdenItemPredicate.EMPTY; - } - return new FirearmAmmoPredicate(needsClip, ammoPredicate, clipPredicate); - } - - public FirearmAmmoPredicate merge(FirearmAmmoPredicate other) { - Boolean newNeedsClip = other.needsClip == null ? this.needsClip : other.needsClip; - OgdenItemPredicate newAmmo = this.ammoPredicate.merge(other.ammoPredicate); - OgdenItemPredicate newClip = this.clipPredicate.merge(other.clipPredicate); - return new FirearmAmmoPredicate(newNeedsClip, newAmmo, newClip); - } - - public void loadTags() { - this.ammoPredicate.loadTags(); - this.clipPredicate.loadTags(); - } - - public ItemStack getCreativeItemStack() { - ItemStack ammoStack = this.ammoPredicate.getModalItem(); - if (ammoStack.isEmpty()) - return ItemStack.EMPTY; - if (!this.needsClip()) - return ammoStack; - ItemStack clipStack = this.clipPredicate.getModalItem(); - if (clipStack.isEmpty()) - return ItemStack.EMPTY; - IClipItem clipItem = IClipItem.asClipItem(clipStack); - int capacity = clipItem.getAmmoCapacity(clipStack); - if (capacity < 1) - return ItemStack.EMPTY; - for (int i = 0; i < capacity; ++i) - clipItem.addAmmo(clipStack, ammoStack.copy(), false); - return clipStack; - } - - public void toNetwork(FriendlyByteBuf buf) { - buf.writeBoolean(this.needsClip()); - this.ammoPredicate.toNetwork(buf); - this.clipPredicate.toNetwork(buf); - } - - public static FirearmAmmoPredicate fromNetwork(FriendlyByteBuf buf) { - Boolean needsClip = buf.readBoolean(); - OgdenItemPredicate ammoPredicate = OgdenItemPredicate.fromNetwork(buf); - OgdenItemPredicate clipPredicate = OgdenItemPredicate.fromNetwork(buf); - return new FirearmAmmoPredicate(needsClip, ammoPredicate, clipPredicate); - } - - public boolean needsClip() { return this.needsClip != null && this.needsClip; } - - @Override - public boolean test(ItemStack itemStack) { - return !this.needsClip() && this.testAmmo(itemStack) || this.testClip(itemStack); - } - - public boolean testAmmo(ItemStack itemStack) { return this.ammoPredicate.test(itemStack); } - - public boolean testClip(ItemStack itemStack) { - if (!this.clipPredicate.test(itemStack)) - return false; - List clipStacks = IClipItem.asClipItem(itemStack).getStoredAmmo(itemStack); - if (clipStacks.isEmpty()) - return false; - for (ItemStack ammoStack : clipStacks) { - if (!this.testAmmo(ammoStack)) - return false; - } - return true; - } - - public int countAmmo(ItemStack itemStack) { - if (this.testAmmo(itemStack)) - return itemStack.getCount(); - if (!this.clipPredicate.test(itemStack)) - return 0; - int count = 0; - List clipStacks = IClipItem.asClipItem(itemStack).getStoredAmmo(itemStack); - if (clipStacks.isEmpty()) - return 0; - for (ItemStack ammoStack : clipStacks) { - if (this.testAmmo(ammoStack)) { - count += ammoStack.getCount(); - } else { - return 0; // Clip cannot be used if cannot insert, and thus not available - } - } - return count; - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmScript.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmScript.java deleted file mode 100644 index a2b735c..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmScript.java +++ /dev/null @@ -1,44 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import java.util.Map; - -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import rbasamoyai.ogden.firearms.OgdenFirearmItem; - -public record FirearmScript(String equipState, Map states) implements IFirearmScript { - - public void tick(LivingEntity entity, Level level, ItemStack stack) { - if (!(stack.getItem() instanceof OgdenFirearmItem firearm)) { // This should not happen! - ; // TODO log invalid item error once - return; - } - - CompoundTag tag = stack.getOrCreateTag(); - if (!tag.contains("CurrentState", CompoundTag.TAG_STRING)) - tag.putString("CurrentState", this.equipState); - String currentState = tag.getString("CurrentState"); - if (!this.states.containsKey(currentState)) { - ; // TODO log invalid state error once - if (currentState.equals(this.equipState)) { // This should not happen! - ; // TODO log no valid equip state error - } - return; - } - - FirearmState state = this.states.get(currentState); - state.tick(new ScriptContext(entity, level, stack, state)); - - String newState = tag.getString("CurrentState"); - if (!newState.equals(currentState)) { - if (!this.states.containsKey(newState)) { - ; // TODO log invalid state change error - tag.putString("CurrentState", this.equipState); - return; - } - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmScriptHandler.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmScriptHandler.java deleted file mode 100644 index 0e6c249..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmScriptHandler.java +++ /dev/null @@ -1,210 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; - -import javax.annotation.Nullable; - -import org.slf4j.Logger; - -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; -import com.google.gson.Gson; -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.mojang.logging.LogUtils; - -import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; -import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.packs.resources.ResourceManager; -import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener; -import net.minecraft.util.GsonHelper; -import net.minecraft.util.profiling.ProfilerFiller; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.base.LazyDataLoader; -import rbasamoyai.ogden.base.OgdenRegistryUtils; -import rbasamoyai.ogden.firearms.OgdenFirearmItem; -import rbasamoyai.ogden.index.OgdenRegistries; - -public class FirearmScriptHandler { - - private static final Map SCRIPTS = new Reference2ObjectOpenHashMap<>(); - - public static class ReloadListener extends SimpleJsonResourceReloadListener { - private static final Gson GSON = new Gson(); - public static final Logger LOGGER = LogUtils.getLogger(); - public static final ReloadListener INSTANCE = new ReloadListener(); - - ReloadListener() { super(GSON, "firearm_properties/scripts"); } - - @Override - protected void apply(Map map, ResourceManager manager, ProfilerFiller profiler) { - SCRIPTS.clear(); - - Map itemLocations = new Reference2ObjectOpenHashMap<>(); - Map builtInScripts = new Object2ObjectOpenHashMap<>(); - StateScriptLoader stateLoader = new StateScriptLoader(); - MainScriptLoader mainLoader = new MainScriptLoader(stateLoader, builtInScripts); - - for (Map.Entry entry : map.entrySet()) { - ResourceLocation loc = entry.getKey(); - JsonElement el = entry.getValue(); - try { - if (!el.isJsonObject()) - throw new JsonParseException("Firearm script file '" + loc + "' is not properly formatted"); - JsonObject obj = el.getAsJsonObject(); - String scriptType = GsonHelper.getAsString(obj, "script_type"); - Optional itemOp = OgdenRegistryUtils.getOptionalItemFromId(loc); - if (itemOp.isPresent()) { - Item item = itemOp.get(); - if (!(item instanceof OgdenFirearmItem)) - LOGGER.warn("Warning: item '" + loc + "' is not an Ogden firearm item and will not work with a firearm script"); - if (!scriptType.equals("main_script")) - throw new JsonParseException("Firearm script file for item " + loc + "expected to have script_type=main_script"); - itemLocations.put(item, loc); - mainLoader.holdElement(loc, obj); - } else if (scriptType.equals("main_script")) { - mainLoader.holdElement(loc, obj); - } else if (scriptType.equals("state")) { - stateLoader.holdElement(loc, obj); - } - } catch (Exception e) { - LOGGER.error("Error loading firearm script file " + loc + ".json:" + e.getMessage()); - } - } - LOGGER.debug("Found " + itemLocations.size() + " firearm item action script(s)"); - LOGGER.debug("Found " + stateLoader.getFoundElementsCount() + " firearm state script(s)"); - LOGGER.debug("Found " + mainLoader.getFoundElementsCount() + " firearm action script(s)"); - - for (Map.Entry entry : itemLocations.entrySet()) { - try { - SCRIPTS.put(entry.getKey(), mainLoader.loadObject(entry.getValue())); - } catch (Exception e) { - LOGGER.error("Error loading firearm script file " + entry.getValue() + ".json: " + e.getMessage()); - } - } - LOGGER.debug("Loaded " + SCRIPTS.size() + " firearm item script(s) and " + mainLoader.getLoadedObjectsCount() + " firearm script(s) of all types"); - } - } - - private static class MainScriptLoader extends LazyDataLoader { - private final StateScriptLoader stateLoader; - private final Map builtInScripts; - private final Multimap scriptDependents = HashMultimap.create(); - - private MainScriptLoader(StateScriptLoader stateLoader, Map builtInScripts) { - this.stateLoader = stateLoader; - this.builtInScripts = builtInScripts; - } - - @Override - public IFirearmScript loadObject(ResourceLocation id) throws JsonParseException { - return this.builtInScripts.containsKey(id) ? this.builtInScripts.get(id) : super.loadObject(id); - } - - @Override - protected IFirearmScript parseJson(JsonObject obj, ResourceLocation id) throws JsonParseException { - IFirearmScript parentScript = null; - if (obj.has("parent")) { - ResourceLocation parentLoc = new ResourceLocation(GsonHelper.getAsString(obj, "parent")); - if (this.hasCircularDependency(parentLoc, id)) - throw new JsonParseException("Circular dependency found: " + parentLoc + " depends on " + id + " while " + id + " depends on " + parentLoc); - parentScript = this.loadObject(parentLoc); - } - - String equipState = null; - Map statesMap = new HashMap<>(); - if (parentScript instanceof FirearmScript merge) { - equipState = merge.equipState(); - statesMap.putAll(merge.states()); - } else if (parentScript != null) { - return parentScript; - } - if (obj.has("equip_state")) - equipState = GsonHelper.getAsString(obj, "equip_state"); - if (equipState == null) - throw new JsonParseException("Firearm script requires 'equip_state' parameter or a parent with an 'equip_state' parameter"); - - if (obj.has("states") || statesMap.isEmpty()) { - JsonObject statesObj = GsonHelper.getAsJsonObject(obj, "states"); - for (Map.Entry entry : statesObj.entrySet()) { - String key = entry.getKey(); - JsonElement stateEl = entry.getValue(); - if (GsonHelper.isStringValue(stateEl)) { - ResourceLocation loc = new ResourceLocation(stateEl.getAsString()); - try { - statesMap.put(key, this.stateLoader.loadObject(loc).copyWithId(key)); - } catch (JsonParseException e) { - throw new JsonParseException("Invalid firearm state '" + key + "': " + e.getMessage()); - } - } else if (stateEl.isJsonObject()) { - statesMap.put(key, parseStateObject(stateEl.getAsJsonObject(), key)); - } else { - throw new JsonParseException("Invalid firearm state '" + key + "': expected string pointing to state script file or JSON object describing state"); - } - } - } - - if (!statesMap.containsKey(equipState)) - throw new JsonParseException("Firearm script 'states' parameter does not have specified 'equip_state' parameter '" + equipState + "'"); - return new FirearmScript(equipState, statesMap); - } - - private boolean hasCircularDependency(ResourceLocation parentId, ResourceLocation childId) { - if (this.scriptDependents.get(childId).contains(parentId)) - return true; - Set parentsOfParent = new HashSet<>(); - parentsOfParent.add(parentId); - for (Map.Entry entry : this.scriptDependents.entries()) { - if (entry.getValue().equals(parentId)) - parentsOfParent.add(entry.getKey()); - } - Collection childrenOfChild = this.scriptDependents.get(childId); - childrenOfChild.add(childId); - for (ResourceLocation ancestorId : parentsOfParent) { - this.scriptDependents.putAll(ancestorId, childrenOfChild); - } - return false; - } - } - - private static class StateScriptLoader extends LazyDataLoader { - @Override - protected FirearmState parseJson(JsonObject obj, ResourceLocation id) throws JsonParseException { - return parseStateObject(obj, id.toString()); - } - } - - private static FirearmState parseStateObject(JsonObject obj, String id) { - JsonArray instructionsJson = GsonHelper.getAsJsonArray(obj, "ticked_instructions"); - if (instructionsJson.isEmpty()) - throw new JsonParseException("Invalid firearm state '" + id + "' has no valid 'ticked_instructions' list"); - List instructions = new ArrayList<>(instructionsJson.size()); - for (int i = 0; i < instructionsJson.size(); ++i) { - JsonElement el = instructionsJson.get(i); - if (!el.isJsonObject()) - throw new JsonParseException("Invalid state instruction at index " + i + ": expected JSON object"); - ResourceLocation loc = new ResourceLocation(GsonHelper.getAsString(obj, "instruction")); - ScriptInstructionType type = OgdenRegistries.SCRIPT_INSTRUCTIONS.getOptional(loc).orElseThrow(() -> { - return new JsonParseException("Unknown firearm instruction '" + loc + "'"); - }); - instructions.add(type.getSerializer().deserialize(obj)); - } - return new FirearmState(id, instructions); - } - - - @Nullable public static IFirearmScript getScript(ItemStack stack) { return getScript(stack.getItem()); } - @Nullable public static IFirearmScript getScript(Item item) { return SCRIPTS.get(item); } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmState.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmState.java deleted file mode 100644 index 3713611..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/FirearmState.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import java.util.List; - -public class FirearmState { - - private final String id; - private final List tickedInstructions; - - public FirearmState(String id, List tickedInstructions) { - this.id = id; - this.tickedInstructions = tickedInstructions; - } - - public String id() { return this.id; } - - public void tick(ScriptContext context) { - for (ScriptInstruction instruction : this.tickedInstructions) { - instruction.run(context); - } - } - - public FirearmState copyWithId(String id) { return new FirearmState(id, this.tickedInstructions); } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/IFirearmScript.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/IFirearmScript.java deleted file mode 100644 index d91b2b9..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/IFirearmScript.java +++ /dev/null @@ -1,11 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; - -public interface IFirearmScript { - - void tick(LivingEntity entity, Level level, ItemStack stack); - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptContext.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptContext.java deleted file mode 100644 index 9219822..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptContext.java +++ /dev/null @@ -1,36 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import java.util.HashMap; -import java.util.Map; - -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; - -public record ScriptContext(LivingEntity entity, Level level, ItemStack stack, Map variables, Map currentFrame, - FirearmState state) { - - public ScriptContext(LivingEntity entity, Level level, ItemStack stack, FirearmState state) { - this(entity, level, stack, new HashMap<>(), new HashMap<>(), state); - } - - public ScriptValue getVariable(String id) { - if (this.currentFrame.containsKey(id)) - return this.currentFrame.get(id); - return this.variables.getOrDefault(id, ScriptValue.VOID); - } - - public ScriptValue setVariable(String id, ScriptValue value) { - if (value.isVoid()) - return ScriptValue.VOID; - this.currentFrame.put(id, value); - return value; - } - - public ScriptContext pushLocalFrame() { - Map pushFrame = new HashMap<>(this.variables); - pushFrame.putAll(this.currentFrame); - return new ScriptContext(this.entity, this.level, this.stack, pushFrame, new HashMap<>(), this.state); - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstruction.java deleted file mode 100644 index e03ecfd..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstruction.java +++ /dev/null @@ -1,5 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -public interface ScriptInstruction extends ScriptValueSupplier { - ScriptInstructionType type(); -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstructionSerializer.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstructionSerializer.java deleted file mode 100644 index cd6847e..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstructionSerializer.java +++ /dev/null @@ -1,8 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -public interface ScriptInstructionSerializer { - ScriptInstruction deserialize(JsonObject obj) throws JsonParseException; -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstructionType.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstructionType.java deleted file mode 100644 index 2034a54..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptInstructionType.java +++ /dev/null @@ -1,19 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import java.util.function.Function; - -public class ScriptInstructionType { - - private final ScriptInstructionSerializer serializer; - - public ScriptInstructionType(ScriptInstructionSerializer ser) { - this.serializer = ser; - } - - public ScriptInstructionType(Function serProv) { - this.serializer = serProv.apply(this); - } - - public ScriptInstructionSerializer getSerializer() { return this.serializer; } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptValue.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptValue.java deleted file mode 100644 index e2d47a4..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptValue.java +++ /dev/null @@ -1,213 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.gson.JsonElement; - -import net.minecraft.Util; -import net.minecraft.nbt.ByteTag; -import net.minecraft.nbt.CollectionTag; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.DoubleTag; -import net.minecraft.nbt.FloatTag; -import net.minecraft.nbt.IntTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.LongTag; -import net.minecraft.nbt.NumericTag; -import net.minecraft.nbt.ShortTag; -import net.minecraft.nbt.StringTag; -import net.minecraft.nbt.Tag; -import net.minecraft.util.GsonHelper; -import net.minecraft.world.item.ItemStack; - -public class ScriptValue implements ScriptValueSupplier { - - @Nullable private final Boolean bool; - @Nullable private final Number num; - @Nullable private final String str; - @Nullable private final ItemStack stack; - @Nullable private final List list; - @Nullable private final Map map; - - public static final ScriptValue VOID = new ScriptValue(null, null, null, null, null, null); - public static final ScriptValue TRUE = new ScriptValue(true); - public static final ScriptValue FALSE = new ScriptValue(false); - public static final ScriptValue ZERO = new ScriptValue(0); - public static final ScriptValue EMPTY_STRING = new ScriptValue(""); - public static final ScriptValue EMPTY_LIST = new ScriptValue(new ArrayList<>()); - public static final ScriptValue EMPTY_OBJECT = new ScriptValue(new HashMap<>()); - public static final ScriptValue EMPTY_ITEM_STACK = new ScriptValue(ItemStack.EMPTY); - - public static final ScriptValue[] INT_CACHE = Util.make(new ScriptValue[1152], c -> { - for (int i = -128; i < 1024; ++i) - c[i + 128] = new ScriptValue(i); - }); - - public static final ScriptValue[] BYTE_CACHE = Util.make(new ScriptValue[256], c -> { - for (int b = -128; b < 128; ++b) - c[b + 128] = new ScriptValue((byte) b); - }); - - private ScriptValue(@Nullable Boolean bool, @Nullable Number num, @Nullable String str, @Nullable ItemStack stack, - @Nullable List list, @Nullable Map map) { - this.bool = bool; - this.num = num; - this.str = str; - this.stack = stack; - this.list = list; - this.map = map; - } - - private ScriptValue(boolean bool) { this(bool, null, null, null, null, null); } - private ScriptValue(String str) { this(null, null, str, null, null, null); } - private ScriptValue(ItemStack stack) { this(null, null, null, stack, null, null); } - private ScriptValue(Number num) { this(null, num, null, null, null, null); } - - public ScriptValue(List list) { this(null, null, null, null, list, null); } - public ScriptValue(Map map) { this(null, null, null, null, null, map); } - - public static ScriptValue bool(boolean value) { return value ? TRUE : FALSE; } - public static ScriptValue string(String str) { return str.isEmpty() ? EMPTY_STRING : new ScriptValue(str); } - public static ScriptValue itemStack(ItemStack stack) { return stack.isEmpty() ? EMPTY_ITEM_STACK : new ScriptValue(stack); } - - public static ScriptValue ofInt(int i) { return -128 <= i && i < 1024 ? INT_CACHE[i + 128] : new ScriptValue(i); } - public static ScriptValue ofByte(byte b) { return BYTE_CACHE[b + 128]; } - public static ScriptValue ofShort(short s) { return -128 <= s && s < 1024 ? INT_CACHE[s + 128] : new ScriptValue(s); } - public static ScriptValue ofLong(long l) { return -128 <= l && l < 1024 ? INT_CACHE[(int) l + 128] : new ScriptValue(l); } - - public static ScriptValue ofFloat(float f) { return f == 0.0f ? ZERO : new ScriptValue(f); } - public static ScriptValue ofDouble(double d) { return d == 0.0d ? ZERO : new ScriptValue(d); } - - @Nullable public Boolean bool() { return this.bool; } - @Nullable public Number num() { return this.num; } - @Nullable public String str() { return this.str; } - @Nullable public ItemStack stack() { return this.stack; } - @Nullable public List list() { return this.list; } - @Nullable public Map map() { return this.map; } - - public boolean isVoid() { - if (this == VOID) - return true; - return this.bool == null && this.num == null && this.str == null && this.stack == null && this.list == null && this.map == null; - } - - @Override - public boolean equals(Object obj) { - if (obj == this) - return true; - if (!(obj instanceof ScriptValue other)) - return false; - - if (!Objects.equals(this.bool, other.bool)) - return false; - if (!Objects.equals(this.num, other.num)) - return false; - if (!Objects.equals(this.str, other.str)) - return false; - - if (this.stack != other.stack) { - if (this.stack == null || other.stack == null) - return false; - if (!ItemStack.isSameItemSameTags(this.stack, other.stack)) - return false; - } - - if (this.list != other.list) { - if (this.list == null || other.list == null) - return false; - if (!this.list.equals(other.list)) - return false; - } - - if (this.map == other.map) - return true; - if (this.map == null || other.map == null) - return false; - return this.map.equals(other.map); - } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { return this; } - - public Tag toNbt() { - if (this.bool != null) { - return ByteTag.valueOf(this.bool); - } else if (this.num instanceof Integer i) { - return IntTag.valueOf(i); - } else if (this.num instanceof Byte b) { - return ByteTag.valueOf(b); - } else if (this.num instanceof Short s) { - return ShortTag.valueOf(s); - } else if (this.num instanceof Long l) { - return LongTag.valueOf(l); - } else if (this.num instanceof Float f) { - return FloatTag.valueOf(f); - } else if (this.num instanceof Double d) { - return DoubleTag.valueOf(d); - } else if (this.num != null) { - return DoubleTag.valueOf(this.num.doubleValue()); - } else if (this.str != null) { - return StringTag.valueOf(this.str); - } else if (this.stack != null) { - return this.stack.isEmpty() ? ItemStack.EMPTY.save(new CompoundTag()) : this.stack.save(new CompoundTag()); - } else if (this.list != null) { - ListTag list = new ListTag(); - for (int i = 0; i < this.list.size(); ++i) - list.add(i, this.list.get(i).toNbt()); - return list; - } else if (this.map != null) { - CompoundTag tag = new CompoundTag(); - for (Map.Entry entry : this.map.entrySet()) - tag.put(entry.getKey(), entry.getValue().toNbt()); - return tag; - } else { - return new CompoundTag(); - } - } - - public static ScriptValue fromNbt(Tag tag) { - if (tag instanceof NumericTag ntag) { - return new ScriptValue(ntag.getAsNumber()); - } else if (tag instanceof CompoundTag ctag) { - Map obj = new HashMap<>(); - for (String key : ctag.getAllKeys()) { - Tag tag1 = ctag.get(key); - if (tag1 == null) // Should not happen - continue; - obj.put(key, fromNbt(tag1)); - } - return new ScriptValue(obj); - } else if (tag instanceof StringTag stag) { - return string(stag.getAsString()); - } else if (tag instanceof CollectionTag cltag) { - int sz = cltag.size(); - List list = new ArrayList<>(sz); - for (Tag tag1 : cltag) - list.add(fromNbt(tag1)); - return new ScriptValue(list); - } else { // If EndTag, should not reach here - return VOID; - } - } - - public static ScriptValue fromJson(JsonElement el) { - if (GsonHelper.isNumberValue(el)) { - return new ScriptValue(el.getAsNumber()); - } else if (GsonHelper.isStringValue(el)) { - return string(el.getAsString()); - } else if (GsonHelper.isBooleanValue(el)) { - return bool(el.getAsBoolean()); - } else { - return ScriptValue.VOID; - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptValueSupplier.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptValueSupplier.java deleted file mode 100644 index cc0f6c9..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/ScriptValueSupplier.java +++ /dev/null @@ -1,34 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.GsonHelper; -import rbasamoyai.ogden.index.OgdenRegistries; - -public interface ScriptValueSupplier { - - @Nonnull ScriptValue run(ScriptContext context); - - static ScriptValueSupplier fromJson(JsonElement el) { - if (el.isJsonObject()) { - JsonObject obj = el.getAsJsonObject(); - if (GsonHelper.isStringValue(obj, "instruction")) { - ResourceLocation loc = new ResourceLocation(GsonHelper.getAsString(obj, "instruction")); - ScriptInstructionType type = OgdenRegistries.SCRIPT_INSTRUCTIONS.getOptional(loc).orElseThrow(() -> { - return new JsonParseException("Unknown firearm instruction '" + loc + "'"); - }); - return type.getSerializer().deserialize(obj); - } else { - return ScriptValue.VOID; - } - } else { - return ScriptValue.fromJson(el); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/OneNumberOperatorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/OneNumberOperatorInstruction.java deleted file mode 100644 index 63142da..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/OneNumberOperatorInstruction.java +++ /dev/null @@ -1,54 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions; - -import java.util.function.Function; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class OneNumberOperatorInstruction implements ScriptInstruction { - - protected final ScriptValueSupplier operand; - - protected OneNumberOperatorInstruction(ScriptValueSupplier operand) { - this.operand = operand; - } - - @Nonnull - @Override - public final ScriptValue run(ScriptContext context) { - Number opRes = this.operand.run(context).num(); - if (opRes == null) { - ; // TODO: log error once - return ScriptValue.ZERO; - } - return this.operate(opRes); - } - - protected abstract ScriptValue operate(Number operator); - - public static abstract class Serializer implements ScriptInstructionSerializer { - private final Function constructor; - - protected Serializer(Function constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("value")) - throw new JsonSyntaxException("Numerical instruction missing parameter 'value'"); - ScriptValueSupplier operand = ScriptValueSupplier.fromJson(obj.get("value")); - return this.constructor.apply(operand); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/TwoNumberOperatorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/TwoNumberOperatorInstruction.java deleted file mode 100644 index d0d7322..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/TwoNumberOperatorInstruction.java +++ /dev/null @@ -1,65 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions; - -import java.util.function.BiFunction; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class TwoNumberOperatorInstruction implements ScriptInstruction { - - protected final ScriptValueSupplier left; - protected final ScriptValueSupplier right; - - protected TwoNumberOperatorInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - this.left = left; - this.right = right; - } - - @Nonnull - @Override - public final ScriptValue run(ScriptContext context) { - Number leftRes = this.left.run(context).num(); - Number rightRes = this.right.run(context).num(); - if (leftRes == null || rightRes == null) { - ; // TODO: log error once - return ScriptValue.ZERO; - } - return this.operate(leftRes, rightRes); - } - - protected abstract ScriptValue operate(Number left, Number right); - - public static abstract class Serializer implements ScriptInstructionSerializer { - private final BiFunction constructor; - - protected Serializer(BiFunction constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - String leftArg = this.getLeftKey(); - String rightArg = this.getRightKey(); - if (!obj.has(leftArg)) - throw new JsonSyntaxException("Numerical instruction missing parameter '" + leftArg + "'"); - if (!obj.has(rightArg)) - throw new JsonSyntaxException("Numerical instruction missing parameter '" + rightArg + "'"); - ScriptValueSupplier left = ScriptValueSupplier.fromJson(obj.get(leftArg)); - ScriptValueSupplier right = ScriptValueSupplier.fromJson(obj.get(rightArg)); - return this.constructor.apply(left, right); - } - - protected String getLeftKey() { return "left"; } - protected String getRightKey() { return "right"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/TwoObjectOperatorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/TwoObjectOperatorInstruction.java deleted file mode 100644 index 8da3930..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/TwoObjectOperatorInstruction.java +++ /dev/null @@ -1,61 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions; - -import java.util.function.BiFunction; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class TwoObjectOperatorInstruction implements ScriptInstruction { - - protected final ScriptValueSupplier left; - protected final ScriptValueSupplier right; - - protected TwoObjectOperatorInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - this.left = left; - this.right = right; - } - - @Nonnull - @Override - public final ScriptValue run(ScriptContext context) { - ScriptValue leftRes = this.left.run(context); - ScriptValue rightRes = this.right.run(context); - return this.operate(leftRes, rightRes, context); - } - - protected abstract ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context); - - public static abstract class Serializer implements ScriptInstructionSerializer { - private final BiFunction constructor; - - protected Serializer(BiFunction constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - String leftArg = this.getLeftKey(); - String rightArg = this.getRightKey(); - if (!obj.has(leftArg)) - throw new JsonSyntaxException("Instruction missing parameter '" + leftArg + "'"); - if (!obj.has(rightArg)) - throw new JsonSyntaxException("Instruction missing parameter '" + rightArg + "'"); - ScriptValueSupplier left = ScriptValueSupplier.fromJson(obj.get(leftArg)); - ScriptValueSupplier right = ScriptValueSupplier.fromJson(obj.get(rightArg)); - return this.constructor.apply(left, right); - } - - protected String getLeftKey() { return "left"; } - protected String getRightKey() { return "right"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/ammunition/CountAvailableAmmoInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/ammunition/CountAvailableAmmoInstruction.java deleted file mode 100644 index 85c48ef..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/ammunition/CountAvailableAmmoInstruction.java +++ /dev/null @@ -1,56 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.ammunition; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.world.Container; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.ammunition.AmmoUtils; -import rbasamoyai.ogden.firearms.config.FirearmAcceptedAmmunitionHandler; -import rbasamoyai.ogden.firearms.config.FirearmAmmoPredicate; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CountAvailableAmmoInstruction implements ScriptInstruction { - - private final ScriptValueSupplier countClips; - - public CountAvailableAmmoInstruction(ScriptValueSupplier countClips) { - this.countClips = countClips; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.COUNT_AVAILABLE_AMMO; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ItemStack stack = context.stack(); - Boolean countClipsRes = this.countClips.run(context).bool(); - boolean countClipsFlag; - if (countClipsRes == null) { - ; // TODO log error once - countClipsFlag = false; - } else { - countClipsFlag = countClipsRes; - } - FirearmAmmoPredicate predicate = FirearmAcceptedAmmunitionHandler.getAmmoPredicate(stack); - Container container = AmmoUtils.getEntityInventory(context.entity()); - return ScriptValue.ofInt(countClipsFlag ? AmmoUtils.countAvailableClips(container, predicate) : AmmoUtils.countAvailableAmmo(container, predicate)); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - ScriptValueSupplier countClips = obj.has("count_clips") ? ScriptValueSupplier.fromJson(obj.get("count_clips")) : ScriptValue.FALSE; - return new CountAvailableAmmoInstruction(countClips); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/ammunition/GetMatchingAmmoInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/ammunition/GetMatchingAmmoInstruction.java deleted file mode 100644 index 8f1463c..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/ammunition/GetMatchingAmmoInstruction.java +++ /dev/null @@ -1,32 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.ammunition; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetMatchingAmmoInstruction implements ScriptInstruction { - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_MATCHING_AMMO; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - return ScriptValue.itemStack(context.entity().getProjectile(context.stack())); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - return new GetMatchingAmmoInstruction(); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseAndInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseAndInstruction.java deleted file mode 100644 index e61b60f..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseAndInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.bitwise; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class BitwiseAndInstruction extends TwoNumberOperatorInstruction { - - public BitwiseAndInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofLong(left.longValue() & right.longValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.BITWISE_AND; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(BitwiseAndInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseLeftShiftInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseLeftShiftInstruction.java deleted file mode 100644 index c143e52..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseLeftShiftInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.bitwise; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class BitwiseLeftShiftInstruction extends TwoNumberOperatorInstruction { - - public BitwiseLeftShiftInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofLong(left.longValue() << right.longValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.BITWISE_LEFT_SHIFT; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(BitwiseLeftShiftInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseNotInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseNotInstruction.java deleted file mode 100644 index d727496..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseNotInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.bitwise; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class BitwiseNotInstruction extends OneNumberOperatorInstruction { - - public BitwiseNotInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.BITWISE_NOT; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofLong(~operator.longValue()); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(BitwiseNotInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseOrInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseOrInstruction.java deleted file mode 100644 index e6dbb74..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseOrInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.bitwise; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class BitwiseOrInstruction extends TwoNumberOperatorInstruction { - - public BitwiseOrInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofLong(left.longValue() | right.longValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.BITWISE_OR; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(BitwiseOrInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseRightShiftInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseRightShiftInstruction.java deleted file mode 100644 index 158c6b0..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseRightShiftInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.bitwise; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class BitwiseRightShiftInstruction extends TwoNumberOperatorInstruction { - - public BitwiseRightShiftInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofLong(left.longValue() >> right.longValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.BITWISE_RIGHT_SHIFT; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(BitwiseRightShiftInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseXorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseXorInstruction.java deleted file mode 100644 index 72e958a..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/bitwise/BitwiseXorInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.bitwise; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class BitwiseXorInstruction extends TwoNumberOperatorInstruction { - - public BitwiseXorInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofLong(left.longValue() ^ right.longValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.BITWISE_XOR; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(BitwiseXorInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/combat/SpawnBulletInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/combat/SpawnBulletInstruction.java deleted file mode 100644 index e5355e8..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/combat/SpawnBulletInstruction.java +++ /dev/null @@ -1,83 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.combat; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.world.entity.LivingEntity; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.Level; -import net.minecraft.world.phys.Vec3; -import rbasamoyai.ogden.entities.OgdenBullet; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SpawnBulletInstruction implements ScriptInstruction { - - private final ScriptValueSupplier itemStack; - private final ScriptValueSupplier spread; - private final ScriptValueSupplier power; - private final ScriptValueSupplier spawnAhead; - - public SpawnBulletInstruction(ScriptValueSupplier itemStack, ScriptValueSupplier spread, ScriptValueSupplier power, ScriptValueSupplier spawnAhead) { - this.itemStack = itemStack; - this.spread = spread; - this.power = power; - this.spawnAhead = spawnAhead; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SPAWN_BULLET; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ItemStack itemStackRes = this.itemStack.run(context).stack(); - if (itemStackRes == null) { - ;// TODO log error once - return ScriptValue.VOID; - } - Number powerRes = this.power.run(context).num(); - if (powerRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - Number spreadRes = this.spread.run(context).num(); - if (spreadRes == null) spreadRes = 0f; - Number aheadRes = this.spawnAhead.run(context).num(); - if (aheadRes == null) aheadRes = 1f; - - LivingEntity living = context.entity(); - Level level = context.level(); - ItemStack firearm = context.stack(); - if (!level.isClientSide) { - OgdenBullet proj = new OgdenBullet(level, itemStackRes.getItem(), firearm.getItem(), living); - Vec3 viewVec = living.getViewVector(1).scale(aheadRes.doubleValue()); - proj.setPos(proj.position().add(viewVec)); - proj.shootFromRotation(living, living.getXRot(), living.getYRot(), 0.0f, powerRes.floatValue(), spreadRes.floatValue()); - level.addFreshEntity(proj); - } - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("bullet")) - throw new JsonParseException("Spawn bullet instruction missing parameter 'bullet'"); - if (!obj.has("power")) - throw new JsonParseException("Spawn bullet instruction missing parameter 'power'"); - ScriptValueSupplier bullet = ScriptValueSupplier.fromJson(obj.get("bullet")); - ScriptValueSupplier spread = obj.has("spread") ? ScriptValueSupplier.fromJson(obj.get("spread")) : ScriptValue.ZERO; - ScriptValueSupplier power = ScriptValueSupplier.fromJson(obj.get("power")); - ScriptValueSupplier ahead = obj.has("ahead") ? ScriptValueSupplier.fromJson(obj.get("ahead")) : ScriptValue.ofFloat(1f); - return new SpawnBulletInstruction(bullet, spread, power, ahead); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/ForInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/ForInstruction.java deleted file mode 100644 index 22ba9d0..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/ForInstruction.java +++ /dev/null @@ -1,84 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.control; - -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.util.GsonHelper; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class ForInstruction implements ScriptInstruction { - - private final ScriptValueSupplier definition; - private final ScriptValueSupplier condition; - private final ScriptValueSupplier step; - private final List runFunctions; - - public ForInstruction(ScriptValueSupplier definition, ScriptValueSupplier condition, ScriptValueSupplier step, List runFunctions) { - this.definition = definition; - this.condition = condition; - this.step = step; - this.runFunctions = runFunctions; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.FOR; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - this.definition.run(context); - int HARD_LIMIT = 5000; - for (int p = 0; p < HARD_LIMIT; ++p) { - Boolean check = this.condition.run(context).bool(); - if (check == null) { - ; // TODO log error once - break; - } else if (!check) { - break; - } - ScriptContext context1 = context.pushLocalFrame(); - for (ScriptValueSupplier sup : this.runFunctions) - sup.run(context1); - this.step.run(context); - } - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("definition")) - throw new JsonParseException("For instruction missing parameter 'definition'"); - if (!obj.has("condition")) - throw new JsonParseException("For instruction missing parameter 'condition'"); - if (!obj.has("step")) - throw new JsonParseException("For instruction missing parameter 'step'"); - if (!obj.has("run")) - throw new JsonParseException("For instruction missing parameter 'run'"); - ScriptValueSupplier definition = ScriptValueSupplier.fromJson(obj.get("definition")); - ScriptValueSupplier condition = ScriptValueSupplier.fromJson(obj.get("condition")); - ScriptValueSupplier step = ScriptValueSupplier.fromJson(obj.get("step")); - List runFunctions; - if (GsonHelper.isArrayNode(obj, "run")) { - runFunctions = new ArrayList<>(); - for (JsonElement el : GsonHelper.getAsJsonArray(obj, "run")) - runFunctions.add(ScriptValueSupplier.fromJson(el)); - } else { - runFunctions = List.of(ScriptValueSupplier.fromJson(obj.get("run"))); - } - return new ForInstruction(definition, condition, step, runFunctions); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/IfInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/IfInstruction.java deleted file mode 100644 index 4978a96..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/IfInstruction.java +++ /dev/null @@ -1,122 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.control; - -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.util.GsonHelper; -import net.minecraft.util.Tuple; -import rbasamoyai.ogden.firearms.scripting.FirearmScriptHandler; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class IfInstruction implements ScriptInstruction { - - private final List>> conditionals; - private final List elseFunctions; - - public IfInstruction(List>> conditionals, List elseFunctions) { - this.conditionals = conditionals; - this.elseFunctions = elseFunctions; - } - - public IfInstruction(ScriptValueSupplier condition, List thenValue, List elseFunctions) { - this(List.of(new Tuple<>(condition, thenValue)), elseFunctions); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.IF; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - if (this.conditionals.isEmpty()) - ; // TODO: log error once - for (Tuple> t : this.conditionals) { - Boolean conditionResult = t.getA().run(context).bool(); - if (conditionResult == null) { - ; // TODO log error once - } else if (conditionResult) { - ScriptValue ret = ScriptValue.VOID; - ScriptContext context1 = context.pushLocalFrame(); - for (ScriptValueSupplier run : t.getB()) - ret = run.run(context1); - return ret; - } - } - ScriptValue ret = ScriptValue.VOID; - ScriptContext context1 = context.pushLocalFrame(); - for (ScriptValueSupplier run : this.elseFunctions) - ret = run.run(context1); - return ret; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - List elseFunctions; - if (GsonHelper.isArrayNode(obj, "else")) { - elseFunctions = new ArrayList<>(); - for (JsonElement el : GsonHelper.getAsJsonArray(obj, "else")) - elseFunctions.add(ScriptValueSupplier.fromJson(el)); - } else if (obj.has("else")) { - elseFunctions = List.of(ScriptValueSupplier.fromJson(obj.get("else"))); - } else { - elseFunctions = List.of(ScriptValue.VOID); - } - if (obj.has("condition") && obj.has("then")) { - ScriptValueSupplier condition = ScriptValueSupplier.fromJson(obj.get("condition")); - List thenFunctions; - if (GsonHelper.isArrayNode(obj, "then")) { - thenFunctions = new ArrayList<>(); - for (JsonElement el : GsonHelper.getAsJsonArray(obj, "then")) - thenFunctions.add(ScriptValueSupplier.fromJson(el)); - } else { - thenFunctions = List.of(ScriptValueSupplier.fromJson(obj.get("then"))); - } - return new IfInstruction(condition, thenFunctions, elseFunctions); - } else if (GsonHelper.isArrayNode(obj, "conditionals")) { - JsonArray conditionalArr = obj.getAsJsonArray("conditionals"); - List>> conditionals = new ArrayList<>(); - for (int i = 0; i < conditionalArr.size(); ++i) { - JsonElement el = conditionalArr.get(i); - if (!el.isJsonObject()) - throw new JsonParseException("If instruction 'conditionals' list has invalid conditional on index " + i + ": expected JSON object"); - JsonObject conditionalObj = el.getAsJsonObject(); - - if (!conditionalObj.has("condition")) - throw new JsonParseException("If instruction 'conditionals' list has invalid conditional on index " + i + ": missing 'condition' value"); - ScriptValueSupplier condition = ScriptValueSupplier.fromJson(conditionalObj.get("condition")); - - List thenFunctions; - if (GsonHelper.isArrayNode(conditionalObj, "then")) { - thenFunctions = new ArrayList<>(); - for (JsonElement el1 : GsonHelper.getAsJsonArray(conditionalObj, "then")) - thenFunctions.add(ScriptValueSupplier.fromJson(el1)); - } else if (conditionalObj.has("then")) { - thenFunctions = List.of(ScriptValueSupplier.fromJson(conditionalObj.get("then"))); - } else { - throw new JsonParseException("If instruction 'conditionals' list has invalid conditional on index " + i + ": missing 'then' value"); - } - conditionals.add(new Tuple<>(condition, thenFunctions)); - } - if (conditionals.isEmpty()) - FirearmScriptHandler.ReloadListener.LOGGER.warn("If instruction has empty 'conditionals' list and will default to else value or void"); - return new IfInstruction(conditionals, elseFunctions); - } else { - throw new JsonParseException("If instruction expected either single 'condition'/'then' pair or list 'conditionals' of 'condition'/'then' pairs"); - } - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/WhileInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/WhileInstruction.java deleted file mode 100644 index 52fd23f..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/control/WhileInstruction.java +++ /dev/null @@ -1,72 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.control; - -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.util.GsonHelper; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class WhileInstruction implements ScriptInstruction { - - private final ScriptValueSupplier condition; - private final List runFunctions; - - public WhileInstruction(ScriptValueSupplier condition, List runFunctions) { - this.condition = condition; - this.runFunctions = runFunctions; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.WHILE; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - int HARD_LIMIT = 5000; - for (int p = 0; p < HARD_LIMIT; ++p) { - Boolean check = this.condition.run(context).bool(); - if (check == null) { - ; // TODO log error once - break; - } else if (!check) { - break; - } - ScriptContext context1 = context.pushLocalFrame(); - for (ScriptValueSupplier sup : this.runFunctions) - sup.run(context1); - } - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("condition")) - throw new JsonParseException("For instruction missing parameter 'condition'"); - if (!obj.has("run")) - throw new JsonParseException("For instruction missing parameter 'run'"); - ScriptValueSupplier condition = ScriptValueSupplier.fromJson(obj.get("condition")); - List runFunctions; - if (GsonHelper.isArrayNode(obj, "run")) { - runFunctions = new ArrayList<>(); - for (JsonElement el : GsonHelper.getAsJsonArray(obj, "run")) - runFunctions.add(ScriptValueSupplier.fromJson(el)); - } else { - runFunctions = List.of(ScriptValueSupplier.fromJson(obj.get("run"))); - } - return new WhileInstruction(condition, runFunctions); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/CopyItemStackInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/CopyItemStackInstruction.java deleted file mode 100644 index 7f6b35b..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/CopyItemStackInstruction.java +++ /dev/null @@ -1,49 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CopyItemStackInstruction implements ScriptInstruction { - - private final ScriptValueSupplier item; - - public CopyItemStackInstruction(ScriptValueSupplier item) { - this.item = item; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.COPY_ITEM_STACK; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ItemStack item = this.item.run(context).stack(); - if (item == null) { - ; // TODO log error once - return ScriptValue.EMPTY_ITEM_STACK; - } - return ScriptValue.itemStack(item.copy()); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("item_stack")) - throw new JsonSyntaxException("Copy item stack instruction missing parameter 'item_stack'"); - ScriptValueSupplier item = ScriptValueSupplier.fromJson(obj.get("item_stack")); - return new CopyItemStackInstruction(item); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/CreateItemStackInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/CreateItemStackInstruction.java deleted file mode 100644 index 9eca49a..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/CreateItemStackInstruction.java +++ /dev/null @@ -1,101 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.ResourceLocationException; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.util.Mth; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.base.OgdenRegistryUtils; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CreateItemStackInstruction implements ScriptInstruction { - - private final ScriptValueSupplier item; - @Nullable private final ScriptValueSupplier count; - @Nullable private final ScriptValueSupplier damage; - @Nullable private final ScriptValueSupplier tag; - - public CreateItemStackInstruction(ScriptValueSupplier item, @Nullable ScriptValueSupplier count, @Nullable ScriptValueSupplier damage, - @Nullable ScriptValueSupplier tag) { - this.item = item; - this.count = count; - this.damage = damage; - this.tag = tag; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CREATE_ITEM_STACK; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String itemRes = this.item.run(context).str(); - if (itemRes == null) { - ; // TODO log error once; - return ScriptValue.EMPTY_ITEM_STACK; - } - ResourceLocation itemLoc; - try { - itemLoc = new ResourceLocation(itemRes); - } catch (ResourceLocationException e) { - ; // TODO log error once - return ScriptValue.EMPTY_ITEM_STACK; - } - ItemStack stack = new ItemStack(OgdenRegistryUtils.getItemFromId(itemLoc)); - if (this.count != null) { - Number countRes = this.count.run(context).num(); - if (countRes == null) { - ; // TODO log error once - } else { - int countVal = Mth.clamp(countRes.intValue(), 0, stack.getMaxStackSize()); - if (countVal == 0) - return ScriptValue.EMPTY_ITEM_STACK; - stack.setCount(countVal); - } - } - if (this.tag != null) { - ScriptValue tagRes = this.tag.run(context); - if (tagRes.map() == null) { - ; // TODO log error once - } else if (!(tagRes.toNbt() instanceof CompoundTag ctag)) { - ; // TODO log error once - } else { - stack.setTag(ctag); - } - } - if (stack.isDamageableItem() && this.damage != null) { - Number damageRes = this.damage.run(context).num(); - if (damageRes == null) { - ; // TODO log error once - } else { - stack.setDamageValue(Mth.clamp(damageRes.intValue(), 0, stack.getMaxDamage() + 1)); - } - } - return ScriptValue.itemStack(stack); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("item")) - throw new JsonParseException("Create item stack instruction missing parameter 'item'"); - ScriptValueSupplier item = ScriptValueSupplier.fromJson(obj.get("item")); - ScriptValueSupplier count = obj.has("count") ? ScriptValueSupplier.fromJson(obj.get("count")) : null; - ScriptValueSupplier damage = obj.has("damage") ? ScriptValueSupplier.fromJson(obj.get("damage")) : null; - ScriptValueSupplier tag = obj.has("tag") ? ScriptValueSupplier.fromJson(obj.get("tag")) : null; - return new CreateItemStackInstruction(item, count, damage, tag); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackCountInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackCountInstruction.java deleted file mode 100644 index f4a21e5..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackCountInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetItemStackCountInstruction extends ItemStackPropertyInstruction { - - public GetItemStackCountInstruction(ScriptValueSupplier stack) { super(stack); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_ITEM_STACK_COUNT; } - - @Override - protected ScriptValue operate(ItemStack itemStack) { - return ScriptValue.ofInt(itemStack.isEmpty() ? 0 : itemStack.getCount()); - } - - public static class Serializer extends ItemStackPropertyInstruction.Serializer { - public Serializer() { super(GetItemStackCountInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackDamageInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackDamageInstruction.java deleted file mode 100644 index 9694da5..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackDamageInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetItemStackDamageInstruction extends ItemStackPropertyInstruction { - - public GetItemStackDamageInstruction(ScriptValueSupplier stack) { super(stack); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_ITEM_STACK_DAMAGE; } - - @Override - protected ScriptValue operate(ItemStack itemStack) { - return ScriptValue.ofInt(itemStack.getDamageValue()); - } - - public static class Serializer extends ItemStackPropertyInstruction.Serializer { - public Serializer() { super(GetItemStackDamageInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackItemInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackItemInstruction.java deleted file mode 100644 index a8786e7..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GetItemStackItemInstruction.java +++ /dev/null @@ -1,26 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; -import rbasamoyai.ogden.base.OgdenRegistryUtils; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetItemStackItemInstruction extends ItemStackPropertyInstruction { - - public GetItemStackItemInstruction(ScriptValueSupplier stack) { super(stack); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_ITEM_STACK_ITEM; } - - @Override - protected ScriptValue operate(ItemStack itemStack) { - return ScriptValue.string(OgdenRegistryUtils.getItemId(itemStack.isEmpty() ? Items.AIR : itemStack.getItem()).toString()); - } - - public static class Serializer extends ItemStackPropertyInstruction.Serializer { - public Serializer() { super(GetItemStackItemInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GrowItemStackInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GrowItemStackInstruction.java deleted file mode 100644 index 5a8aba7..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/GrowItemStackInstruction.java +++ /dev/null @@ -1,26 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.util.Mth; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GrowItemStackInstruction extends ItemStackCountInstruction { - - public GrowItemStackInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GROW_ITEM_STACK; } - - @Override - protected ItemStack operate(ItemStack stack, int count) { - stack.grow(count); - stack.setCount(Mth.clamp(stack.getCount(), 0, stack.getMaxStackSize())); - return stack; - } - - public static class Serializer extends ItemStackCountInstruction.Serializer { - public Serializer() { super(GrowItemStackInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/IsSameItemInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/IsSameItemInstruction.java deleted file mode 100644 index 3d5883e..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/IsSameItemInstruction.java +++ /dev/null @@ -1,42 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class IsSameItemInstruction extends TwoObjectOperatorInstruction { - - public IsSameItemInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - super(left, right); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.IS_SAME_ITEM; } - - @Override - protected ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - ItemStack leftRes = left.run(context).stack(); - if (leftRes == null) { - ; // TODO log error once - return ScriptValue.FALSE; - } - ItemStack rightRes = right.run(context).stack(); - if (rightRes == null) { - ; // TODO log error once - return ScriptValue.FALSE; - } - Item leftItem = leftRes.isEmpty() ? Items.AIR : leftRes.getItem(); - Item rightItem = rightRes.isEmpty() ? Items.AIR : rightRes.getItem(); - return ScriptValue.bool(leftItem == rightItem); - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(IsSameItemInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackCountInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackCountInstruction.java deleted file mode 100644 index 72e548b..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackCountInstruction.java +++ /dev/null @@ -1,44 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import java.util.function.BiFunction; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; - -public abstract class ItemStackCountInstruction extends TwoObjectOperatorInstruction { - - protected ItemStackCountInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - super(left, right); - } - - @Override - protected final ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - ItemStack stack = left.stack(); - if (stack == null) { - ; // TODO log error once - return ScriptValue.EMPTY_ITEM_STACK; - } - Number count = right.num(); - if (count == null) { - ; // TODO log error once - return left; - } - ItemStack newStack = this.operate(stack, count.intValue()); - return ScriptValue.itemStack(newStack); - } - - protected abstract ItemStack operate(ItemStack stack, int count); - - public static abstract class Serializer extends TwoObjectOperatorInstruction.Serializer { - protected Serializer(BiFunction constructor) { - super(constructor); - } - @Override protected String getLeftKey() { return "item_stack"; } - @Override protected String getRightKey() { return "count"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackDamageInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackDamageInstruction.java deleted file mode 100644 index 69e0d42..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackDamageInstruction.java +++ /dev/null @@ -1,46 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import java.util.function.BiFunction; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; - -public abstract class ItemStackDamageInstruction extends TwoObjectOperatorInstruction { - - protected ItemStackDamageInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - super(left, right); - } - - @Override - protected final ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - ItemStack stack = left.stack(); - if (stack == null) { - ; // TODO log error once - return ScriptValue.EMPTY_ITEM_STACK; - } - if (!stack.isDamageableItem()) - return left; - Number damage = right.num(); - if (damage == null) { - ; // TODO log error once - return left; - } - ItemStack newStack = this.operate(stack, damage.intValue()); - return ScriptValue.itemStack(newStack); - } - - protected abstract ItemStack operate(ItemStack stack, int damage); - - public static abstract class Serializer extends TwoObjectOperatorInstruction.Serializer { - protected Serializer(BiFunction constructor) { - super(constructor); - } - @Override protected String getLeftKey() { return "item_stack"; } - @Override protected String getRightKey() { return "damage"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackPropertyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackPropertyInstruction.java deleted file mode 100644 index 6703061..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ItemStackPropertyInstruction.java +++ /dev/null @@ -1,54 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import java.util.function.Function; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class ItemStackPropertyInstruction implements ScriptInstruction { - - private final ScriptValueSupplier stack; - - protected ItemStackPropertyInstruction(ScriptValueSupplier stack) { - this.stack = stack; - } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ItemStack stackRes = this.stack.run(context).stack(); - if (stackRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - return this.operate(stackRes); - } - - protected abstract ScriptValue operate(ItemStack itemStack); - - public static abstract class Serializer implements ScriptInstructionSerializer { - private final Function constructor; - - protected Serializer(Function constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("stack")) - throw new JsonParseException("Item stack property instruction missing parameter 'stack'"); - ScriptValueSupplier stack = ScriptValueSupplier.fromJson(obj.get("stack")); - return this.constructor.apply(stack); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SetItemStackCountInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SetItemStackCountInstruction.java deleted file mode 100644 index d449bfb..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SetItemStackCountInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.util.Mth; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SetItemStackCountInstruction extends ItemStackCountInstruction { - - public SetItemStackCountInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SET_ITEM_STACK_COUNT; } - - @Override - protected ItemStack operate(ItemStack stack, int count) { - stack.setCount(Mth.clamp(count, 0, stack.getMaxStackSize())); - return stack; - } - - public static class Serializer extends ItemStackCountInstruction.Serializer { - public Serializer() { super(SetItemStackCountInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SetItemStackDamageInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SetItemStackDamageInstruction.java deleted file mode 100644 index f7ef060..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SetItemStackDamageInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.util.Mth; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SetItemStackDamageInstruction extends ItemStackDamageInstruction { - - public SetItemStackDamageInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SET_ITEM_STACK_DAMAGE; } - - @Override - protected ItemStack operate(ItemStack stack, int damage) { - stack.setDamageValue(Mth.clamp(damage, 0, stack.getMaxDamage() + 1)); - return stack; - } - - public static class Serializer extends ItemStackDamageInstruction.Serializer { - public Serializer() { super(SetItemStackDamageInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ShrinkItemStackInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ShrinkItemStackInstruction.java deleted file mode 100644 index dac0423..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/ShrinkItemStackInstruction.java +++ /dev/null @@ -1,23 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class ShrinkItemStackInstruction extends GrowItemStackInstruction { - - public ShrinkItemStackInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SHRINK_ITEM_STACK; } - - @Override - protected ItemStack operate(ItemStack stack, int count) { - return super.operate(stack, -count); - } - - public static class Serializer extends ItemStackCountInstruction.Serializer { - public Serializer() { super(ShrinkItemStackInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SplitItemStackInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SplitItemStackInstruction.java deleted file mode 100644 index 638eb6e..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/item_stack/SplitItemStackInstruction.java +++ /dev/null @@ -1,27 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.item_stack; - -import net.minecraft.util.Mth; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SplitItemStackInstruction extends ItemStackCountInstruction { - - public SplitItemStackInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SPLIT_ITEM_STACK; } - - @Override - protected ItemStack operate(ItemStack stack, int count) { - ItemStack newStack = stack.split(count); - stack.setCount(Mth.clamp(stack.getCount(), 0, stack.getMaxStackSize())); - newStack.setCount(Mth.clamp(newStack.getCount(), 0, newStack.getMaxStackSize())); - return newStack; - } - - public static class Serializer extends ItemStackCountInstruction.Serializer { - public Serializer() { super(SplitItemStackInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/AddListElementInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/AddListElementInstruction.java deleted file mode 100644 index c110fd2..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/AddListElementInstruction.java +++ /dev/null @@ -1,62 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.list; - -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class AddListElementInstruction implements ScriptInstruction { - - private final ScriptValueSupplier target; - private final ScriptValueSupplier source; - private final ScriptValueSupplier index; - - public AddListElementInstruction(ScriptValueSupplier target, ScriptValueSupplier source, ScriptValueSupplier index) { - this.target = target; - this.source = source; - this.index = index; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.ADD_LIST_ELEMENT; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ScriptValue listRes = this.target.run(context); - List listVal = listRes.list(); - if (listVal == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - Number indexRes = this.index.run(context).num(); - int indexVal = indexRes == null ? -1 : indexRes.intValue(); - indexVal = indexVal < 0 ? listVal.size() : Math.min(indexVal, listVal.size()); - listVal.add(indexVal, this.source.run(context)); - return listRes; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("target")) - throw new JsonParseException("Add list element instruction missing parameter 'target'"); - if (!obj.has("element")) - throw new JsonParseException("Add list element instruction missing parameter 'element'"); - ScriptValueSupplier target = ScriptValueSupplier.fromJson(obj.get("target")); - ScriptValueSupplier element = ScriptValueSupplier.fromJson(obj.get("element")); - ScriptValueSupplier index = obj.has("index") ? ScriptValueSupplier.fromJson(obj.get("index")) : ScriptValue.ofInt(-1); - return new AddListElementInstruction(target, element, index); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/AddListToListInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/AddListToListInstruction.java deleted file mode 100644 index 2b85336..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/AddListToListInstruction.java +++ /dev/null @@ -1,67 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.list; - -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class AddListToListInstruction implements ScriptInstruction { - - private final ScriptValueSupplier target; - private final ScriptValueSupplier source; - private final ScriptValueSupplier index; - - public AddListToListInstruction(ScriptValueSupplier target, ScriptValueSupplier source, ScriptValueSupplier index) { - this.target = target; - this.source = source; - this.index = index; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.ADD_LIST_TO_LIST; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ScriptValue targetRes = this.target.run(context); - List targetVal = targetRes.list(); - if (targetVal == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - List sourceRes = this.source.run(context).list(); - if (sourceRes == null) { - ; // TODO log error once - return targetRes; - } - Number indexRes = this.index.run(context).num(); - int indexVal = indexRes == null ? -1 : indexRes.intValue(); - indexVal = indexVal < 0 ? targetVal.size() : Math.min(indexVal, targetVal.size()); - targetVal.addAll(indexVal, sourceRes); - return targetRes; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("target")) - throw new JsonParseException("Add list to list instruction missing parameter 'target'"); - if (!obj.has("source")) - throw new JsonParseException("Add list to list instruction missing parameter 'source'"); - ScriptValueSupplier target = ScriptValueSupplier.fromJson(obj.get("target")); - ScriptValueSupplier source = ScriptValueSupplier.fromJson(obj.get("source")); - ScriptValueSupplier index = obj.has("index") ? ScriptValueSupplier.fromJson(obj.get("index")) : ScriptValue.ofInt(-1); - return new AddListToListInstruction(target, source, index); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/CreateListInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/CreateListInstruction.java deleted file mode 100644 index f96c1b8..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/CreateListInstruction.java +++ /dev/null @@ -1,52 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.list; - -import java.util.ArrayList; -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonArray; -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.util.GsonHelper; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CreateListInstruction implements ScriptInstruction { - - private final List values; - - public CreateListInstruction(List values) { - this.values = values; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CREATE_LIST; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - List list = new ArrayList<>(); - for (ScriptValueSupplier value : this.values) - list.add(value.run(context)); - return new ScriptValue(list); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - JsonArray arr = GsonHelper.getAsJsonArray(obj, "elements", new JsonArray()); - List values = new ArrayList<>(); - for (JsonElement el : arr) - values.add(ScriptValueSupplier.fromJson(el)); - return new CreateListInstruction(values); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/ListLengthInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/ListLengthInstruction.java deleted file mode 100644 index 0fa57bd..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/ListLengthInstruction.java +++ /dev/null @@ -1,50 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.list; - -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class ListLengthInstruction implements ScriptInstruction { - - private final ScriptValueSupplier list; - - public ListLengthInstruction(ScriptValueSupplier list) { - this.list = list; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.LIST_LENGTH; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ScriptValue listRes = this.list.run(context); - List listVal = listRes.list(); - if (listVal == null) { - ; // TODO log error once - return ScriptValue.ZERO; - } - return ScriptValue.ofInt(listVal.size()); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("list")) - throw new JsonParseException("Sublist instruction missing parameter 'list'"); - ScriptValueSupplier list = ScriptValueSupplier.fromJson(obj.get("list")); - return new ListLengthInstruction(list); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/RemoveListElementsInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/RemoveListElementsInstruction.java deleted file mode 100644 index 3141250..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/RemoveListElementsInstruction.java +++ /dev/null @@ -1,75 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.list; - -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RemoveListElementsInstruction implements ScriptInstruction { - - private final ScriptValueSupplier list; - private final ScriptValueSupplier start; - private final ScriptValueSupplier end; - - public RemoveListElementsInstruction(ScriptValueSupplier list, ScriptValueSupplier start, ScriptValueSupplier end) { - this.list = list; - this.start = start; - this.end = end; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.REMOVE_LIST_ELEMENTS; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ScriptValue listRes = this.list.run(context); - List listVal = listRes.list(); - if (listVal == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - - Number startRes = this.start.run(context).num(); - if (startRes == null) { - ; // TODO log error once - return listRes; - } - int si = startRes.intValue(); - si = si < 0 ? listVal.size() : Math.min(si, listVal.size()); - - Number endRes = this.end.run(context).num(); - int ei = endRes == null ? si + 1 : endRes.intValue(); - ei = ei < 0 ? listVal.size() : Math.min(ei, listVal.size()); - if (si > ei) { - ; // TODO log error once - return listRes; - } - listVal.subList(si, ei).clear(); - return listRes; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("list")) - throw new JsonParseException("Remove list elements instruction missing parameter 'list'"); - if (!obj.has("start")) - throw new JsonParseException("Remove list elements instruction missing parameter 'start'"); - ScriptValueSupplier list = ScriptValueSupplier.fromJson(obj.get("list")); - ScriptValueSupplier start = ScriptValueSupplier.fromJson(obj.get("start")); - ScriptValueSupplier end = obj.has("end") ? ScriptValueSupplier.fromJson(obj.get("end")) : ScriptValue.VOID; - return new RemoveListElementsInstruction(list, start, end); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/SublistInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/SublistInstruction.java deleted file mode 100644 index ed5e758..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/list/SublistInstruction.java +++ /dev/null @@ -1,81 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.list; - -import java.util.List; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.util.Mth; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SublistInstruction implements ScriptInstruction { - - private final ScriptValueSupplier list; - private final ScriptValueSupplier start; - private final ScriptValueSupplier end; - - public SublistInstruction(ScriptValueSupplier list, ScriptValueSupplier start, ScriptValueSupplier end) { - this.list = list; - this.start = start; - this.end = end; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SUBLIST; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ScriptValue listRes = this.list.run(context); - List listVal = listRes.list(); - if (listVal == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - - Number startRes = this.start.run(context).num(); - if (startRes == null) { - ; // TODO log error once - return listRes; - } - int si = Mth.clamp(startRes.intValue(), 0, listVal.size()); - - Number endRes = this.end.run(context).num(); - if (endRes == null) { - ; // TODO log error once - return listRes; - } - int ei = Mth.clamp(endRes.intValue(), 0, listVal.size()); - - if (si > ei) { - ; // TODO log error once - return listRes; - } - listVal.subList(si, ei).clear(); - return listRes; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("list")) - throw new JsonParseException("Sublist instruction missing parameter 'list'"); - if (!obj.has("start")) - throw new JsonParseException("Sublist instruction missing parameter 'start'"); - if (!obj.has("end")) - throw new JsonParseException("Sublist instruction missing parameter 'end'"); - ScriptValueSupplier list = ScriptValueSupplier.fromJson(obj.get("list")); - ScriptValueSupplier start = ScriptValueSupplier.fromJson(obj.get("start")); - ScriptValueSupplier end = ScriptValueSupplier.fromJson(obj.get("end")); - return new SublistInstruction(list, start, end); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/AndOperatorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/AndOperatorInstruction.java deleted file mode 100644 index 4727e78..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/AndOperatorInstruction.java +++ /dev/null @@ -1,19 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class AndOperatorInstruction extends TwoBooleanOperatorInstruction { - - public AndOperatorInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.AND; } - - @Override protected boolean operate(boolean left, boolean right) { return left && right; } - - public static class Serializer extends TwoBooleanOperatorInstruction.Serializer { - public Serializer() { super(AndOperatorInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/EqualsInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/EqualsInstruction.java deleted file mode 100644 index d91cdd7..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/EqualsInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class EqualsInstruction extends TwoObjectOperatorInstruction { - - public EqualsInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.EQUALS; } - - @Override - protected ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - return ScriptValue.bool(left.equals(right)); - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(EqualsInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/GreaterThanInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/GreaterThanInstruction.java deleted file mode 100644 index 60b265b..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/GreaterThanInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GreaterThanInstruction extends TwoNumberOperatorInstruction { - - public GreaterThanInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.bool(left.doubleValue() > right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GREATER_THAN; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(GreaterThanInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/GreaterThanOrEqualToInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/GreaterThanOrEqualToInstruction.java deleted file mode 100644 index c8263ec..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/GreaterThanOrEqualToInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GreaterThanOrEqualToInstruction extends TwoNumberOperatorInstruction { - - public GreaterThanOrEqualToInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.bool(left.doubleValue() >= right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GREATER_THAN_OR_EQUAL_TO; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(GreaterThanOrEqualToInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/LessThanInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/LessThanInstruction.java deleted file mode 100644 index 81181d0..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/LessThanInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class LessThanInstruction extends TwoNumberOperatorInstruction { - - public LessThanInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.bool(left.doubleValue() < right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.LESS_THAN; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(LessThanInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/LessThanOrEqualToInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/LessThanOrEqualToInstruction.java deleted file mode 100644 index 89335b1..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/LessThanOrEqualToInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class LessThanOrEqualToInstruction extends TwoNumberOperatorInstruction { - - public LessThanOrEqualToInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.bool(left.doubleValue() <= right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.LESS_THAN_OR_EQUAL_TO; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(LessThanOrEqualToInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/NotInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/NotInstruction.java deleted file mode 100644 index 93b30a0..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/NotInstruction.java +++ /dev/null @@ -1,48 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class NotInstruction implements ScriptInstruction { - - private final ScriptValueSupplier operand; - - public NotInstruction(ScriptValueSupplier operand) { - this.operand = operand; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.NOT; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - Boolean opRes = this.operand.run(context).bool(); - if (opRes == null) { - ; // TODO: log error once - return ScriptValue.FALSE; - } - return ScriptValue.bool(!opRes); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("value")) - throw new JsonSyntaxException("Logical instruction missing parameter 'value'"); - ScriptValueSupplier operand = ScriptValueSupplier.fromJson(obj.get("value")); - return new NotInstruction(operand); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/OrInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/OrInstruction.java deleted file mode 100644 index f68c79d..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/OrInstruction.java +++ /dev/null @@ -1,19 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class OrInstruction extends TwoBooleanOperatorInstruction { - - public OrInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.OR; } - - @Override protected boolean operate(boolean left, boolean right) { return left || right; } - - public static class Serializer extends TwoBooleanOperatorInstruction.Serializer { - public Serializer() { super(OrInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/TwoBooleanOperatorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/TwoBooleanOperatorInstruction.java deleted file mode 100644 index bfe4be7..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/TwoBooleanOperatorInstruction.java +++ /dev/null @@ -1,39 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import java.util.function.BiFunction; - -import javax.annotation.Nonnull; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; - -public abstract class TwoBooleanOperatorInstruction extends TwoObjectOperatorInstruction { - - protected TwoBooleanOperatorInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - super(left, right); - } - - @Nonnull - @Override - public final ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - Boolean leftRes = this.left.run(context).bool(); - Boolean rightRes = this.right.run(context).bool(); - if (leftRes == null || rightRes == null) { - ; // TODO: log error once - return ScriptValue.FALSE; - } - return ScriptValue.bool(this.operate(leftRes, rightRes)); - } - - protected abstract boolean operate(boolean left, boolean right); - - public static abstract class Serializer extends TwoObjectOperatorInstruction.Serializer { - protected Serializer(BiFunction constructor) { - super(constructor); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/UnequalsInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/UnequalsInstruction.java deleted file mode 100644 index ac4c288..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/logical/UnequalsInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.logical; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class UnequalsInstruction extends TwoObjectOperatorInstruction { - - public UnequalsInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.UNEQUAL; } - - @Override - protected ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - return ScriptValue.bool(!left.equals(right)); - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(UnequalsInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/AbsInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/AbsInstruction.java deleted file mode 100644 index 78640cc..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/AbsInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class AbsInstruction extends OneNumberOperatorInstruction { - - public AbsInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.ABS; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.abs(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(AbsInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/AddInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/AddInstruction.java deleted file mode 100644 index ba7d8cd..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/AddInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class AddInstruction extends TwoNumberOperatorInstruction { - - public AddInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofDouble(left.doubleValue() + right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.ADD; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(AddInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CeilInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CeilInstruction.java deleted file mode 100644 index 51a39f4..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CeilInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CeilInstruction extends OneNumberOperatorInstruction { - - public CeilInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CEIL; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.ceil(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(CeilInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CubeInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CubeInstruction.java deleted file mode 100644 index 4719d70..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CubeInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CubeInstruction extends OneNumberOperatorInstruction { - - public CubeInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CUBE; } - - @Override - protected ScriptValue operate(Number operator) { - double value = operator.doubleValue(); - return ScriptValue.ofDouble(value * value * value); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(CubeInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CubeRootInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CubeRootInstruction.java deleted file mode 100644 index 535ba00..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/CubeRootInstruction.java +++ /dev/null @@ -1,21 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CubeRootInstruction extends OneNumberOperatorInstruction { - - public CubeRootInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CUBE_ROOT; } - - @Override protected ScriptValue operate(Number operator) { return ScriptValue.ofDouble(Math.pow(operator.doubleValue(), 1 / 3d)); } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(CubeRootInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/DivideInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/DivideInstruction.java deleted file mode 100644 index 9a237fd..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/DivideInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class DivideInstruction extends TwoNumberOperatorInstruction { - - public DivideInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofDouble(Math.abs(right.doubleValue()) < 1e-6d ? 0d : left.doubleValue() / right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.DIVIDE; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(DivideInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/DivideIntInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/DivideIntInstruction.java deleted file mode 100644 index 7d65d36..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/DivideIntInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class DivideIntInstruction extends TwoNumberOperatorInstruction { - - public DivideIntInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofLong(right.longValue() == 0 ? 0 : left.longValue() / right.longValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.DIVIDE_INT; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(DivideIntInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/ExpInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/ExpInstruction.java deleted file mode 100644 index 9e890e6..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/ExpInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class ExpInstruction extends OneNumberOperatorInstruction { - - public ExpInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.EXP; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.exp(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(ExpInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/FloorInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/FloorInstruction.java deleted file mode 100644 index 2e00fe6..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/FloorInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class FloorInstruction extends OneNumberOperatorInstruction { - - public FloorInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.FLOOR; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.floor(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(FloorInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/LnInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/LnInstruction.java deleted file mode 100644 index f81068f..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/LnInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class LnInstruction extends OneNumberOperatorInstruction { - - public LnInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.LN; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.log(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(LnInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/Log10Instruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/Log10Instruction.java deleted file mode 100644 index b95c1b1..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/Log10Instruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class Log10Instruction extends OneNumberOperatorInstruction { - - public Log10Instruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.LOG10; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.log10(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(Log10Instruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/LogABInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/LogABInstruction.java deleted file mode 100644 index f649954..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/LogABInstruction.java +++ /dev/null @@ -1,28 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class LogABInstruction extends TwoNumberOperatorInstruction { - - public LogABInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - double value = Math.log(right.doubleValue()) / Math.log(left.doubleValue()); - return ScriptValue.ofDouble(Double.isFinite(value) ? value : 0d); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.LOG_AB; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(LogABInstruction::new); } - - @Override protected String getLeftKey() { return "base"; } - @Override protected String getRightKey() { return "value"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/ModuloInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/ModuloInstruction.java deleted file mode 100644 index f0dba87..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/ModuloInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class ModuloInstruction extends TwoNumberOperatorInstruction { - - public ModuloInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofDouble(Math.abs(right.doubleValue()) < 1e-6d ? 0d : left.doubleValue() % right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.MODULO; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(ModuloInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/MultiplyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/MultiplyInstruction.java deleted file mode 100644 index 8147349..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/MultiplyInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class MultiplyInstruction extends TwoNumberOperatorInstruction { - - public MultiplyInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofDouble(left.doubleValue() * right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.MULTIPLY; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(MultiplyInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/NthRootInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/NthRootInstruction.java deleted file mode 100644 index b967884..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/NthRootInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class NthRootInstruction extends TwoNumberOperatorInstruction { - - public NthRootInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - double value = Math.pow(left.doubleValue(), 1 / right.doubleValue()); - return ScriptValue.ofDouble(Double.isFinite(value) ? value : left.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.NTH_ROOT; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(NthRootInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/PowInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/PowInstruction.java deleted file mode 100644 index e3e926c..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/PowInstruction.java +++ /dev/null @@ -1,28 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class PowInstruction extends TwoNumberOperatorInstruction { - - public PowInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - double value = Math.pow(left.doubleValue(), right.doubleValue()); - return ScriptValue.ofDouble(Double.isFinite(value) ? value : left.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.POW; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(PowInstruction::new); } - - @Override protected String getLeftKey() { return "base"; } - @Override protected String getRightKey() { return "power"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/RoundInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/RoundInstruction.java deleted file mode 100644 index ea59557..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/RoundInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RoundInstruction extends OneNumberOperatorInstruction { - - public RoundInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.ROUND; } - - @Override - protected ScriptValue operate(Number operator) { - return ScriptValue.ofDouble(Math.round(operator.doubleValue())); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(RoundInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SquareInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SquareInstruction.java deleted file mode 100644 index c3c2f58..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SquareInstruction.java +++ /dev/null @@ -1,25 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SquareInstruction extends OneNumberOperatorInstruction { - - public SquareInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SQUARE; } - - @Override - protected ScriptValue operate(Number operator) { - double value = operator.doubleValue(); - return ScriptValue.ofDouble(value * value); - } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(SquareInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SquareRootInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SquareRootInstruction.java deleted file mode 100644 index 9b80abe..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SquareRootInstruction.java +++ /dev/null @@ -1,21 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.OneNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SquareRootInstruction extends OneNumberOperatorInstruction { - - public SquareRootInstruction(ScriptValueSupplier operand) { super(operand); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SQUARE_ROOT; } - - @Override protected ScriptValue operate(Number operator) { return ScriptValue.ofDouble(Math.sqrt(operator.doubleValue())); } - - public static class Serializer extends OneNumberOperatorInstruction.Serializer { - public Serializer() { super(SquareRootInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SubtractInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SubtractInstruction.java deleted file mode 100644 index a7b2f5d..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/math/SubtractInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.math; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoNumberOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SubtractInstruction extends TwoNumberOperatorInstruction { - - public SubtractInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override - protected ScriptValue operate(Number left, Number right) { - return ScriptValue.ofDouble(left.doubleValue() - right.doubleValue()); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SUBTRACT; } - - public static class Serializer extends TwoNumberOperatorInstruction.Serializer { - public Serializer() { super(SubtractInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/AddIndexToPathInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/AddIndexToPathInstruction.java deleted file mode 100644 index c422c0f..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/AddIndexToPathInstruction.java +++ /dev/null @@ -1,37 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class AddIndexToPathInstruction extends TwoObjectOperatorInstruction { - - public AddIndexToPathInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.ADD_INDEX_TO_PATH; } - - @Override - protected ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - String path = left.str(); - if (path == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - Number index = right.num(); - if (index == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - return ScriptValue.string(path + "[" + index.intValue() + "]"); - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(AddIndexToPathInstruction::new); } - @Override protected String getLeftKey() { return "path"; } - @Override protected String getRightKey() { return "index"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/CopyNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/CopyNbtInstruction.java deleted file mode 100644 index b5ffa34..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/CopyNbtInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import javax.annotation.Nullable; - -import net.minecraft.world.level.storage.loot.functions.CopyNbtFunction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CopyNbtInstruction extends SourceToTargetNbtInstruction { - - public CopyNbtInstruction(ScriptValueSupplier sourcePath, @Nullable ScriptValueSupplier sourceStack, - ScriptValueSupplier targetPath, @Nullable ScriptValueSupplier targetStack) { - super(sourcePath, sourceStack, targetPath, targetStack); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.COPY_NBT; } - @Override protected CopyNbtFunction.MergeStrategy getMergeOp() { return CopyNbtFunction.MergeStrategy.REPLACE; } - - public static class Serializer extends SourceToTargetNbtInstruction.Serializer { - public Serializer() { super(CopyNbtInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/GetNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/GetNbtInstruction.java deleted file mode 100644 index 71d8014..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/GetNbtInstruction.java +++ /dev/null @@ -1,92 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import java.util.Collection; -import java.util.Iterator; -import java.util.function.Function; - -import javax.annotation.Nullable; - -import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; - -import net.minecraft.commands.arguments.NbtPathArgument; -import net.minecraft.nbt.CollectionTag; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; -import rbasamoyai.ogden.base.Components; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public class GetNbtInstruction extends SourceNbtInstruction { - - // Adapted from DataCommands --ritchie - private static final SimpleCommandExceptionType ERROR_MULTIPLE_TAGS = new SimpleCommandExceptionType(Components.translatable("commands.data.get.multiple")); - - private final Getter getter; - private final ScriptInstructionType type; - - public GetNbtInstruction(ScriptValueSupplier id, @Nullable ScriptValueSupplier nbtSource, Getter getter, ScriptInstructionType type) { - super(id, nbtSource); - this.getter = getter; - this.type = type; - } - - @Override public ScriptInstructionType type() { return this.type; } - - @Override - protected ScriptValue operate(NbtPathArgument.NbtPath id, CompoundTag tag) { - try { - return this.getter.getValue(tag, id); - } catch (CommandSyntaxException e) { - ; // TODO log error once - return ScriptValue.VOID; - } - } - - public static class Serializer extends SourceNbtInstruction.Serializer { - public Serializer(ScriptInstructionType type, Getter getter) { - super((id, source) -> new GetNbtInstruction(id, source, getter, type)); - } - - public static Function provider(Getter getter) { - return type -> new Serializer(type, getter); - } - - public static Function provider(Function valueProvider, int tagId, ScriptValue defaultValue) { - return type -> new Serializer(type, createSimpleGetter(valueProvider, tagId, defaultValue)); - } - } - - // Adapted from DataCommands#getSingleTag - public static Tag getSingleTag(CompoundTag tag, NbtPathArgument.NbtPath path) throws CommandSyntaxException { - Collection collection = path.get(tag); - Iterator iterator = collection.iterator(); - Tag tag1 = iterator.next(); - if (iterator.hasNext()) { - throw ERROR_MULTIPLE_TAGS.create(); - } else { - return tag1; - } - } - - public static ScriptValue getScriptValueList(CompoundTag tag, NbtPathArgument.NbtPath id) throws CommandSyntaxException { - // Using raw tag to not require tag type check in CompoundTag#getList --ritchie - Tag tag1 = getSingleTag(tag, id); - return tag1 instanceof CollectionTag ? ScriptValue.fromNbt(tag1) : ScriptValue.EMPTY_LIST; - } - - public static Getter createSimpleGetter(Function valueProvider, int tagId, ScriptValue defaultValue) { - return (tag, id) -> { - Tag tag1 = getSingleTag(tag, id); - return tag1.getId() == tagId ? valueProvider.apply(tag1) : defaultValue; - }; - } - - @FunctionalInterface - public interface Getter { - ScriptValue getValue(CompoundTag tag, NbtPathArgument.NbtPath id) throws CommandSyntaxException; - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/InsertNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/InsertNbtInstruction.java deleted file mode 100644 index 8683ab0..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/InsertNbtInstruction.java +++ /dev/null @@ -1,203 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import java.util.Arrays; -import java.util.List; -import java.util.Locale; -import java.util.Map; -import java.util.function.Function; -import java.util.stream.Collectors; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.mojang.brigadier.StringReader; - -import net.minecraft.commands.arguments.NbtPathArgument; -import net.minecraft.nbt.ByteArrayTag; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.IntArrayTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.LongArrayTag; -import net.minecraft.nbt.StringTag; -import net.minecraft.nbt.Tag; -import net.minecraft.util.GsonHelper; -import net.minecraft.util.Mth; -import net.minecraft.util.StringRepresentable; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class InsertNbtInstruction implements ScriptInstruction { - - private final ScriptValueSupplier source; - private final SourceType type; - private final ScriptValueSupplier target; - private final ScriptValueSupplier index; - private final ScriptValueSupplier start; - private final ScriptValueSupplier end; - @Nullable private final ScriptValueSupplier sourceNbtSource; - @Nullable private final ScriptValueSupplier targetNbtSource; - - public InsertNbtInstruction(ScriptValueSupplier source, SourceType type, ScriptValueSupplier target, ScriptValueSupplier index, - ScriptValueSupplier start, ScriptValueSupplier end, @Nullable ScriptValueSupplier sourceNbtSource, - @Nullable ScriptValueSupplier targetNbtSource) { - this.source = source; - this.type = type; - this.target = target; - this.index = index; - this.start = start; - this.end = end; - this.sourceNbtSource = sourceNbtSource; - this.targetNbtSource = targetNbtSource; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.INSERT_NBT; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ScriptValue sourceRes = this.source.run(context); - String targetRes = this.target.run(context).str(); - if (targetRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - Number indexRes = this.index.run(context).num(); - if (indexRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - Number startRes = this.start.run(context).num(); - if (startRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - Number endRes = this.end.run(context).num(); - if (endRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - CompoundTag sourceItemTag = context.stack().getOrCreateTag(); - CompoundTag targetItemTag = sourceItemTag; - if (this.sourceNbtSource != null) { - ItemStack sourceNbtSourceRes = this.sourceNbtSource.run(context).stack(); - if (sourceNbtSourceRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - sourceItemTag = sourceNbtSourceRes.getOrCreateTag(); - } - } - if (this.targetNbtSource != null) { - ItemStack targetNbtSourceRes = this.targetNbtSource.run(context).stack(); - if (targetNbtSourceRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - targetItemTag = targetNbtSourceRes.getOrCreateTag(); - } - } - try { - Tag sourceTag; - if (this.type == SourceType.FROM) { - String sourceStr = sourceRes.str(); - if (sourceStr == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - NbtPathArgument.NbtPath path = (new NbtPathArgument()).parse(new StringReader(sourceStr)); - sourceTag = GetNbtInstruction.getSingleTag(sourceItemTag, path); - } else { - sourceTag = sourceRes.toNbt(); - } - NbtPathArgument.NbtPath targetPath = (new NbtPathArgument()).parse(new StringReader(targetRes)); - Tag targetTag = GetNbtInstruction.getSingleTag(targetItemTag, targetPath); - if (sourceTag.getId() != targetTag.getId()) { - ; // TODO log error once - return ScriptValue.VOID; - } - int index = indexRes.intValue(); - int start = startRes.intValue(); - int end = endRes.intValue(); - if (sourceTag.getId() == Tag.TAG_STRING) { - String sourceStr = sourceTag.getAsString(); - StringBuilder sb = new StringBuilder(sourceTag.getAsString()); - index = index < 0 ? sb.length() : Math.min(index, sb.length()); - start = start < 0 ? sb.length() : Mth.clamp(start, 0, sb.length()); - end = end < 0 ? sb.length() : Mth.clamp(end, 0, sb.length()); - sb.insert(index, sourceStr, start, end); - targetPath.set(sourceItemTag, StringTag.valueOf(sb.toString())); - } else if (sourceTag.getId() == Tag.TAG_LIST) { - ListTag sourceList = (ListTag) sourceTag; - ListTag targetList = (ListTag) targetTag; - if (sourceList.getElementType() != targetList.getElementType()) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - insertList(sourceList, targetList, index, start, end); - } - } else if (sourceTag.getId() == Tag.TAG_INT_ARRAY) { - insertList((IntArrayTag) sourceTag, (IntArrayTag) targetTag, index, start, end); - } else if (sourceTag.getId() == Tag.TAG_LONG_ARRAY) { - insertList((LongArrayTag) sourceTag, (LongArrayTag) targetTag, index, start, end); - } else if (sourceTag.getId() == Tag.TAG_BYTE_ARRAY) { - insertList((ByteArrayTag) sourceTag, (ByteArrayTag) targetTag, index, start, end); - } else { - ; // TODO log error once - } - } catch (Exception e) { - ; // TODO log error once - } - return ScriptValue.VOID; - } - - public static void insertList(List source, List target, int index, int start, int end) { - index = index < 0 ? target.size() : Math.min(index, target.size()); - start = start < 0 ? source.size() : Mth.clamp(start, 0, source.size()); - end = end < 0 ? source.size() : Mth.clamp(end, 0, source.size()); - target.addAll(index, source.subList(start, end)); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("source")) - throw new JsonParseException("Insert NBT instruction missing parameter 'source'"); - if (!obj.has("target")) - throw new JsonParseException("Insert NBT instruction missing parameter 'target'"); - if (!obj.has("start")) - throw new JsonParseException("Insert NBT instruction missing parameter 'start'"); - ScriptValueSupplier source = ScriptValueSupplier.fromJson(obj.get("source")); - SourceType type = SourceType.valueOf(GsonHelper.getAsString(obj, "type")); - ScriptValueSupplier target = ScriptValueSupplier.fromJson(obj.get("target")); - ScriptValueSupplier index = obj.has("index") ? ScriptValueSupplier.fromJson(obj.get("index")) : ScriptValue.ofInt(-1); - ScriptValueSupplier start = ScriptValueSupplier.fromJson(obj.get("start")); - ScriptValueSupplier end = obj.has("end") ? ScriptValueSupplier.fromJson(obj.get("end")) : ScriptValue.ofInt(-1); - ScriptValueSupplier sourceNbtSource = obj.has("source_nbt_stack") ? ScriptValueSupplier.fromJson(obj.get("source_nbt_stack")) : null; - ScriptValueSupplier targetNbtSource = obj.has("target_nbt_stack") ? ScriptValueSupplier.fromJson(obj.get("target_nbt_stack")) : null; - return new InsertNbtInstruction(source, type, target, index, start, end, sourceNbtSource, targetNbtSource); - } - } - - public enum SourceType implements StringRepresentable { - VALUE, - FROM; - - private static final Map BY_ID = Arrays.stream(values()) - .collect(Collectors.toMap(SourceType::getSerializedName, Function.identity())); - - private final String id = this.name().toLowerCase(Locale.ROOT); - - @Override public String getSerializedName() { return this.getSerializedName(); } - - public static SourceType byId(String id) { return BY_ID.getOrDefault(id, VALUE); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/MergeNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/MergeNbtInstruction.java deleted file mode 100644 index ce6dd99..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/MergeNbtInstruction.java +++ /dev/null @@ -1,24 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import javax.annotation.Nullable; - -import net.minecraft.world.level.storage.loot.functions.CopyNbtFunction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class MergeNbtInstruction extends SourceToTargetNbtInstruction { - - public MergeNbtInstruction(ScriptValueSupplier sourcePath, @Nullable ScriptValueSupplier sourceStack, - ScriptValueSupplier targetPath, @Nullable ScriptValueSupplier targetStack) { - super(sourcePath, sourceStack, targetPath, targetStack); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.MERGE_NBT; } - @Override protected CopyNbtFunction.MergeStrategy getMergeOp() { return CopyNbtFunction.MergeStrategy.MERGE; } - - public static class Serializer extends SourceToTargetNbtInstruction.Serializer { - public Serializer() { super(MergeNbtInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/RemoveNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/RemoveNbtInstruction.java deleted file mode 100644 index 3728bcd..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/RemoveNbtInstruction.java +++ /dev/null @@ -1,28 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import javax.annotation.Nullable; - -import net.minecraft.commands.arguments.NbtPathArgument; -import net.minecraft.nbt.CompoundTag; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RemoveNbtInstruction extends SourceNbtInstruction { - - public RemoveNbtInstruction(ScriptValueSupplier id, @Nullable ScriptValueSupplier nbtSource) { super(id, nbtSource); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.REMOVE_NBT; } - - @Override - protected ScriptValue operate(NbtPathArgument.NbtPath id, CompoundTag tag) { - id.remove(tag); - return ScriptValue.VOID; - } - - public static class Serializer extends SourceNbtInstruction.Serializer { - public Serializer() { super(RemoveNbtInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SetNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SetNbtInstruction.java deleted file mode 100644 index 32d8a6f..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SetNbtInstruction.java +++ /dev/null @@ -1,145 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import java.util.List; -import java.util.Map; -import java.util.function.Function; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.mojang.brigadier.StringReader; - -import net.minecraft.commands.arguments.NbtPathArgument; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.ListTag; -import net.minecraft.nbt.Tag; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public class SetNbtInstruction implements ScriptInstruction { - - private final ScriptValueSupplier id; - @Nullable private final ScriptValueSupplier nbtSource; - private final Getter getter; - private final ScriptValueSupplier value; - private final ScriptInstructionType type; - - public SetNbtInstruction(ScriptValueSupplier id, @Nullable ScriptValueSupplier nbtSource, Getter getter, - ScriptValueSupplier value, ScriptInstructionType type) { - this.id = id; - this.nbtSource = nbtSource; - this.getter = getter; - this.value = value; - this.type = type; - } - - @Override public ScriptInstructionType type() { return this.type; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String idRes = this.id.run(context).str(); - if (idRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - CompoundTag tag = context.stack().getOrCreateTag(); - if (this.nbtSource != null) { - ItemStack nbtSourceRes = this.nbtSource.run(context).stack(); - if (nbtSourceRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - tag = nbtSourceRes.getOrCreateTag(); - } - } - try { - Tag addTag = this.getter.getValue(this.value.run(context)); - if (addTag == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - NbtPathArgument.NbtPath path = (new NbtPathArgument()).parse(new StringReader(idRes)); - path.set(tag, addTag); - } catch (Exception e) { - ; // TODO log error once - } - return ScriptValue.VOID; - } - - @FunctionalInterface - public interface Getter { - @Nullable Tag getValue(ScriptValue value) throws Exception; - - static Getter fromFunctions(Function valueGetter, Function tagProvider) { - return sv -> { - T t = valueGetter.apply(sv); - return t == null ? null : tagProvider.apply(t); - }; - } - - static Getter fromFunctions(Function valueGetter, Function transformer, Function tagProvider) { - return sv -> { - I i = valueGetter.apply(sv); - if (i == null) - return null; - T t = transformer.apply(i); - return t == null ? null : tagProvider.apply(t); - }; - } - } - - public static class Serializer implements ScriptInstructionSerializer { - private final Constructor constructor; - - public Serializer(ScriptInstructionType type, Getter getter) { - this.constructor = (id, nbtSource, value) -> new SetNbtInstruction(id, nbtSource, getter, value, type); - } - - public static Function provider(Getter getter) { - return type -> new Serializer(type, getter); - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("id")) - throw new JsonParseException("Set NBT instruction missing value 'id'"); - if (!obj.has("value")) - throw new JsonParseException("Set NBT instruction missing value 'value'"); - ScriptValueSupplier id = ScriptValueSupplier.fromJson(obj.get("id")); - ScriptValueSupplier value = ScriptValueSupplier.fromJson(obj.get("value")); - ScriptValueSupplier nbtSource = obj.has("source") ? ScriptValueSupplier.fromJson(obj.get("source")) : null; - return this.constructor.create(id, nbtSource, value); - } - - public interface Constructor { - ScriptInstruction create(ScriptValueSupplier id, @Nullable ScriptValueSupplier nbtSource, ScriptValueSupplier value); - } - } - - public static Tag getListTag(List list) { - ListTag listTag = new ListTag(); - for (ScriptValue scriptValue : list) - listTag.add(scriptValue.toNbt()); - return listTag; - } - - public static Tag getObjectTag(Map obj) { - CompoundTag tag = new CompoundTag(); - for (Map.Entry entry : obj.entrySet()) - tag.put(entry.getKey(), entry.getValue().toNbt()); - return tag; - } - - public static Tag getItemStackTag(ItemStack stack) { - return stack.save(new CompoundTag()); - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SourceNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SourceNbtInstruction.java deleted file mode 100644 index a6672f7..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SourceNbtInstruction.java +++ /dev/null @@ -1,78 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import java.util.function.BiFunction; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; -import com.mojang.brigadier.StringReader; - -import net.minecraft.commands.arguments.NbtPathArgument; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class SourceNbtInstruction implements ScriptInstruction { - - private final ScriptValueSupplier id; - @Nullable private final ScriptValueSupplier nbtStack; - - protected SourceNbtInstruction(ScriptValueSupplier id, @Nullable ScriptValueSupplier nbtStack) { - this.id = id; - this.nbtStack = nbtStack; - } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String idRes = this.id.run(context).str(); - if (idRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - CompoundTag tag = context.stack().getOrCreateTag(); - if (this.nbtStack != null) { - ItemStack nbtStackRes = this.nbtStack.run(context).stack(); - if (nbtStackRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - tag = nbtStackRes.getOrCreateTag(); - } - } - try { - NbtPathArgument.NbtPath path = (new NbtPathArgument()).parse(new StringReader(idRes)); - return this.operate(path, tag); - } catch (Exception e) { - ; // TODO log error once - return ScriptValue.VOID; - } - } - - protected abstract ScriptValue operate(NbtPathArgument.NbtPath id, CompoundTag tag); - - public static abstract class Serializer implements ScriptInstructionSerializer { - private final BiFunction constructor; - - protected Serializer(BiFunction constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("id")) - throw new JsonSyntaxException("NBT instruction missing parameter 'id'"); - ScriptValueSupplier id = ScriptValueSupplier.fromJson(obj.get("id")); - ScriptValueSupplier nbtSource = obj.has("source") ? ScriptValueSupplier.fromJson(obj.get("source")) : null; - return this.constructor.apply(id, nbtSource); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SourceToTargetNbtInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SourceToTargetNbtInstruction.java deleted file mode 100644 index 044ed25..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/nbt/SourceToTargetNbtInstruction.java +++ /dev/null @@ -1,114 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.nbt; - -import java.util.List; - -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; -import com.google.gson.JsonSyntaxException; -import com.mojang.brigadier.StringReader; -import com.mojang.brigadier.exceptions.CommandSyntaxException; - -import net.minecraft.commands.arguments.NbtPathArgument; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.storage.loot.functions.CopyNbtFunction; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class SourceToTargetNbtInstruction implements ScriptInstruction { - - private final ScriptValueSupplier sourcePath; - @Nullable private final ScriptValueSupplier sourceStack; - - private final ScriptValueSupplier targetPath; - @Nullable private final ScriptValueSupplier targetStack; - - protected SourceToTargetNbtInstruction(ScriptValueSupplier sourcePath, @Nullable ScriptValueSupplier sourceStack, - ScriptValueSupplier targetPath, @Nullable ScriptValueSupplier targetStack) { - this.sourcePath = sourcePath; - this.sourceStack = sourceStack; - this.targetPath = targetPath; - this.targetStack = targetStack; - } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String sourcePathRes = this.sourcePath.run(context).str(); - if (sourcePathRes == null) { - ; // TODO: log error once - return ScriptValue.VOID; - } - String targetPathRes = this.targetPath.run(context).str(); - if (targetPathRes == null) { - ; // TODO: log error once - return ScriptValue.VOID; - } - CompoundTag sourceTag = context.stack().getOrCreateTag(); - CompoundTag targetTag = sourceTag; - if (this.sourceStack != null) { - ItemStack sourceStackRes = this.sourceStack.run(context).stack(); - if (sourceStackRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - sourceTag = sourceStackRes.getOrCreateTag(); - } - } - if (this.targetStack != null) { - ItemStack targetStackRes = this.targetStack.run(context).stack(); - if (targetStackRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } else { - targetTag = targetStackRes.getOrCreateTag(); - } - } - try { - NbtPathArgument.NbtPath sourceArg = (new NbtPathArgument()).parse(new StringReader(sourcePathRes)); - NbtPathArgument.NbtPath targetArg = (new NbtPathArgument()).parse(new StringReader(targetPathRes)); - List list = sourceArg.get(sourceTag); - if (!list.isEmpty()) - this.getMergeOp().merge(targetTag, targetArg, list); - } catch (CommandSyntaxException e) { - ; // TODO log error once - } - return ScriptValue.VOID; - } - - protected abstract CopyNbtFunction.MergeStrategy getMergeOp(); - - public abstract static class Serializer implements ScriptInstructionSerializer { - private final Constructor constructor; - - protected Serializer(Constructor constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("source_path")) - throw new JsonSyntaxException("Instruction missing parameter 'source_path'"); - if (!obj.has("target_path")) - throw new JsonSyntaxException("Instruction missing parameter 'target_path'"); - ScriptValueSupplier sourcePath = ScriptValueSupplier.fromJson(obj.get("source_path")); - ScriptValueSupplier targetPath = ScriptValueSupplier.fromJson(obj.get("target_path")); - ScriptValueSupplier sourceStackProvider = obj.has("source_stack") ? ScriptValueSupplier.fromJson(obj.get("source_stack")) : null; - ScriptValueSupplier targetStackProvider = obj.has("target_stack") ? ScriptValueSupplier.fromJson(obj.get("target_stack")) : null; - return this.constructor.create(sourcePath, sourceStackProvider, targetPath, targetStackProvider); - } - - public interface Constructor { - ScriptInstruction create(ScriptValueSupplier sourcePath, @Nullable ScriptValueSupplier sourceStackProvider, - ScriptValueSupplier targetPath, @Nullable ScriptValueSupplier targetStackProvider); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/CreateObjectInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/CreateObjectInstruction.java deleted file mode 100644 index 8da4947..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/CreateObjectInstruction.java +++ /dev/null @@ -1,52 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.object; - -import java.util.HashMap; -import java.util.Map; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonElement; -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.util.GsonHelper; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class CreateObjectInstruction implements ScriptInstruction { - - private final Map objectSup; - - public CreateObjectInstruction(Map objectSup) { - this.objectSup = objectSup; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CREATE_OBJECT; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - Map res = new HashMap<>(); - for (Map.Entry entry : this.objectSup.entrySet()) - res.put(entry.getKey(), entry.getValue().run(context)); - return new ScriptValue(res); - } - - public static class Serializer implements ScriptInstructionSerializer { - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - JsonObject mapObj = GsonHelper.getAsJsonObject(obj, "data"); - Map objSup = new HashMap<>(); - for (Map.Entry entry : mapObj.entrySet()) - objSup.put(entry.getKey(), ScriptValueSupplier.fromJson(entry.getValue())); - return new CreateObjectInstruction(objSup); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/GetObjectPropertyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/GetObjectPropertyInstruction.java deleted file mode 100644 index 0a1129a..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/GetObjectPropertyInstruction.java +++ /dev/null @@ -1,26 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.object; - -import java.util.Map; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetObjectPropertyInstruction extends KeyOnlyObjectPropertyInstruction { - - public GetObjectPropertyInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_OBJECT_PROPERTY; } - - @Override - protected ScriptValue operateOnMap(Map map, String key) { - return map.getOrDefault(key, ScriptValue.VOID); - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(GetObjectPropertyInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/KeyOnlyObjectPropertyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/KeyOnlyObjectPropertyInstruction.java deleted file mode 100644 index 264e7fb..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/KeyOnlyObjectPropertyInstruction.java +++ /dev/null @@ -1,44 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.object; - -import java.util.Map; -import java.util.function.BiFunction; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; - -public abstract class KeyOnlyObjectPropertyInstruction extends TwoObjectOperatorInstruction { - - protected KeyOnlyObjectPropertyInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { - super(left, right); - } - - @Override - protected final ScriptValue operate(ScriptValue left, ScriptValue right, ScriptContext context) { - Map map = left.map(); - if (map == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - String key = right.str(); - if (key == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - return this.operateOnMap(map, key); - } - - protected abstract ScriptValue operateOnMap(Map map, String key); - - public static abstract class Serializer extends TwoObjectOperatorInstruction.Serializer { - protected Serializer(BiFunction constructor) { - super(constructor); - } - - @Override protected String getLeftKey() { return "object"; } - @Override protected String getRightKey() { return "value"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/RemoveObjectPropertyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/RemoveObjectPropertyInstruction.java deleted file mode 100644 index adf3869..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/RemoveObjectPropertyInstruction.java +++ /dev/null @@ -1,27 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.object; - -import java.util.Map; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RemoveObjectPropertyInstruction extends KeyOnlyObjectPropertyInstruction { - - public RemoveObjectPropertyInstruction(ScriptValueSupplier left, ScriptValueSupplier right) { super(left, right); } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.REMOVE_OBJECT_PROPERTY; } - - @Override - protected ScriptValue operateOnMap(Map map, String key) { - map.remove(key); - return ScriptValue.VOID; - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(RemoveObjectPropertyInstruction::new); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/SetObjectPropertyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/SetObjectPropertyInstruction.java deleted file mode 100644 index 9ca2a9b..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/object/SetObjectPropertyInstruction.java +++ /dev/null @@ -1,66 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.object; - -import java.util.Map; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SetObjectPropertyInstruction implements ScriptInstruction { - - private final ScriptValueSupplier obj; - private final ScriptValueSupplier key; - private final ScriptValueSupplier value; - - public SetObjectPropertyInstruction(ScriptValueSupplier obj, ScriptValueSupplier key, ScriptValueSupplier value) { - this.obj = obj; - this.key = key; - this.value = value; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SET_OBJECT_PROPERTY; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - Map objRes = this.obj.run(context).map(); - if (objRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - String keyRes = this.key.run(context).str(); - if (keyRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - ScriptValue previous = objRes.put(keyRes, this.value.run(context)); - return previous == null ? ScriptValue.VOID : previous; - } - - public static class Serializer implements ScriptInstructionSerializer { - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("object")) - throw new JsonParseException("Set object property instruction missing parameter 'object'"); - if (!obj.has("key")) - throw new JsonParseException("Set object property instruction missing parameter 'key'"); - if (!obj.has("value")) - throw new JsonParseException("Set object property instruction missing parameter 'value'"); - ScriptValueSupplier objSup = ScriptValueSupplier.fromJson(obj.get("object")); - ScriptValueSupplier key = ScriptValueSupplier.fromJson(obj.get("key")); - ScriptValueSupplier value = ScriptValueSupplier.fromJson(obj.get("value")); - return new SetObjectPropertyInstruction(objSup, key, value); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomBooleanInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomBooleanInstruction.java deleted file mode 100644 index 56475e0..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomBooleanInstruction.java +++ /dev/null @@ -1,27 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.random; - -import java.util.Random; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RandomBooleanInstruction extends RandomValueInstruction { - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.RANDOM_BOOLEAN; } - @Override protected ScriptValue randomValue(Random random, ScriptContext context) { return ScriptValue.bool(random.nextBoolean()); } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - return new RandomBooleanInstruction(); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomBoundedNumberInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomBoundedNumberInstruction.java deleted file mode 100644 index 1f26895..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomBoundedNumberInstruction.java +++ /dev/null @@ -1,80 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.random; - -import java.util.Random; -import java.util.function.BiFunction; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; - -public abstract class RandomBoundedNumberInstruction extends RandomValueInstruction { - - private final ScriptValueSupplier minBound; - private final ScriptValueSupplier maxBound; - - protected RandomBoundedNumberInstruction(ScriptValueSupplier minBound, ScriptValueSupplier maxBound) { - this.minBound = minBound; - this.maxBound = maxBound; - } - - @Override - protected final ScriptValue randomValue(Random random, ScriptContext context) { - Number minRes = this.minBound.run(context).num(); - if (minRes == null) { - ; // TODO log error once - return ScriptValue.ZERO; - } - Number maxRes = this.maxBound.run(context).num(); - if (maxRes == null) { - ; // TODO log error once - return ScriptValue.ZERO; - } - if (!this.validRange(minRes, maxRes)) { - ; // TODO log error once - return ScriptValue.ZERO; - } - return this.randomNumber(random, minRes, maxRes); - } - - protected abstract boolean validRange(Number min, Number max); - protected abstract ScriptValue randomNumber(Random random, Number min, Number max); - - public static abstract class Serializer implements ScriptInstructionSerializer { - private final BiFunction constructor; - - protected Serializer(BiFunction constructor) { - this.constructor = constructor; - } - - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - boolean hasMinBound = obj.has("min"); - boolean hasMaxBound = obj.has("max"); - ScriptValueSupplier minBound; - ScriptValueSupplier maxBound; - if (hasMinBound && hasMaxBound) { - minBound = ScriptValueSupplier.fromJson(obj.get("min")); - maxBound = ScriptValueSupplier.fromJson(obj.get("max")); - } else if (hasMinBound) { - minBound = ScriptValueSupplier.fromJson(obj.get("min")); - maxBound = ScriptValue.ZERO; - } else if (hasMaxBound) { - minBound = ScriptValue.ZERO; - maxBound = ScriptValueSupplier.fromJson(obj.get("max")); - } else { - minBound = this.getDefaultMinValue(); - maxBound = this.getDefaultMaxValue(); - } - return this.constructor.apply(minBound, maxBound); - } - - protected abstract ScriptValue getDefaultMinValue(); - protected abstract ScriptValue getDefaultMaxValue(); - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomFloatInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomFloatInstruction.java deleted file mode 100644 index d95db52..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomFloatInstruction.java +++ /dev/null @@ -1,31 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.random; - -import java.util.Random; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RandomFloatInstruction extends RandomBoundedNumberInstruction { - - public RandomFloatInstruction(ScriptValueSupplier minBound, ScriptValueSupplier maxBound) { - super(minBound, maxBound); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.RANDOM_FLOAT; } - - @Override protected boolean validRange(Number min, Number max) { return min.doubleValue() < max.doubleValue(); } - - @Override - protected ScriptValue randomNumber(Random random, Number min, Number max) { - return ScriptValue.ofDouble(random.nextDouble(min.doubleValue(), max.doubleValue())); - } - - public static class Serializer extends RandomBoundedNumberInstruction.Serializer { - public Serializer() { super(RandomFloatInstruction::new); } - @Override protected ScriptValue getDefaultMinValue() { return ScriptValue.ofLong(Long.MIN_VALUE); } - @Override protected ScriptValue getDefaultMaxValue() { return ScriptValue.ofLong(Long.MAX_VALUE); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomGaussianInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomGaussianInstruction.java deleted file mode 100644 index 3309aa4..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomGaussianInstruction.java +++ /dev/null @@ -1,56 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.random; - -import java.util.Random; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RandomGaussianInstruction extends RandomValueInstruction { - - private final ScriptValueSupplier mean; - private final ScriptValueSupplier stdDev; - - public RandomGaussianInstruction(ScriptValueSupplier mean, ScriptValueSupplier stdDev) { - this.mean = mean; - this.stdDev = stdDev; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.RANDOM_GAUSSIAN; } - - @Override - protected ScriptValue randomValue(Random random, ScriptContext context) { - Number meanRes = this.mean.run(context).num(); - if (meanRes == null) { - ; // TODO log error once - return ScriptValue.ZERO; - } - Number stdDevRes = this.stdDev.run(context).num(); - if (stdDevRes == null) { - ; // TODO log error once - return ScriptValue.ZERO; - } - if (stdDevRes.doubleValue() < 0) { - ; // TODO log error once - return ScriptValue.ZERO; - } - return ScriptValue.ofDouble(random.nextGaussian(meanRes.doubleValue(), stdDevRes.doubleValue())); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - ScriptValueSupplier mean = obj.has("mean") ? ScriptValueSupplier.fromJson(obj.get("mean")) : ScriptValue.ZERO; - ScriptValueSupplier stdDev = obj.has("standard_deviation") ? ScriptValueSupplier.fromJson(obj.get("standard_deviation")) : ScriptValue.ofDouble(1); - return new RandomGaussianInstruction(mean, stdDev); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomIntInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomIntInstruction.java deleted file mode 100644 index 5f01d97..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomIntInstruction.java +++ /dev/null @@ -1,31 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.random; - -import java.util.Random; - -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class RandomIntInstruction extends RandomBoundedNumberInstruction { - - public RandomIntInstruction(ScriptValueSupplier minBound, ScriptValueSupplier maxBound) { - super(minBound, maxBound); - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.RANDOM_INT; } - - @Override protected boolean validRange(Number min, Number max) { return min.longValue() < max.longValue(); } - - @Override - protected ScriptValue randomNumber(Random random, Number min, Number max) { - return ScriptValue.ofLong(random.nextLong(min.longValue(), max.longValue())); - } - - public static class Serializer extends RandomBoundedNumberInstruction.Serializer { - public Serializer() { super(RandomIntInstruction::new); } - @Override protected ScriptValue getDefaultMinValue() { return ScriptValue.ofLong(Long.MIN_VALUE); } - @Override protected ScriptValue getDefaultMaxValue() { return ScriptValue.ofLong(Long.MAX_VALUE); } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomValueInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomValueInstruction.java deleted file mode 100644 index 18e6696..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/random/RandomValueInstruction.java +++ /dev/null @@ -1,21 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.random; - -import java.util.Random; - -import javax.annotation.Nonnull; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; - -public abstract class RandomValueInstruction implements ScriptInstruction { - - @Nonnull - @Override - public final ScriptValue run(ScriptContext context) { - return this.randomValue(context.level().getRandom(), context); - } - - protected abstract ScriptValue randomValue(Random random, ScriptContext context); - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/ChangeStateInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/ChangeStateInstruction.java deleted file mode 100644 index 6925acd..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/ChangeStateInstruction.java +++ /dev/null @@ -1,50 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.state_actions; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.nbt.CompoundTag; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class ChangeStateInstruction implements ScriptInstruction { - - private final ScriptValueSupplier state; - - public ChangeStateInstruction(ScriptValueSupplier state) { - this.state = state; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.CHANGE_STATE; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String stateRes = this.state.run(context).str(); - if (stateRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - CompoundTag tag = context.stack().getOrCreateTag(); - tag.putString("CurrentTime", stateRes); - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("state")) - throw new JsonParseException("Change state instruction missing parameter 'state'"); - ScriptValueSupplier state = ScriptValueSupplier.fromJson(obj.get("state")); - return new ChangeStateInstruction(state); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/HasActionTimePassedInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/HasActionTimePassedInstruction.java deleted file mode 100644 index c4b8f3a..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/HasActionTimePassedInstruction.java +++ /dev/null @@ -1,52 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.state_actions; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.nbt.CompoundTag; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class HasActionTimePassedInstruction implements ScriptInstruction { - - private final ScriptValueSupplier time; - - public HasActionTimePassedInstruction(ScriptValueSupplier time) { - this.time = time; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.HAS_ACTION_TIME_PASSED; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - Number timeRes = this.time.run(context).num(); - if (timeRes == null) { - ; // TODO log error once - return ScriptValue.FALSE; - } - float t = timeRes.floatValue(); - CompoundTag tag = context.stack().getOrCreateTag(); - float currentTime = tag.getFloat("CurrentTime"); - float nextTime = tag.getFloat("NextTime"); - return ScriptValue.bool(currentTime <= t && t < nextTime); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("time")) - throw new JsonParseException("Has action time passed instruction missing parameter 'time'"); - ScriptValueSupplier time = ScriptValueSupplier.fromJson(obj.get("time")); - return new HasActionTimePassedInstruction(time); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/SyncAnimationInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/SyncAnimationInstruction.java deleted file mode 100644 index d58a315..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/SyncAnimationInstruction.java +++ /dev/null @@ -1,51 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.state_actions; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.OgdenFirearmItem; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SyncAnimationInstruction implements ScriptInstruction { - - private final ScriptValueSupplier animationData; - - public SyncAnimationInstruction(ScriptValueSupplier animationData) { - this.animationData = animationData; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SYNC_ANIMATION; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ItemStack stack = context.stack(); - if (!(stack.getItem() instanceof OgdenFirearmItem firearm)) { - ; // TODO log error once - return ScriptValue.VOID; - } - ScriptValue dataRes = this.animationData.run(context); - firearm.syncFirearmAnimation(dataRes, context); - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("animation_data")) - throw new JsonParseException("Sync animation instruction missing parameter 'animation_data'"); - ScriptValueSupplier animationData = ScriptValueSupplier.fromJson(obj.get("animation_data")); - return new SyncAnimationInstruction(animationData); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/UpdateActionTimerInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/UpdateActionTimerInstruction.java deleted file mode 100644 index 8d22f63..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/state_actions/UpdateActionTimerInstruction.java +++ /dev/null @@ -1,51 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.state_actions; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.Tag; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.firearms.OgdenFirearmItem; -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class UpdateActionTimerInstruction implements ScriptInstruction { - - public UpdateActionTimerInstruction() { - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.UPDATE_ACTION_TIMER; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - ItemStack stack = context.stack(); - if (!(stack.getItem() instanceof OgdenFirearmItem firearm)) { - ; // TODO log error once - return ScriptValue.VOID; - } - CompoundTag tag = stack.getOrCreateTag(); - if (!tag.contains("NextTime", Tag.TAG_FLOAT)) - tag.putFloat("NextTime", 0f); - float time = tag.getFloat("NextTime"); - tag.putFloat("CurrentTime", time); - time += firearm.getPassedActionTime(context); - tag.putFloat("NextTime", time); - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - return new UpdateActionTimerInstruction(); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/string/InsertStringInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/string/InsertStringInstruction.java deleted file mode 100644 index 4c712f1..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/string/InsertStringInstruction.java +++ /dev/null @@ -1,70 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.string; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class InsertStringInstruction implements ScriptInstruction { - - private final ScriptValueSupplier target; - private final ScriptValueSupplier source; - private final ScriptValueSupplier index; - - public InsertStringInstruction(ScriptValueSupplier target, ScriptValueSupplier source, ScriptValueSupplier index) { - this.target = target; - this.source = source; - this.index = index; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.INSERT_STRING; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String targetRes = this.target.run(context).str(); - if (targetRes == null) { - ; // TODO log error once - return ScriptValue.VOID; - } - - String sourceRes = this.source.run(context).str(); - if (sourceRes == null) { - ; // TODO log error once - return ScriptValue.string(targetRes); - } - Number indexRes = this.index.run(context).num(); - if (indexRes == null) { - ; // TODO log error once - return ScriptValue.string(targetRes); - } - int i = indexRes.intValue(); - i = i < 0 ? targetRes.length() : Math.min(i, targetRes.length()); - return ScriptValue.string(new StringBuilder(targetRes).insert(i, sourceRes).toString()); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("target")) - throw new JsonParseException("Insert string instruction missing parameter 'target'"); - if (!obj.has("source")) - throw new JsonParseException("Insert string instruction missing parameter 'source'"); - if (!obj.has("index")) - throw new JsonParseException("Insert string instruction missing parameter 'index'"); - ScriptValueSupplier target = ScriptValueSupplier.fromJson(obj.get("target")); - ScriptValueSupplier source = ScriptValueSupplier.fromJson(obj.get("source")); - ScriptValueSupplier index = ScriptValueSupplier.fromJson(obj.get("index")); - return new InsertStringInstruction(target, source, index); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/string/SubstringInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/string/SubstringInstruction.java deleted file mode 100644 index d4f88e4..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/string/SubstringInstruction.java +++ /dev/null @@ -1,73 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.string; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SubstringInstruction implements ScriptInstruction { - - private final ScriptValueSupplier str; - private final ScriptValueSupplier start; - private final ScriptValueSupplier end; - - public SubstringInstruction(ScriptValueSupplier str, ScriptValueSupplier start, ScriptValueSupplier end) { - this.str = str; - this.start = start; - this.end = end; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SUBSTRING; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String strRes = this.str.run(context).str(); - if (strRes == null) { - ; // TODO log error once - return ScriptValue.EMPTY_STRING; - } - Number startRes = this.start.run(context).num(); - if (startRes == null) { - ; // TODO log error once - return ScriptValue.string(strRes); - } - int si = startRes.intValue(); - si = si < 0 ? strRes.length() : Math.min(si, strRes.length()); - Number endRes = this.end.run(context).num(); - if (endRes == null) { - ; // TODO log error once - return ScriptValue.string(strRes); - } - int ei = endRes.intValue(); - ei = ei < 0 ? strRes.length() : Math.min(ei, strRes.length()); - if (si > ei) { - ; // TODO log error once - return ScriptValue.string(strRes); - } - return ScriptValue.string(strRes.substring(si, ei)); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("string")) - throw new JsonParseException("Substring instruction missing parameter 'string'"); - if (!obj.has("start")) - throw new JsonParseException("Substring instruction missing parameter 'start'"); - ScriptValueSupplier str = ScriptValueSupplier.fromJson(obj.get("string")); - ScriptValueSupplier start = ScriptValueSupplier.fromJson(obj.get("start")); - ScriptValueSupplier end = obj.has("end") ? ScriptValueSupplier.fromJson(obj.get("end")) : ScriptValue.ofInt(-1); - return new SubstringInstruction(str, start, end); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/GetPropertyInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/GetPropertyInstruction.java deleted file mode 100644 index 5c08197..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/GetPropertyInstruction.java +++ /dev/null @@ -1,48 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.variables; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetPropertyInstruction implements ScriptInstruction { - - private final ScriptValueSupplier id; - - public GetPropertyInstruction(ScriptValueSupplier id) { - this.id = id; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_PROPERTY; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String idRes = this.id.run(context).str(); - if (idRes == null) { - ; // TODO: log error once - return ScriptValue.VOID; - } - // TODO: Properties access - return ScriptValue.VOID; - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("id")) - throw new JsonParseException("Get property instruction missing parameter 'id'"); - ScriptValueSupplier id = ScriptValueSupplier.fromJson(obj.get("id")); - return new GetPropertyInstruction(id); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/GetVariableInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/GetVariableInstruction.java deleted file mode 100644 index 1ceab7f..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/GetVariableInstruction.java +++ /dev/null @@ -1,47 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.variables; - -import javax.annotation.Nonnull; - -import com.google.gson.JsonObject; -import com.google.gson.JsonParseException; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class GetVariableInstruction implements ScriptInstruction { - - private final ScriptValueSupplier id; - - public GetVariableInstruction(ScriptValueSupplier id) { - this.id = id; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.GET_VARIABLE; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String idRes = this.id.run(context).str(); - if (idRes == null) { - ; // TODO: log error once - return ScriptValue.VOID; - } - return context.getVariable(idRes); - } - - public static class Serializer implements ScriptInstructionSerializer { - @Override - public ScriptInstruction deserialize(JsonObject obj) throws JsonParseException { - if (!obj.has("id")) - throw new JsonParseException("Get variable instruction missing parameter 'id'"); - ScriptValueSupplier id = ScriptValueSupplier.fromJson(obj.get("id")); - return new GetVariableInstruction(id); - } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/SetVariableInstruction.java b/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/SetVariableInstruction.java deleted file mode 100644 index f03e9a3..0000000 --- a/src/main/java/rbasamoyai/ogden/firearms/scripting/instructions/variables/SetVariableInstruction.java +++ /dev/null @@ -1,43 +0,0 @@ -package rbasamoyai.ogden.firearms.scripting.instructions.variables; - -import javax.annotation.Nonnull; - -import rbasamoyai.ogden.firearms.scripting.ScriptContext; -import rbasamoyai.ogden.firearms.scripting.ScriptInstruction; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.ScriptValueSupplier; -import rbasamoyai.ogden.firearms.scripting.instructions.TwoObjectOperatorInstruction; -import rbasamoyai.ogden.index.OgdenScriptInstructionTypes; - -public class SetVariableInstruction implements ScriptInstruction { - - private final ScriptValueSupplier id; - private final ScriptValueSupplier value; - - public SetVariableInstruction(ScriptValueSupplier id, ScriptValueSupplier value) { - this.id = id; - this.value = value; - } - - @Override public ScriptInstructionType type() { return OgdenScriptInstructionTypes.SET_VARIABLE; } - - @Nonnull - @Override - public ScriptValue run(ScriptContext context) { - String idRes = this.id.run(context).str(); - ScriptValue result = this.value.run(context); - if (idRes == null) { - ; // TODO: log error once - return result; - } - return context.setVariable(idRes, result); - } - - public static class Serializer extends TwoObjectOperatorInstruction.Serializer { - public Serializer() { super(SetVariableInstruction::new); } - @Override protected String getLeftKey() { return "id"; } - @Override protected String getRightKey() { return "value"; } - } - -} diff --git a/src/main/java/rbasamoyai/ogden/index/OgdenRegistries.java b/src/main/java/rbasamoyai/ogden/index/OgdenRegistries.java deleted file mode 100644 index 32e236b..0000000 --- a/src/main/java/rbasamoyai/ogden/index/OgdenRegistries.java +++ /dev/null @@ -1,38 +0,0 @@ -package rbasamoyai.ogden.index; - -import com.mojang.serialization.Lifecycle; - -import net.minecraft.core.MappedRegistry; -import net.minecraft.core.Registry; -import net.minecraft.core.WritableRegistry; -import net.minecraft.resources.ResourceKey; -import rbasamoyai.ogden.OgdenMod; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; - -public class OgdenRegistries { - - public static class Keys { - public static final ResourceKey> SCRIPT_INSTRUCTIONS = key("script_instructions"); - - private static ResourceKey> key(String id) { return ResourceKey.createRegistryKey(OgdenMod.resource(id)); } - } - - @SuppressWarnings("rawtypes") - private static MappedRegistry makeRegistrySimple(ResourceKey> key) { - MappedRegistry registry = new MappedRegistry<>(key, Lifecycle.stable(), null); - WritableRegistry root = (WritableRegistry) Registry.REGISTRY; - root.register(key, registry, Lifecycle.stable()); - return registry; - } - - public static MappedRegistry SCRIPT_INSTRUCTIONS; - - private static boolean initialized = false; - - public static void initRegistries() { - if (initialized) return; - SCRIPT_INSTRUCTIONS = makeRegistrySimple(Keys.SCRIPT_INSTRUCTIONS); - initialized = true; - } - -} diff --git a/src/main/java/rbasamoyai/ogden/index/OgdenScriptInstructionTypes.java b/src/main/java/rbasamoyai/ogden/index/OgdenScriptInstructionTypes.java deleted file mode 100644 index d6a5725..0000000 --- a/src/main/java/rbasamoyai/ogden/index/OgdenScriptInstructionTypes.java +++ /dev/null @@ -1,262 +0,0 @@ -package rbasamoyai.ogden.index; - -import java.util.function.Function; - -import net.minecraft.core.Registry; -import net.minecraft.nbt.ByteTag; -import net.minecraft.nbt.CompoundTag; -import net.minecraft.nbt.DoubleTag; -import net.minecraft.nbt.FloatTag; -import net.minecraft.nbt.IntTag; -import net.minecraft.nbt.LongTag; -import net.minecraft.nbt.ShortTag; -import net.minecraft.nbt.StringTag; -import net.minecraft.nbt.Tag; -import net.minecraft.world.item.ItemStack; -import rbasamoyai.ogden.OgdenMod; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionSerializer; -import rbasamoyai.ogden.firearms.scripting.ScriptInstructionType; -import rbasamoyai.ogden.firearms.scripting.ScriptValue; -import rbasamoyai.ogden.firearms.scripting.instructions.ammunition.CountAvailableAmmoInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.ammunition.GetMatchingAmmoInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.bitwise.BitwiseAndInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.bitwise.BitwiseLeftShiftInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.bitwise.BitwiseNotInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.bitwise.BitwiseOrInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.bitwise.BitwiseRightShiftInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.bitwise.BitwiseXorInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.combat.SpawnBulletInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.control.ForInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.control.IfInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.control.WhileInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.CopyItemStackInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.CreateItemStackInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.GetItemStackCountInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.GetItemStackDamageInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.GetItemStackItemInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.GrowItemStackInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.IsSameItemInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.SetItemStackCountInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.SetItemStackDamageInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.ShrinkItemStackInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.item_stack.SplitItemStackInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.list.AddListElementInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.list.AddListToListInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.list.CreateListInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.list.ListLengthInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.list.RemoveListElementsInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.list.SublistInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.AndOperatorInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.EqualsInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.GreaterThanInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.GreaterThanOrEqualToInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.LessThanInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.LessThanOrEqualToInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.NotInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.OrInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.logical.UnequalsInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.AbsInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.AddInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.CeilInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.CubeInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.CubeRootInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.DivideInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.DivideIntInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.ExpInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.FloorInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.LnInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.Log10Instruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.LogABInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.ModuloInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.MultiplyInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.NthRootInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.PowInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.RoundInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.SquareInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.SquareRootInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.math.SubtractInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.AddIndexToPathInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.CopyNbtInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.GetNbtInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.InsertNbtInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.MergeNbtInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.RemoveNbtInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.nbt.SetNbtInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.object.CreateObjectInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.object.GetObjectPropertyInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.object.RemoveObjectPropertyInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.object.SetObjectPropertyInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.random.RandomBooleanInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.random.RandomFloatInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.random.RandomGaussianInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.random.RandomIntInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.state_actions.ChangeStateInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.state_actions.HasActionTimePassedInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.state_actions.SyncAnimationInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.state_actions.UpdateActionTimerInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.string.InsertStringInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.string.SubstringInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.variables.GetPropertyInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.variables.GetVariableInstruction; -import rbasamoyai.ogden.firearms.scripting.instructions.variables.SetVariableInstruction; - -public class OgdenScriptInstructionTypes { - - //////// Mathematical operator instructions //////// - public static final ScriptInstructionType - ADD = register("add", new AddInstruction.Serializer()), - SUBTRACT = register("subtract", new SubtractInstruction.Serializer()), - MULTIPLY = register("multiply", new MultiplyInstruction.Serializer()), - DIVIDE = register("divide", new DivideInstruction.Serializer()), - DIVIDE_INT = register("divide_int", new DivideIntInstruction.Serializer()), - MODULO = register("modulo", new ModuloInstruction.Serializer()), - SQUARE = register("square", new SquareInstruction.Serializer()), - CUBE = register("cube", new CubeInstruction.Serializer()), - POW = register("pow", new PowInstruction.Serializer()), - SQUARE_ROOT = register("square_root", new SquareRootInstruction.Serializer()), - CUBE_ROOT = register("cube_root", new CubeRootInstruction.Serializer()), - NTH_ROOT = register("nth_root", new NthRootInstruction.Serializer()), - EXP = register("exp", new ExpInstruction.Serializer()), - LN = register("ln", new LnInstruction.Serializer()), - LOG10 = register("log10", new Log10Instruction.Serializer()), - LOG_AB = register("log_ab", new LogABInstruction.Serializer()), - FLOOR = register("floor", new FloorInstruction.Serializer()), - CEIL = register("ceil", new CeilInstruction.Serializer()), - ROUND = register("round", new RoundInstruction.Serializer()), - ABS = register("abs", new AbsInstruction.Serializer()); - - //////// Random generation instructions //////// - public static final ScriptInstructionType - RANDOM_INT = register("random_int", new RandomIntInstruction.Serializer()), - RANDOM_FLOAT = register("random_float", new RandomFloatInstruction.Serializer()), - RANDOM_BOOLEAN = register("random_boolean", new RandomBooleanInstruction.Serializer()), - RANDOM_GAUSSIAN = register("random_gaussian", new RandomGaussianInstruction.Serializer()); - - //////// Bitwise operator instructions //////// - public static final ScriptInstructionType - BITWISE_NOT = register("bitwise_not", new BitwiseNotInstruction.Serializer()), - BITWISE_OR = register("bitwise_or", new BitwiseOrInstruction.Serializer()), - BITWISE_AND = register("bitwise_and", new BitwiseAndInstruction.Serializer()), - BITWISE_XOR = register("bitwise_xor", new BitwiseXorInstruction.Serializer()), - BITWISE_LEFT_SHIFT = register("bitwise_left_shift", new BitwiseLeftShiftInstruction.Serializer()), - BITWISE_RIGHT_SHIFT = register("bitwise_right_shift", new BitwiseRightShiftInstruction.Serializer()); - - //////// Logical boolean operator instructions //////// - public static final ScriptInstructionType - NOT = register("not", new NotInstruction.Serializer()), - OR = register("or", new OrInstruction.Serializer()), - AND = register("and", new AndOperatorInstruction.Serializer()), - EQUALS = register("equals", new EqualsInstruction.Serializer()), - UNEQUAL = register("unequal", new UnequalsInstruction.Serializer()), - LESS_THAN = register("less_than", new LessThanInstruction.Serializer()), - LESS_THAN_OR_EQUAL_TO = register("less_than_or_equal_to", new LessThanOrEqualToInstruction.Serializer()), - GREATER_THAN = register("greater_than", new GreaterThanInstruction.Serializer()), - GREATER_THAN_OR_EQUAL_TO = register("greater_than_or_equal_to", new GreaterThanOrEqualToInstruction.Serializer()); - - //////// String operation instructions //////// - public static final ScriptInstructionType - INSERT_STRING = register("insert_string", new InsertStringInstruction.Serializer()), - SUBSTRING = register("substring", new SubstringInstruction.Serializer()); - - //////// List operation instructions //////// - public static final ScriptInstructionType - CREATE_LIST = register("create_list", new CreateListInstruction.Serializer()), - REMOVE_LIST_ELEMENTS = register("remove_list_elements", new RemoveListElementsInstruction.Serializer()), - ADD_LIST_ELEMENT = register("add_list_element", new AddListElementInstruction.Serializer()), - ADD_LIST_TO_LIST = register("add_list_to_list", new AddListToListInstruction.Serializer()), - SUBLIST = register("sublist", new SublistInstruction.Serializer()), - LIST_LENGTH = register("list_length", new ListLengthInstruction.Serializer()); - - //////// Object operation instructions //////// - public static final ScriptInstructionType - CREATE_OBJECT = register("create_object", new CreateObjectInstruction.Serializer()), - SET_OBJECT_PROPERTY = register("set_object_property", new SetObjectPropertyInstruction.Serializer()), - GET_OBJECT_PROPERTY = register("get_object_property", new GetObjectPropertyInstruction.Serializer()), - REMOVE_OBJECT_PROPERTY = register("remove_object_property", new RemoveObjectPropertyInstruction.Serializer()); - - //////// Item stack operation instructions //////// - public static final ScriptInstructionType - CREATE_ITEM_STACK = register("create_item_stack", new CreateItemStackInstruction.Serializer()), - COPY_ITEM_STACK = register("copy_item_stack", new CopyItemStackInstruction.Serializer()), - SET_ITEM_STACK_COUNT = register("set_item_stack_count", new SetItemStackCountInstruction.Serializer()), - SHRINK_ITEM_STACK = register("shrink_item_stack", new ShrinkItemStackInstruction.Serializer()), - GROW_ITEM_STACK = register("grow_item_stack", new GrowItemStackInstruction.Serializer()), - SPLIT_ITEM_STACK = register("split_item_stack", new SplitItemStackInstruction.Serializer()), - SET_ITEM_STACK_DAMAGE = register("set_item_stack_damage", new SetItemStackDamageInstruction.Serializer()), - GET_ITEM_STACK_ITEM = register("get_item_stack_item", new GetItemStackItemInstruction.Serializer()), - GET_ITEM_STACK_COUNT = register("get_item_stack_count", new GetItemStackCountInstruction.Serializer()), - GET_ITEM_STACK_DAMAGE = register("get_item_stack_damage", new GetItemStackDamageInstruction.Serializer()), - IS_SAME_ITEM = register("is_same_item", new IsSameItemInstruction.Serializer()); - - //////// Control structure instructions //////// - public static final ScriptInstructionType - IF = register("if", new IfInstruction.Serializer()), - FOR = register("for", new ForInstruction.Serializer()), - WHILE = register("while", new WhileInstruction.Serializer()); - - //////// Variables and data instructions //////// - public static final ScriptInstructionType - SET_VARIABLE = register("set_variable", new SetVariableInstruction.Serializer()), - GET_VARIABLE = register("get_variable", new GetVariableInstruction.Serializer()), - GET_PROPERTY = register("get_property", new GetPropertyInstruction.Serializer()); - - //////// Firearm state instructions //////// - public static final ScriptInstructionType - CHANGE_STATE = register("change_state", new ChangeStateInstruction.Serializer()), - HAS_ACTION_TIME_PASSED = register("has_action_time_passed", new HasActionTimePassedInstruction.Serializer()), - UPDATE_ACTION_TIMER = register("update_action_timer", new UpdateActionTimerInstruction.Serializer()), - SYNC_ANIMATION = register("sync_animation", new SyncAnimationInstruction.Serializer()); - - //////// Ammunition management instructions //////// - public static final ScriptInstructionType - COUNT_AVAILABLE_AMMO = register("count_available_ammo", new CountAvailableAmmoInstruction.Serializer()), - GET_MATCHING_AMMO = register("get_matching_ammo", new GetMatchingAmmoInstruction.Serializer()); - - //////// Combat instructions //////// - public static final ScriptInstructionType - SPAWN_BULLET = register("spawn_bullet", new SpawnBulletInstruction.Serializer()); - - //////// Camera manipulation instructions //////// - - //////// NBT instructions //////// - public static final ScriptInstructionType - ADD_INDEX_TO_PATH = register("add_index_to_path", new AddIndexToPathInstruction.Serializer()), - REMOVE_NBT = register("remove_nbt", new RemoveNbtInstruction.Serializer()), - COPY_NBT = register("copy_nbt", new CopyNbtInstruction.Serializer()), - MERGE_NBT = register("merge_nbt", new MergeNbtInstruction.Serializer()), - INSERT_NBT = register("insert_nbt", new InsertNbtInstruction.Serializer()), - GET_NBT_INT = register("get_nbt_int", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_INT, ScriptValue.ZERO)), - GET_NBT_BYTE = register("get_nbt_byte", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_BYTE, ScriptValue.ZERO)), - GET_NBT_SHORT = register("get_nbt_short", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_SHORT, ScriptValue.ZERO)), - GET_NBT_LONG = register("get_nbt_long", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_LONG, ScriptValue.ZERO)), - GET_NBT_FLOAT = register("get_nbt_float", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_FLOAT, ScriptValue.ZERO)), - GET_NBT_DOUBLE = register("get_nbt_double", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_DOUBLE, ScriptValue.ZERO)), - GET_NBT_BOOLEAN = register("get_nbt_boolean", GetNbtInstruction.Serializer.provider(tag -> ScriptValue.bool(((ByteTag) tag).getAsByte() != (byte) 0), Tag.TAG_BYTE, ScriptValue.FALSE)), - GET_NBT_STRING = register("get_nbt_string", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_STRING, ScriptValue.EMPTY_STRING)), - GET_NBT_LIST = register("get_nbt_list", GetNbtInstruction.Serializer.provider(GetNbtInstruction::getScriptValueList)), - GET_NBT_OBJECT = register("get_nbt_object", GetNbtInstruction.Serializer.provider(ScriptValue::fromNbt, Tag.TAG_COMPOUND, ScriptValue.EMPTY_OBJECT)), - GET_NBT_ITEM_STACK = register("get_nbt_item_stack", GetNbtInstruction.Serializer.provider(tag -> ScriptValue.itemStack(ItemStack.of((CompoundTag) tag)), Tag.TAG_COMPOUND, ScriptValue.EMPTY_ITEM_STACK)), - SET_NBT_INT = register("set_nbt_int", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::num, Number::intValue, IntTag::valueOf))), - SET_NBT_BYTE = register("set_nbt_byte", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::num, Number::byteValue, ByteTag::valueOf))), - SET_NBT_SHORT = register("set_nbt_short", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::num, Number::shortValue, ShortTag::valueOf))), - SET_NBT_LONG = register("set_nbt_long", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::num, Number::longValue, LongTag::valueOf))), - SET_NBT_FLOAT = register("set_nbt_float", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::num, Number::floatValue, FloatTag::valueOf))), - SET_NBT_DOUBLE = register("set_nbt_double", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::num, Number::doubleValue, DoubleTag::valueOf))), - SET_NBT_BOOLEAN = register("set_nbt_boolean", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::bool, ByteTag::valueOf))), - SET_NBT_STRING = register("set_nbt_string", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::str, StringTag::valueOf))), - SET_NBT_LIST = register("set_nbt_list", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::list, SetNbtInstruction::getListTag))), - SET_NBT_OBJECT = register("set_nbt_object", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::map, SetNbtInstruction::getObjectTag))), - SET_NBT_ITEM_STACK = register("set_nbt_item_stack", SetNbtInstruction.Serializer.provider(SetNbtInstruction.Getter.fromFunctions(ScriptValue::stack, SetNbtInstruction::getItemStackTag))); - - private static ScriptInstructionType register(String id, ScriptInstructionSerializer ser) { - return Registry.register(OgdenRegistries.SCRIPT_INSTRUCTIONS, OgdenMod.resource(id), new ScriptInstructionType(ser)); - } - - private static ScriptInstructionType register(String id, Function serProv) { - return Registry.register(OgdenRegistries.SCRIPT_INSTRUCTIONS, OgdenMod.resource(id), new ScriptInstructionType(serProv)); - } - - public static void register() { - } - -} diff --git a/src/main/java/rbasamoyai/ogden/network/OgdenNetwork.java b/src/main/java/rbasamoyai/ogden/network/OgdenNetwork.java index e9e085c..affca04 100644 --- a/src/main/java/rbasamoyai/ogden/network/OgdenNetwork.java +++ b/src/main/java/rbasamoyai/ogden/network/OgdenNetwork.java @@ -7,12 +7,10 @@ import net.minecraftforge.network.simple.SimpleChannel; import rbasamoyai.ogden.OgdenMod; import rbasamoyai.ogden.ammunition.AmmunitionPropertiesHandler.ClientboundSyncAmmunitionPropertiesPacket; -import rbasamoyai.ogden.ammunition.clips.ClipPropertiesHandler.ClientboundSyncClipPropertiesPacket; -import rbasamoyai.ogden.firearms.config.FirearmAcceptedAmmunitionHandler.ClientboundSyncFirearmAcceptedAmmunitionPacket; public class OgdenNetwork { - public static final String VERSION = "1.0.0"; + public static final String VERSION = "0.0.1"; public static final SimpleChannel INSTANCE = construct(); @@ -29,8 +27,6 @@ public static SimpleChannel construct() { addStandardPacket(channel, id++, ClientboundCheckChannelVersionPacket.class, ClientboundCheckChannelVersionPacket::new); addStandardPacket(channel, id++, ClientboundSyncOgdenProjectile.class, ClientboundSyncOgdenProjectile::new); addStandardPacket(channel, id++, ClientboundSyncAmmunitionPropertiesPacket.class, ClientboundSyncAmmunitionPropertiesPacket::copyOf); - addStandardPacket(channel, id++, ClientboundSyncFirearmAcceptedAmmunitionPacket.class, ClientboundSyncFirearmAcceptedAmmunitionPacket::copyOf); - addStandardPacket(channel, id++, ClientboundSyncClipPropertiesPacket.class, ClientboundSyncClipPropertiesPacket::copyOf); return channel; }