Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import namidevelopment.kiriyaga.api.annotation.RegisterFeature;
import namidevelopment.kiriyaga.api.annotation.SubscribeEvent;
import namidevelopment.kiriyaga.api.event.impl.PacketReceiveEvent;
import namidevelopment.kiriyaga.api.event.impl.PreTickEvent;
import namidevelopment.kiriyaga.api.event.impl.Render3DEvent;
import namidevelopment.kiriyaga.api.model.feature.Feature;
Expand Down Expand Up @@ -63,6 +64,11 @@ public void onRender(Render3DEvent event) {
trap.onRender(event);
}

@SubscribeEvent
public void onPacketReceive(PacketReceiveEvent event) {
trap.handlePacket(event, this, getTrapTargets());
}

private List<BlockPos> getTrapTargets() {
Set<BlockPos> targets = new HashSet<>(getSurround(MC.player, 0, extension.get()));
if (corners.get()) {
Expand All @@ -88,4 +94,4 @@ private List<BlockPos> getTrapTargets() {
}
return new ArrayList<>(targets);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
import namidevelopment.kiriyaga.api.util.render.RenderUtil;
import namidevelopment.kiriyaga.nami.impl.feature.client.ColorFeature;
import namidevelopment.kiriyaga.nami.impl.feature.client.TrapFeature;
import namidevelopment.kiriyaga.api.event.impl.PacketReceiveEvent;
import namidevelopment.kiriyaga.api.util.InventoryUtils;
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Items;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -54,6 +60,7 @@ public class TrapComponent {

private final List<BlockPos> targetPositions = new ArrayList<>();
private final List<BlockPos> placedPositions = new ArrayList<>(); // todo: finish this
private final List<BlockPos> pendingSimulations = new ArrayList<>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, its just not the way it should be done


public TrapComponent(Feature feature) {
range = feature.addSetting(new DoubleSetting("Range", 4.50, 1.0, 6.0));
Expand Down Expand Up @@ -100,6 +107,8 @@ public void onTick(PreTickEvent event, Feature owner, List<BlockPos> newTargets)
TrapFeature trapFeature = FEATURE_SERVICE.getStorage().getByClass(TrapFeature.class);
tickTimer(trapFeature);

pendingSimulations.removeIf(pos -> !MC.level.getBlockState(pos).canBeReplaced());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this is based on client side only logic also, nahhhh


/* if (simulate.get() && !placedPositions.isEmpty()) {
Item handItem = MC.player.getMainHandItem().getItem();

Expand Down Expand Up @@ -315,4 +324,75 @@ private void tickTimer(TrapFeature trapFeature) {
}
}
}

public void handlePacket(PacketReceiveEvent event, Feature owner, List<BlockPos> currentTargets) {
if (!simulate.get()) return;
if (MC.player == null || MC.level == null) return;

if (event.getPacket() instanceof ClientboundAddEntityPacket packet &&
packet.getType() == EntityType.END_CRYSTAL) {
handleSequentialCrystals(packet, owner, currentTargets);
}
}

private void handleSequentialCrystals(ClientboundAddEntityPacket packet, Feature owner, List<BlockPos> currentTargets) {
Vec3 crystalPos = new Vec3(packet.getX(), packet.getY(), packet.getZ());
AABB crystalBox = new AABB(
crystalPos.add(-1.0, 0, -1.0),
crystalPos.add(1.0, 2.0, 1.0)
);

BlockPos targetPos = null;
for (BlockPos pos : currentTargets) {
if (new AABB(pos).intersects(crystalBox)) {
targetPos = pos;
break;
}
}

if (targetPos == null) return;
if (!multiTask.get() && MC.player.isUsingItem()) return;

int slot = findObsidianSlot();
if (slot == -1 && !(MC.player.getOffhandItem().getItem() instanceof BlockItem))
return;

boolean isOffhand = MC.player.getOffhandItem().getItem() instanceof BlockItem;
int previousSlot = MC.player.getInventory().getSelectedSlot();
boolean switched = false;

if (!isOffhand && slot != -1 && slot != previousSlot) {
InventoryUtils.attemptSwitch(slot);
switched = true;
}

EndCrystal tempCrystal = new EndCrystal(MC.level, packet.getX(), packet.getY(), packet.getZ());
tempCrystal.setId(packet.getId());
MC.gameMode.attack(MC.player, tempCrystal);

if (swing.get()) {
MC.player.swing(InteractionHand.MAIN_HAND);
}

Item item = isOffhand ? MC.player.getOffhandItem().getItem() : MC.player.getInventory().getItem(slot).getItem();

boolean placed = place(targetPos, item, airPlace.get(), grim.get(), owner);

if (placed) {
pendingSimulations.add(targetPos);
}

if (switched && swapBack.get()) {
InventoryUtils.attemptSwitch(previousSlot);
}
}

private int findObsidianSlot() {
for (int i = 0; i < 9; i++) {
if (MC.player.getInventory().getItem(i).getItem() == Items.OBSIDIAN) {
return i;
}
}
return -1;
}
}