-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality for the block detector
1 parent
26186e3
commit 6836624
Showing
9 changed files
with
503 additions
and
337 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/client/java/com/luna/jetoverlay/client/BlockDetectorScreen.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.luna.jetoverlay.client; | ||
|
||
import com.luna.jetoverlay.ModNetworking; | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; | ||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.GuiGraphics; | ||
import net.minecraft.client.gui.components.AbstractSliderButton; | ||
import net.minecraft.client.gui.screens.Screen; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.resources.ResourceLocation; | ||
|
||
public class BlockDetectorScreen extends Screen { | ||
|
||
public static final ResourceLocation _background = new ResourceLocation("jetoverlay", "textures/block_detector_ui.png"); | ||
|
||
int _range; | ||
int _maxRange = 10; | ||
int _width; | ||
int _maxWidth = 4; | ||
BlockPos _blockDetectorPos; | ||
|
||
BlockDetectorSlider _rangeSlider = new BlockDetectorSlider(0, 0, 110, 20, | ||
_maxRange, 1, 0, "Collider range: ", this); | ||
BlockDetectorSlider _widthSlider = new BlockDetectorSlider(0, 0, 110, 20, | ||
_maxWidth, 0, 1, "Collider width: ", this); | ||
|
||
public BlockDetectorScreen(BlockPos __screenBlock, int __range, int __width) { | ||
super(Component.literal("Block Detector")); | ||
_blockDetectorPos = __screenBlock; | ||
_range = __range; | ||
_width = __width; | ||
} | ||
|
||
@Override | ||
protected void init() { | ||
int leftPos = (this.width - 256) / 2; | ||
int topPos = (this.height - 50) / 2; | ||
|
||
_rangeSlider.setPosition(leftPos + 10, topPos + 20); | ||
_widthSlider.setPosition(leftPos + 130, topPos + 20); | ||
|
||
_rangeSlider.update(_range); | ||
_widthSlider.update(_width); | ||
addRenderableWidget(_rangeSlider); | ||
addRenderableWidget(_widthSlider); | ||
} | ||
|
||
@Override | ||
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) { | ||
int leftPos = (this.width - 256) / 2; | ||
int topPos = (this.height - 50) / 2; | ||
|
||
guiGraphics.blit(_background, leftPos, topPos, 0, 0, 0, 256, 50, 256, 50); | ||
|
||
guiGraphics.drawString(Minecraft.getInstance().font, getTitle(), leftPos + 10, topPos + 6, 0x202020, false); | ||
|
||
super.render(guiGraphics, mouseX, mouseY, partialTick); | ||
} | ||
|
||
public void setRange(int __value, int __id) { | ||
if (__id == 0) { | ||
if (_range == __value) | ||
return; | ||
_range = __value; | ||
|
||
FriendlyByteBuf buffer = PacketByteBufs.create(); | ||
buffer.writeBlockPos(_blockDetectorPos); | ||
buffer.writeInt(_range); | ||
|
||
ClientPlayNetworking.send(ModNetworking.BLOCK_DETECTOR_SET_RANGE_PACKET_ID, buffer); | ||
} | ||
else { | ||
if (_width == __value) | ||
return; | ||
_width = __value; | ||
|
||
FriendlyByteBuf buffer = PacketByteBufs.create(); | ||
buffer.writeBlockPos(_blockDetectorPos); | ||
buffer.writeInt(_width); | ||
|
||
ClientPlayNetworking.send(ModNetworking.BLOCK_DETECTOR_SET_THICKNESS_PACKET_ID, buffer); | ||
} | ||
} | ||
|
||
private static class BlockDetectorSlider extends AbstractSliderButton { | ||
BlockDetectorScreen _parent; | ||
int _valueRange; | ||
int _maxValue; | ||
int _minValue; | ||
int _id; | ||
String _text; | ||
|
||
private int getIntValue() { | ||
return (int) Math.round(value * _valueRange); | ||
} | ||
|
||
public BlockDetectorSlider(int x, int y, int width, int height, | ||
int __maxValue, int __minValue, int __id, String __text, BlockDetectorScreen parent) { | ||
super(x, y, width, height, Component.literal(""), 0); | ||
_parent = parent; | ||
_valueRange = __maxValue - _minValue; | ||
_maxValue = __maxValue; | ||
_minValue = __minValue; | ||
_text = __text; | ||
_id = __id; | ||
} | ||
|
||
public void update(int __value) { | ||
_valueRange = _maxValue - _minValue; | ||
value = (double) (__value - _minValue) / _valueRange; | ||
updateMessage(); | ||
} | ||
|
||
@Override | ||
protected void updateMessage() { | ||
setMessage(Component.literal(_text + (getIntValue() + _minValue))); | ||
} | ||
|
||
@Override | ||
protected void applyValue() { | ||
value = (float) getIntValue() / (float) _valueRange; | ||
_parent.setRange((int) (value * _valueRange) + _minValue, _id); | ||
} | ||
} | ||
} |
22 changes: 0 additions & 22 deletions
22
src/client/java/com/luna/jetoverlay/client/CollisionDetectorScreen.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
168 changes: 71 additions & 97 deletions
168
src/main/java/com/luna/jetoverlay/blocks/CollisionDetectorEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,84 @@ | ||
package com.luna.jetoverlay.blocks; | ||
|
||
import com.luna.jetoverlay.ModItems; | ||
import it.unimi.dsi.fastutil.doubles.DoubleList; | ||
import com.mojang.datafixers.util.Pair; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.world.entity.projectile.Projectile; | ||
import net.minecraft.core.Vec3i; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.entity.BlockEntity; | ||
import net.minecraft.world.level.block.entity.BlockEntityType; | ||
import net.minecraft.world.level.block.state.BlockState; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
import net.minecraft.world.phys.shapes.DiscreteVoxelShape; | ||
import net.minecraft.world.phys.shapes.VoxelShape; | ||
import net.minecraft.world.level.block.state.properties.BlockStateProperties; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
|
||
public class CollisionDetectorEntity extends BlockEntity { | ||
public static byte _redstonePower = 0; | ||
public CollisionDetectorEntity (BlockPos pos, BlockState blockState) { | ||
super(ModItems.COLLISION_DETECTOR_ENTITY, pos, blockState); | ||
} | ||
static long _lastRotationUpdateTick; | ||
public static <T extends BlockEntity> void tick (Level __level, BlockPos __blockPos, BlockState __blockState, T __entity) { | ||
long currentTick = __level.getGameTime(); | ||
System.out.println("yippe"); | ||
if (currentTick != _lastRotationUpdateTick) { | ||
_lastRotationUpdateTick = currentTick; | ||
boolean blockDetected = false; | ||
Direction direction = Direction.SOUTH; // Replace with the desired direction | ||
|
||
AABB bb = new AABB(new BlockPos(__blockPos.getX(), __blockPos.getY(), __blockPos.getZ() + 1), | ||
new BlockPos(0,0,0)).inflate(0,0,5); | ||
|
||
BlockPos minPos = new BlockPos((int)bb.minX, (int)bb.minY, (int)bb.minZ); | ||
BlockPos maxPos = new BlockPos((int)bb.maxX, (int)bb.maxY, (int)bb.maxZ); | ||
|
||
// Iterate over all positions within the bounding box | ||
for (BlockPos pos : BlockPos.betweenClosed(minPos, maxPos)) { | ||
BlockState blockState = __level.getBlockState(pos); | ||
if (!blockState.isAir()) { // Check if there's a block (not air) | ||
blockDetected = true; | ||
} | ||
else { | ||
blockDetected = false; | ||
} | ||
} | ||
|
||
if(blockDetected) { | ||
_redstonePower = 15; | ||
System.out.println("Block found"); | ||
} | ||
else { | ||
_redstonePower = 0; | ||
System.out.println("Block not found"); | ||
} | ||
Objects.requireNonNull(__level).blockUpdated(__blockPos, __blockState.getBlock()); | ||
|
||
|
||
} | ||
} | ||
private static AABB createDirectionalBoundingBox(BlockPos pos, Direction direction) { | ||
double expansion = 2.0; // The length of the hitbox | ||
|
||
switch (direction) { | ||
case NORTH: | ||
return new AABB( | ||
pos.getX() - 2, pos.getY() - 2, pos.getZ() - 2 + expansion, | ||
pos.getX() + 2, pos.getY() + 2, pos.getZ() + 2 | ||
); | ||
case SOUTH: | ||
return new AABB( | ||
pos.getX() - 2, pos.getY() - 2, pos.getZ() - 2, | ||
pos.getX() + 2, pos.getY() + 2, pos.getZ() - 2 - expansion | ||
); | ||
case WEST: | ||
return new AABB( | ||
pos.getX() - 2 - expansion, pos.getY() - 2, pos.getZ() - 2, | ||
pos.getX() + 2, pos.getY() + 2, pos.getZ() + 2 | ||
); | ||
case EAST: | ||
return new AABB( | ||
pos.getX() - 2, pos.getY() - 2, pos.getZ() - 2, | ||
pos.getX() + 2 + expansion, pos.getY() + 2, pos.getZ() + 2 | ||
); | ||
default: | ||
return new AABB( | ||
pos.getX() - 2, pos.getY() - 2, pos.getZ() - 2, | ||
pos.getX() + 2, pos.getY() + 2, pos.getZ() + 2 | ||
); | ||
} | ||
} | ||
|
||
private static boolean detectBlocksInBoundingBox(Level level, AABB bb) { | ||
// Convert AABB to BlockPos range | ||
BlockPos minPos = new BlockPos((int)bb.minX, (int)bb.minY, (int)bb.minZ); | ||
BlockPos maxPos = new BlockPos((int)bb.maxX, (int)bb.maxY, (int)bb.maxZ); | ||
|
||
// Iterate over all positions within the bounding box | ||
for (BlockPos pos : BlockPos.betweenClosed(minPos, maxPos)) { | ||
BlockState blockState = level.getBlockState(pos); | ||
if (!blockState.isAir()) { // Check if there's a block (not air) | ||
return true; // Block detected | ||
} | ||
} | ||
return false; // No blocks detected | ||
} | ||
public int _redstonePower = 0; | ||
public int width = 0; | ||
public int range = 3; | ||
|
||
public CollisionDetectorEntity(BlockPos pos, BlockState blockState) { | ||
super(ModItems.COLLISION_DETECTOR_ENTITY, pos, blockState); | ||
} | ||
|
||
static Pair<BlockPos, BlockPos> getDetectionMinMax(BlockPos __at, Direction __dir, int __length, int __thickness) { | ||
Vec3i normal = __dir.getNormal(); | ||
Vec3i absNormal = new Vec3i(Math.abs(normal.getX()), Math.abs(normal.getY()), Math.abs(normal.getZ())); | ||
Vec3i facingAway = new Vec3i(1, 1, 1).subtract(absNormal).multiply(__thickness); | ||
|
||
Vec3i min = __at.subtract(facingAway).offset(normal); | ||
Vec3i max = __at.offset(facingAway).relative(__dir, __length); | ||
|
||
return new Pair<>(new BlockPos(min.getX(), min.getY(), min.getZ()), | ||
new BlockPos(max.getX(), max.getY(), max.getZ())); | ||
} | ||
|
||
public static <T extends BlockEntity> void tick(Level __level, BlockPos __blockPos, BlockState __blockState, T __entity) { | ||
// Only update every 4 game ticks | ||
if (__level.getGameTime() % 4 == 0) { | ||
return; | ||
} | ||
|
||
if (!(__entity instanceof CollisionDetectorEntity collisionBlock)) { | ||
return; | ||
} | ||
|
||
boolean blockDetected = false; | ||
Direction direction = __blockState.getValue(BlockStateProperties.HORIZONTAL_FACING); | ||
|
||
Pair<BlockPos, BlockPos> minMax = getDetectionMinMax(__blockPos, direction, collisionBlock.range, collisionBlock.width); | ||
|
||
// Iterate over all positions within the bounding box | ||
for (BlockPos pos : BlockPos.betweenClosed(minMax.getFirst(), minMax.getSecond())) { | ||
BlockState blockState = __level.getBlockState(pos); | ||
if (!blockState.isAir()) { // Check if there's a block (not air) | ||
blockDetected = true; | ||
} | ||
} | ||
|
||
int newRedstonePower = blockDetected ? 15 : 0; | ||
if (newRedstonePower != collisionBlock._redstonePower) { | ||
collisionBlock._redstonePower = newRedstonePower; | ||
Objects.requireNonNull(__level).blockUpdated(__blockPos, __blockState.getBlock()); | ||
} | ||
} | ||
|
||
@Override | ||
public void load(CompoundTag __nbt) { | ||
super.load(__nbt); | ||
if (__nbt.contains("range")) | ||
range = __nbt.getInt("range"); | ||
if (__nbt.contains("width")) | ||
width = __nbt.getInt("width"); | ||
} | ||
|
||
@Override | ||
public void saveAdditional(CompoundTag __nbt) { | ||
super.saveAdditional(__nbt); | ||
// Save the current value of the number to the nbt | ||
__nbt.putInt("range", range); | ||
__nbt.putInt("width", width); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.