From 3413e2bc3f1a0e53918fba8ed11bff679def2a09 Mon Sep 17 00:00:00 2001 From: pietro-lopes Date: Wed, 27 Sep 2023 10:14:08 -0300 Subject: [PATCH 1/5] feat: added afterPlace event --- .../almostreliable/morejs/core/Events.java | 2 + .../structure/StructureAfterPlaceEventJS.java | 149 ++++++++++++++++++ .../mixin/structure/StructuresMixin.java | 43 +++++ .../main/resources/morejs-common.mixins.json | 71 +++++---- 4 files changed, 231 insertions(+), 34 deletions(-) create mode 100644 Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java create mode 100644 Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java diff --git a/Common/src/main/java/com/almostreliable/morejs/core/Events.java b/Common/src/main/java/com/almostreliable/morejs/core/Events.java index 2b0f0bf..aec6834 100644 --- a/Common/src/main/java/com/almostreliable/morejs/core/Events.java +++ b/Common/src/main/java/com/almostreliable/morejs/core/Events.java @@ -7,6 +7,7 @@ import com.almostreliable.morejs.features.misc.PiglinPlayerBehaviorEventJS; import com.almostreliable.morejs.features.potion.PotionBrewingRegisterEvent; import com.almostreliable.morejs.features.structure.StructureLoadEventJS; +import com.almostreliable.morejs.features.structure.StructureAfterPlaceEventJS; import com.almostreliable.morejs.features.teleport.EntityTeleportsEventJS; import com.almostreliable.morejs.features.villager.events.*; import dev.latvian.mods.kubejs.event.EventGroup; @@ -28,6 +29,7 @@ public interface Events { EventHandler ENCHANTMENT_TABLE_TOOLTIP = GROUP.client("enchantmentTableTooltip", () -> EnchantmentTableTooltipEventJS.class); EventHandler TELEPORT = GROUP.server("teleport", () -> EntityTeleportsEventJS.class).hasResult(); EventHandler STRUCTURE_LOAD = GROUP.server("structureLoad", () -> StructureLoadEventJS.class); + EventHandler STRUCTURE_AFTER_PLACE = GROUP.server("structureAfterPlace", () -> StructureAfterPlaceEventJS.class); EventHandler XP_CHANGE = GROUP.server("playerXpChange", () -> ExperiencePlayerEventJS.class).hasResult(); EventHandler PIGLIN_PLAYER_BEHAVIOR = GROUP.server("piglinPlayerBehavior", () -> PiglinPlayerBehaviorEventJS.class); EventHandler POTION_BREWING_REGISTER = GROUP.startup("registerPotionBrewing", diff --git a/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java b/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java new file mode 100644 index 0000000..f032f4e --- /dev/null +++ b/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java @@ -0,0 +1,149 @@ +package com.almostreliable.morejs.features.structure; + +import com.almostreliable.morejs.core.Events; +import dev.latvian.mods.kubejs.level.LevelEventJS; +import dev.latvian.mods.rhino.util.HideFromJS; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.StructureManager; +import net.minecraft.world.level.WorldGenLevel; +import net.minecraft.world.level.chunk.ChunkGenerator; +import net.minecraft.world.level.levelgen.structure.BoundingBox; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraft.world.level.levelgen.structure.StructurePiece; +import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer; +import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType; +import net.minecraft.world.phys.AABB; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +public class StructureAfterPlaceEventJS extends LevelEventJS { + + private final Structure structure; + private final WorldGenLevel worldGenLevel; + private final StructureManager structureManager; + private final ChunkGenerator chunkGenerator; + private final RandomSource randomSource; + private final BoundingBox boundingBox; + private final ChunkPos chunkPos; + private final PiecesContainer piecesContainer; + private Map intersectionMap; + + public StructureAfterPlaceEventJS(Structure structure, WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer) { + this.structure = structure; + this.worldGenLevel = worldGenLevel; + this.structureManager = structureManager; + this.chunkGenerator = chunkGenerator; + this.randomSource = randomSource; + this.boundingBox = boundingBox; + this.chunkPos = chunkPos; + this.piecesContainer = piecesContainer; + } + + public Structure getStructure() { + return structure; + } + + public StructureManager getStructureManager() { + return structureManager; + } + + public ChunkGenerator getChunkGenerator() { + return chunkGenerator; + } + + public RandomSource getRandomSource() { + return randomSource; + } + + public BoundingBox getChunkBoundingBox() { + return boundingBox; + } + + public ChunkPos getChunkPos() { + return chunkPos; + } + + public PiecesContainer getPiecesContainer() { + return piecesContainer; + } + + public BoundingBox getStructureBoundingBox() { + return piecesContainer.calculateBoundingBox(); + } + + public ServerLevel getLevel() { + return worldGenLevel.getLevel(); + } + + public WorldGenLevel getWorldGenLevel() { + return worldGenLevel; + } + + public static ResourceLocation getPieceType(StructurePieceType pieceType) { + return Objects.requireNonNull(BuiltInRegistries.STRUCTURE_PIECE.getKey(pieceType)); + } + + public ResourceLocation getId() { + return Objects.requireNonNull(structureManager + .registryAccess() + .registryOrThrow(Registries.STRUCTURE) + .getKey(structure)); + } + + public String getGenStep() { + return structure.step().getName(); + } + + public List getIntersectionBoxes() { + return getIntersectionMap().values().stream().toList(); + } + + public List getIntersectionPieces() { + return getIntersectionMap().keySet().stream().toList(); + } + + public Map getIntersectionMap() { + if (intersectionMap == null) { + Map map = new HashMap<>(); + for (StructurePiece sp : piecesContainer.pieces()) { + if (boundingBox.intersects(sp.getBoundingBox())) { + AABB aabb = AABB.of(boundingBox).intersect(AABB.of(sp.getBoundingBox())); + map.put(sp, + new BoundingBox((int) aabb.minX, + (int) aabb.minY, + (int) aabb.minZ, + (int) aabb.maxX - 1, + (int) aabb.maxY - 1, + (int) aabb.maxZ - 1)); + } + } + intersectionMap = map; + } + return intersectionMap; + } + + public ResourceLocation getType() { + return Objects.requireNonNull(BuiltInRegistries.STRUCTURE_TYPE.getKey(structure.type())); + } + + @HideFromJS + public static void invoke(Structure structure, WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer) { + var event = new StructureAfterPlaceEventJS(structure, + worldGenLevel, + structureManager, + chunkGenerator, + randomSource, + boundingBox, + chunkPos, + piecesContainer); + Events.STRUCTURE_AFTER_PLACE.post(event); + } +} diff --git a/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java b/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java new file mode 100644 index 0000000..7c7a393 --- /dev/null +++ b/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java @@ -0,0 +1,43 @@ +package com.almostreliable.morejs.mixin.structure; + +import com.almostreliable.morejs.features.structure.StructureAfterPlaceEventJS; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.StructureManager; +import net.minecraft.world.level.WorldGenLevel; +import net.minecraft.world.level.chunk.ChunkGenerator; +import net.minecraft.world.level.levelgen.structure.BoundingBox; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer; +import net.minecraft.world.level.levelgen.structure.structures.DesertPyramidStructure; +import net.minecraft.world.level.levelgen.structure.structures.WoodlandMansionStructure; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +public class StructuresMixin { + @Mixin(Structure.class) + public static class StructureMixin { + @Inject(method = "afterPlace", at = @At("RETURN")) + private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer, CallbackInfo ci) { + StructureAfterPlaceEventJS.invoke((Structure) (Object) this, worldGenLevel, structureManager, chunkGenerator, randomSource, boundingBox, chunkPos, piecesContainer); + } + } + + @Mixin(DesertPyramidStructure.class) + public static class DesertPyramidStructureMixin { + @Inject(method = "afterPlace", at = @At("RETURN")) + private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer, CallbackInfo ci) { + StructureAfterPlaceEventJS.invoke((Structure) (Object) this, worldGenLevel, structureManager, chunkGenerator, randomSource, boundingBox, chunkPos, piecesContainer); + } + } + + @Mixin(WoodlandMansionStructure.class) + public static class WoodlandMansionStructureMixin { + @Inject(method = "afterPlace", at = @At("RETURN")) + private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer, CallbackInfo ci) { + StructureAfterPlaceEventJS.invoke((Structure) (Object) this, worldGenLevel, structureManager, chunkGenerator, randomSource, boundingBox, chunkPos, piecesContainer); + } + } +} diff --git a/Common/src/main/resources/morejs-common.mixins.json b/Common/src/main/resources/morejs-common.mixins.json index d60499a..96287ef 100644 --- a/Common/src/main/resources/morejs-common.mixins.json +++ b/Common/src/main/resources/morejs-common.mixins.json @@ -1,37 +1,40 @@ { - "required": true, - "minVersion": "0.8", - "package": "com.almostreliable.morejs.mixin", - "refmap": "morejs.refmap.json", - "compatibilityLevel": "JAVA_17", - "mixins": [ - "enchanting.EnchantmentHelperMixin", - "enchanting.EnchantmentMenuMixin", - "enchanting.EnchantmentMixin", - "entity.PiglinSpecificSensorMixin", - "potion.PotionBrewingAccessor", - "structure.StructureBlockInfoMixin", - "structure.StructureManagerMixin", - "structure.StructureTemplateMixin", - "villager.AbstractVillagerMixin", - "villager.MerchantMenuMixin", - "villager.MerchantOfferMixin", - "villager.MerchantOffersMixin", - "villager.VillagerTradesMixin$EmeraldForItemsMixin", - "villager.VillagerTradesMixin$EmeraldsForVillagerTypeItemMixin", - "villager.VillagerTradesMixin$EnchantBookForEmeraldsMixin", - "villager.VillagerTradesMixin$EnchantedItemForEmeraldsMixin", - "villager.VillagerTradesMixin$ItemsAndEmeraldsToItemsMixin", - "villager.VillagerTradesMixin$ItemsForEmeraldsMixin", - "villager.VillagerTradesMixin$SuspiciousStewForEmeraldMixin", - "villager.VillagerTradesMixin$TippedArrowForItemsAndEmeraldsMixin", - "villager.VillagerTradesMixin$TreasureMapForEmeraldsMixin" - ], - "client": [ - "enchanting.EnchantmentScreenMixin", - "villager.MerchantScreenMixin" - ], - "injectors": { - "defaultRequire": 1 + "required": true, + "minVersion": "0.8", + "package": "com.almostreliable.morejs.mixin", + "refmap": "morejs.refmap.json", + "compatibilityLevel": "JAVA_17", + "mixins": [ + "enchanting.EnchantmentHelperMixin", + "enchanting.EnchantmentMenuMixin", + "enchanting.EnchantmentMixin", + "entity.PiglinSpecificSensorMixin", + "potion.PotionBrewingAccessor", + "structure.StructureBlockInfoMixin", + "structure.StructureManagerMixin", + "structure.StructuresMixin$DesertPyramidStructureMixin", + "structure.StructuresMixin$StructureMixin", + "structure.StructuresMixin$WoodlandMansionStructureMixin", + "structure.StructureTemplateMixin", + "villager.AbstractVillagerMixin", + "villager.MerchantMenuMixin", + "villager.MerchantOfferMixin", + "villager.MerchantOffersMixin", + "villager.VillagerTradesMixin$EmeraldForItemsMixin", + "villager.VillagerTradesMixin$EmeraldsForVillagerTypeItemMixin", + "villager.VillagerTradesMixin$EnchantBookForEmeraldsMixin", + "villager.VillagerTradesMixin$EnchantedItemForEmeraldsMixin", + "villager.VillagerTradesMixin$ItemsAndEmeraldsToItemsMixin", + "villager.VillagerTradesMixin$ItemsForEmeraldsMixin", + "villager.VillagerTradesMixin$SuspiciousStewForEmeraldMixin", + "villager.VillagerTradesMixin$TippedArrowForItemsAndEmeraldsMixin", + "villager.VillagerTradesMixin$TreasureMapForEmeraldsMixin" + ], + "client": [ + "enchanting.EnchantmentScreenMixin", + "villager.MerchantScreenMixin" + ], + "injectors": { + "defaultRequire": 1 } } From c110d63cd85fc1d7d3a9f9d14a08465423fdb286 Mon Sep 17 00:00:00 2001 From: pietro-lopes Date: Wed, 27 Sep 2023 12:10:54 -0300 Subject: [PATCH 2/5] fix .editorconfig and recommit json --- .editorconfig | 16 ++-- .../main/resources/morejs-common.mixins.json | 74 +++++++++---------- 2 files changed, 45 insertions(+), 45 deletions(-) diff --git a/.editorconfig b/.editorconfig index faed97b..4d47f99 100644 --- a/.editorconfig +++ b/.editorconfig @@ -277,7 +277,7 @@ ij_editorconfig_space_before_colon = false ij_editorconfig_space_before_comma = false ij_editorconfig_spaces_around_assignment_operators = true -[{*.ant, *.fxml, *.jhm, *.jnlp, *.jrxml, *.jspx, *.pom, *.rng, *.tagx, *.tld, *.wsdl, *.xml, *.xsd, *.xsl, *.xslt, *.xul}] +[{*.ant,*.fxml,*.jhm,*.jnlp,*.jrxml,*.jspx,*.pom,*.rng,*.tagx,*.tld,*.wsdl,*.xml,*.xsd,*.xsl,*.xslt,*.xul}] ij_visual_guides = none ij_xml_align_attributes = true ij_xml_align_text = false @@ -297,7 +297,7 @@ ij_xml_space_inside_empty_tag = false ij_xml_text_wrap = normal ij_xml_use_custom_settings = false -[{*.bash, *.sh, *.zsh}] +[{*.bash,*.sh,*.zsh}] indent_size = 2 tab_width = 2 ij_visual_guides = none @@ -308,7 +308,7 @@ ij_shell_redirect_followed_by_space = false ij_shell_switch_cases_indented = false ij_shell_use_unix_line_separator = true -[{*.gant, *.gradle, *.groovy, *.gy}] +[{*.gant,*.gradle,*.groovy,*.gy}] ij_visual_guides = none ij_groovy_align_group_field_declarations = false ij_groovy_align_multiline_array_initializer_expression = false @@ -571,7 +571,7 @@ ij_kotlin_wrap_elvis_expressions = 1 ij_kotlin_wrap_expression_body_functions = 1 ij_kotlin_wrap_first_method_in_call_chain = false -[{*.har, *.json, mcmod.info, pack.mcmeta}] +[{*.har,*.json,mcmod.info,pack.mcmeta}] indent_size = 2 max_line_length = 150 tab_width = 2 @@ -588,7 +588,7 @@ ij_json_spaces_within_braces = false ij_json_spaces_within_brackets = false ij_json_wrap_long_lines = false -[{*.htm, *.html, *.sht, *.shtm, *.shtml}] +[{*.htm,*.html,*.sht,*.shtm,*.shtml}] ij_visual_guides = none ij_html_add_new_line_before_tags = body, div, p, form, h1, h2, h3 ij_html_align_attributes = true @@ -617,7 +617,7 @@ ij_html_space_inside_empty_tag = false ij_html_text_wrap = normal ij_html_uniform_ident = false -[{*.markdown, *.md}] +[{*.markdown,*.md}] ij_visual_guides = none ij_markdown_force_one_space_after_blockquote_symbol = true ij_markdown_force_one_space_after_header_symbol = true @@ -631,11 +631,11 @@ ij_markdown_min_lines_around_block_elements = 1 ij_markdown_min_lines_around_header = 1 ij_markdown_min_lines_between_paragraphs = 1 -[{*.toml, Cargo.lock, Cargo.toml.orig, Gopkg.lock, Pipfile, poetry.lock}] +[{*.toml,Cargo.lock,Cargo.toml.orig,Gopkg.lock,Pipfile,poetry.lock}] ij_visual_guides = none ij_toml_keep_indents_on_empty_lines = false -[{*.yaml, *.yml}] +[{*.yaml,*.yml}] indent_size = 2 ij_visual_guides = none ij_yaml_align_values_properties = do_not_align diff --git a/Common/src/main/resources/morejs-common.mixins.json b/Common/src/main/resources/morejs-common.mixins.json index 96287ef..355e999 100644 --- a/Common/src/main/resources/morejs-common.mixins.json +++ b/Common/src/main/resources/morejs-common.mixins.json @@ -1,40 +1,40 @@ { - "required": true, - "minVersion": "0.8", - "package": "com.almostreliable.morejs.mixin", - "refmap": "morejs.refmap.json", - "compatibilityLevel": "JAVA_17", - "mixins": [ - "enchanting.EnchantmentHelperMixin", - "enchanting.EnchantmentMenuMixin", - "enchanting.EnchantmentMixin", - "entity.PiglinSpecificSensorMixin", - "potion.PotionBrewingAccessor", - "structure.StructureBlockInfoMixin", - "structure.StructureManagerMixin", - "structure.StructuresMixin$DesertPyramidStructureMixin", - "structure.StructuresMixin$StructureMixin", - "structure.StructuresMixin$WoodlandMansionStructureMixin", - "structure.StructureTemplateMixin", - "villager.AbstractVillagerMixin", - "villager.MerchantMenuMixin", - "villager.MerchantOfferMixin", - "villager.MerchantOffersMixin", - "villager.VillagerTradesMixin$EmeraldForItemsMixin", - "villager.VillagerTradesMixin$EmeraldsForVillagerTypeItemMixin", - "villager.VillagerTradesMixin$EnchantBookForEmeraldsMixin", - "villager.VillagerTradesMixin$EnchantedItemForEmeraldsMixin", - "villager.VillagerTradesMixin$ItemsAndEmeraldsToItemsMixin", - "villager.VillagerTradesMixin$ItemsForEmeraldsMixin", - "villager.VillagerTradesMixin$SuspiciousStewForEmeraldMixin", - "villager.VillagerTradesMixin$TippedArrowForItemsAndEmeraldsMixin", - "villager.VillagerTradesMixin$TreasureMapForEmeraldsMixin" - ], - "client": [ - "enchanting.EnchantmentScreenMixin", - "villager.MerchantScreenMixin" - ], - "injectors": { - "defaultRequire": 1 + "required" : true, + "minVersion" : "0.8", + "package" : "com.almostreliable.morejs.mixin", + "refmap" : "morejs.refmap.json", + "compatibilityLevel" : "JAVA_17", + "mixins" : [ + "enchanting.EnchantmentHelperMixin", + "enchanting.EnchantmentMenuMixin", + "enchanting.EnchantmentMixin", + "entity.PiglinSpecificSensorMixin", + "potion.PotionBrewingAccessor", + "structure.StructureBlockInfoMixin", + "structure.StructureManagerMixin", + "structure.StructuresMixin$DesertPyramidStructureMixin", + "structure.StructuresMixin$StructureMixin", + "structure.StructuresMixin$WoodlandMansionStructureMixin", + "structure.StructureTemplateMixin", + "villager.AbstractVillagerMixin", + "villager.MerchantMenuMixin", + "villager.MerchantOfferMixin", + "villager.MerchantOffersMixin", + "villager.VillagerTradesMixin$EmeraldForItemsMixin", + "villager.VillagerTradesMixin$EmeraldsForVillagerTypeItemMixin", + "villager.VillagerTradesMixin$EnchantBookForEmeraldsMixin", + "villager.VillagerTradesMixin$EnchantedItemForEmeraldsMixin", + "villager.VillagerTradesMixin$ItemsAndEmeraldsToItemsMixin", + "villager.VillagerTradesMixin$ItemsForEmeraldsMixin", + "villager.VillagerTradesMixin$SuspiciousStewForEmeraldMixin", + "villager.VillagerTradesMixin$TippedArrowForItemsAndEmeraldsMixin", + "villager.VillagerTradesMixin$TreasureMapForEmeraldsMixin" + ], + "client" : [ + "enchanting.EnchantmentScreenMixin", + "villager.MerchantScreenMixin" + ], + "injectors" : { + "defaultRequire" : 1 } } From 5e6c50c82d93b024def90e57375b376c30d17eb3 Mon Sep 17 00:00:00 2001 From: pietro-lopes Date: Wed, 27 Sep 2023 13:01:28 -0300 Subject: [PATCH 3/5] fix .editorconfig --- .editorconfig | 2 +- .../main/resources/morejs-common.mixins.json | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.editorconfig b/.editorconfig index 4d47f99..9c59eb9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -582,7 +582,7 @@ ij_json_keep_indents_on_empty_lines = false ij_json_keep_line_breaks = true ij_json_space_after_colon = true ij_json_space_after_comma = true -ij_json_space_before_colon = true +ij_json_space_before_colon = false ij_json_space_before_comma = false ij_json_spaces_within_braces = false ij_json_spaces_within_brackets = false diff --git a/Common/src/main/resources/morejs-common.mixins.json b/Common/src/main/resources/morejs-common.mixins.json index 355e999..b27485c 100644 --- a/Common/src/main/resources/morejs-common.mixins.json +++ b/Common/src/main/resources/morejs-common.mixins.json @@ -1,10 +1,10 @@ { - "required" : true, - "minVersion" : "0.8", - "package" : "com.almostreliable.morejs.mixin", - "refmap" : "morejs.refmap.json", - "compatibilityLevel" : "JAVA_17", - "mixins" : [ + "required": true, + "minVersion": "0.8", + "package": "com.almostreliable.morejs.mixin", + "refmap": "morejs.refmap.json", + "compatibilityLevel": "JAVA_17", + "mixins": [ "enchanting.EnchantmentHelperMixin", "enchanting.EnchantmentMenuMixin", "enchanting.EnchantmentMixin", @@ -30,11 +30,11 @@ "villager.VillagerTradesMixin$TippedArrowForItemsAndEmeraldsMixin", "villager.VillagerTradesMixin$TreasureMapForEmeraldsMixin" ], - "client" : [ + "client": [ "enchanting.EnchantmentScreenMixin", "villager.MerchantScreenMixin" ], - "injectors" : { - "defaultRequire" : 1 + "injectors": { + "defaultRequire": 1 } } From f7c40b485db0c0ef06bd367c51ff96944e5912cd Mon Sep 17 00:00:00 2001 From: pietro-lopes Date: Sun, 1 Oct 2023 13:18:05 -0300 Subject: [PATCH 4/5] injecting at proper place now --- .../structure/StructureAfterPlaceEventJS.java | 17 +------- .../mixin/structure/StructureStartMixin.java | 41 ++++++++++++++++++ .../mixin/structure/StructuresMixin.java | 43 ------------------- .../main/resources/morejs-common.mixins.json | 4 +- 4 files changed, 43 insertions(+), 62 deletions(-) create mode 100644 Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructureStartMixin.java delete mode 100644 Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java diff --git a/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java b/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java index f032f4e..c98c1d9 100644 --- a/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java +++ b/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java @@ -1,8 +1,6 @@ package com.almostreliable.morejs.features.structure; -import com.almostreliable.morejs.core.Events; import dev.latvian.mods.kubejs.level.LevelEventJS; -import dev.latvian.mods.rhino.util.HideFromJS; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.server.level.ServerLevel; import net.minecraft.core.registries.Registries; @@ -87,7 +85,7 @@ public WorldGenLevel getWorldGenLevel() { return worldGenLevel; } - public static ResourceLocation getPieceType(StructurePieceType pieceType) { + public ResourceLocation getPieceType(StructurePieceType pieceType) { return Objects.requireNonNull(BuiltInRegistries.STRUCTURE_PIECE.getKey(pieceType)); } @@ -133,17 +131,4 @@ public Map getIntersectionMap() { public ResourceLocation getType() { return Objects.requireNonNull(BuiltInRegistries.STRUCTURE_TYPE.getKey(structure.type())); } - - @HideFromJS - public static void invoke(Structure structure, WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer) { - var event = new StructureAfterPlaceEventJS(structure, - worldGenLevel, - structureManager, - chunkGenerator, - randomSource, - boundingBox, - chunkPos, - piecesContainer); - Events.STRUCTURE_AFTER_PLACE.post(event); - } } diff --git a/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructureStartMixin.java b/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructureStartMixin.java new file mode 100644 index 0000000..5a147eb --- /dev/null +++ b/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructureStartMixin.java @@ -0,0 +1,41 @@ +package com.almostreliable.morejs.mixin.structure; + +import com.almostreliable.morejs.core.Events; +import com.almostreliable.morejs.features.structure.StructureAfterPlaceEventJS; +import net.minecraft.util.RandomSource; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.StructureManager; +import net.minecraft.world.level.WorldGenLevel; +import net.minecraft.world.level.chunk.ChunkGenerator; +import net.minecraft.world.level.levelgen.structure.BoundingBox; +import net.minecraft.world.level.levelgen.structure.Structure; +import net.minecraft.world.level.levelgen.structure.StructureStart; +import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +@Mixin(StructureStart.class) +public class StructureStartMixin { + + @Shadow @Final private PiecesContainer pieceContainer; + + @Shadow @Final private Structure structure; + + @Inject(method = "placeInChunk", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/level/levelgen/structure/Structure;afterPlace(Lnet/minecraft/world/level/WorldGenLevel;Lnet/minecraft/world/level/StructureManager;Lnet/minecraft/world/level/chunk/ChunkGenerator;Lnet/minecraft/util/RandomSource;Lnet/minecraft/world/level/levelgen/structure/BoundingBox;Lnet/minecraft/world/level/ChunkPos;Lnet/minecraft/world/level/levelgen/structure/pieces/PiecesContainer;)V", shift = At.Shift.AFTER)) + private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, CallbackInfo ci) { + if (!Events.STRUCTURE_AFTER_PLACE.hasListeners()) return; + var event = new StructureAfterPlaceEventJS(this.structure, + worldGenLevel, + structureManager, + chunkGenerator, + randomSource, + boundingBox, + chunkPos, + this.pieceContainer); + Events.STRUCTURE_AFTER_PLACE.post(event); + } +} diff --git a/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java b/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java deleted file mode 100644 index 7c7a393..0000000 --- a/Common/src/main/java/com/almostreliable/morejs/mixin/structure/StructuresMixin.java +++ /dev/null @@ -1,43 +0,0 @@ -package com.almostreliable.morejs.mixin.structure; - -import com.almostreliable.morejs.features.structure.StructureAfterPlaceEventJS; -import net.minecraft.util.RandomSource; -import net.minecraft.world.level.ChunkPos; -import net.minecraft.world.level.StructureManager; -import net.minecraft.world.level.WorldGenLevel; -import net.minecraft.world.level.chunk.ChunkGenerator; -import net.minecraft.world.level.levelgen.structure.BoundingBox; -import net.minecraft.world.level.levelgen.structure.Structure; -import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer; -import net.minecraft.world.level.levelgen.structure.structures.DesertPyramidStructure; -import net.minecraft.world.level.levelgen.structure.structures.WoodlandMansionStructure; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; - -public class StructuresMixin { - @Mixin(Structure.class) - public static class StructureMixin { - @Inject(method = "afterPlace", at = @At("RETURN")) - private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer, CallbackInfo ci) { - StructureAfterPlaceEventJS.invoke((Structure) (Object) this, worldGenLevel, structureManager, chunkGenerator, randomSource, boundingBox, chunkPos, piecesContainer); - } - } - - @Mixin(DesertPyramidStructure.class) - public static class DesertPyramidStructureMixin { - @Inject(method = "afterPlace", at = @At("RETURN")) - private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer, CallbackInfo ci) { - StructureAfterPlaceEventJS.invoke((Structure) (Object) this, worldGenLevel, structureManager, chunkGenerator, randomSource, boundingBox, chunkPos, piecesContainer); - } - } - - @Mixin(WoodlandMansionStructure.class) - public static class WoodlandMansionStructureMixin { - @Inject(method = "afterPlace", at = @At("RETURN")) - private void morejs$invokeEventAfterPlace(WorldGenLevel worldGenLevel, StructureManager structureManager, ChunkGenerator chunkGenerator, RandomSource randomSource, BoundingBox boundingBox, ChunkPos chunkPos, PiecesContainer piecesContainer, CallbackInfo ci) { - StructureAfterPlaceEventJS.invoke((Structure) (Object) this, worldGenLevel, structureManager, chunkGenerator, randomSource, boundingBox, chunkPos, piecesContainer); - } - } -} diff --git a/Common/src/main/resources/morejs-common.mixins.json b/Common/src/main/resources/morejs-common.mixins.json index b27485c..f8588ea 100644 --- a/Common/src/main/resources/morejs-common.mixins.json +++ b/Common/src/main/resources/morejs-common.mixins.json @@ -12,9 +12,7 @@ "potion.PotionBrewingAccessor", "structure.StructureBlockInfoMixin", "structure.StructureManagerMixin", - "structure.StructuresMixin$DesertPyramidStructureMixin", - "structure.StructuresMixin$StructureMixin", - "structure.StructuresMixin$WoodlandMansionStructureMixin", + "structure.StructureStartMixin", "structure.StructureTemplateMixin", "villager.AbstractVillagerMixin", "villager.MerchantMenuMixin", From 8f970d69af6e584d3ec16ccfea70f68520cfd113 Mon Sep 17 00:00:00 2001 From: LLytho Date: Tue, 17 Oct 2023 13:24:57 +0200 Subject: [PATCH 5/5] Update StructureAfterPlaceEventJS.java --- .../structure/StructureAfterPlaceEventJS.java | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java b/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java index c98c1d9..cd4fb43 100644 --- a/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java +++ b/Common/src/main/java/com/almostreliable/morejs/features/structure/StructureAfterPlaceEventJS.java @@ -2,9 +2,9 @@ import dev.latvian.mods.kubejs.level.LevelEventJS; import net.minecraft.core.registries.BuiltInRegistries; -import net.minecraft.server.level.ServerLevel; import net.minecraft.core.registries.Registries; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; import net.minecraft.util.RandomSource; import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.StructureManager; @@ -17,10 +17,7 @@ import net.minecraft.world.level.levelgen.structure.pieces.StructurePieceType; import net.minecraft.world.phys.AABB; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; public class StructureAfterPlaceEventJS extends LevelEventJS { @@ -100,12 +97,12 @@ public String getGenStep() { return structure.step().getName(); } - public List getIntersectionBoxes() { - return getIntersectionMap().values().stream().toList(); + public Collection getIntersectionBoxes() { + return getIntersectionMap().values(); } - public List getIntersectionPieces() { - return getIntersectionMap().keySet().stream().toList(); + public Collection getIntersectionPieces() { + return getIntersectionMap().keySet(); } public Map getIntersectionMap() { @@ -114,17 +111,19 @@ public Map getIntersectionMap() { for (StructurePiece sp : piecesContainer.pieces()) { if (boundingBox.intersects(sp.getBoundingBox())) { AABB aabb = AABB.of(boundingBox).intersect(AABB.of(sp.getBoundingBox())); - map.put(sp, - new BoundingBox((int) aabb.minX, - (int) aabb.minY, - (int) aabb.minZ, - (int) aabb.maxX - 1, - (int) aabb.maxY - 1, - (int) aabb.maxZ - 1)); + BoundingBox bb = new BoundingBox((int) aabb.minX, + (int) aabb.minY, + (int) aabb.minZ, + (int) aabb.maxX - 1, + (int) aabb.maxY - 1, + (int) aabb.maxZ - 1); + map.put(sp, bb); } } + intersectionMap = map; } + return intersectionMap; }