Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added afterPlace event #7

Merged
merged 5 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -582,13 +582,13 @@ 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
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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.almostreliable.morejs.features.structure;

import dev.latvian.mods.kubejs.level.LevelEventJS;
import net.minecraft.core.registries.BuiltInRegistries;
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;
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.*;

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<StructurePiece, BoundingBox> 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 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 Collection<BoundingBox> getIntersectionBoxes() {
return getIntersectionMap().values();
}

public Collection<StructurePiece> getIntersectionPieces() {
return getIntersectionMap().keySet();
}

public Map<StructurePiece, BoundingBox> getIntersectionMap() {
if (intersectionMap == null) {
Map<StructurePiece, BoundingBox> map = new HashMap<>();
for (StructurePiece sp : piecesContainer.pieces()) {
if (boundingBox.intersects(sp.getBoundingBox())) {
AABB aabb = AABB.of(boundingBox).intersect(AABB.of(sp.getBoundingBox()));
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;
}

public ResourceLocation getType() {
return Objects.requireNonNull(BuiltInRegistries.STRUCTURE_TYPE.getKey(structure.type()));
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
1 change: 1 addition & 0 deletions Common/src/main/resources/morejs-common.mixins.json
rlnt marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"potion.PotionBrewingAccessor",
"structure.StructureBlockInfoMixin",
"structure.StructureManagerMixin",
"structure.StructureStartMixin",
"structure.StructureTemplateMixin",
"villager.AbstractVillagerMixin",
"villager.MerchantMenuMixin",
Expand Down