Skip to content

Commit

Permalink
feat: added validation checker for point
Browse files Browse the repository at this point in the history
  • Loading branch information
Uhutown committed Oct 2, 2023
1 parent 7cb2c73 commit f2cc40d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,40 @@
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition.Builder;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;

public class PathwayRequester extends BasicBlock {

public static final BooleanProperty POWERD = BooleanProperty.create("powerd");
public static final TileEntitySupplierWrapper WRAPPER = PathwayRequesterTileEntity::new;

public PathwayRequester() {
super(Properties.of(Material.METAL));
}

@Override
protected void createBlockStateDefinition(Builder<Block, BlockState> builder) {
builder.add(POWERD);
}

@Override
public void neighborChanged(BlockState state, Level world, BlockPos pos, Block block,
BlockPos toPos, boolean moveing) {
if (world.isClientSide)
return;
if (world.hasNeighborSignal(pos)) {
final BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof PathwayRequesterTileEntity) {
((PathwayRequesterTileEntity) entity).requestPathway();
if (!state.getValue(POWERD)) {
world.setBlockAndUpdate(pos, state.setValue(POWERD, true));
final BlockEntity entity = world.getBlockEntity(pos);
if (entity instanceof PathwayRequesterTileEntity) {
((PathwayRequesterTileEntity) entity).requestPathway();
}
}
} else {
world.setBlockAndUpdate(pos, state.setValue(POWERD, false));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.troblecodings.signals.guis;

import java.util.Map;
import java.util.function.Consumer;

import com.troblecodings.core.I18Wrapper;
import com.troblecodings.core.ReadBuffer;
import com.troblecodings.core.WriteBuffer;
import com.troblecodings.guilib.ecs.ContainerBase;
import com.troblecodings.guilib.ecs.GuiInfo;
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.core.PosIdentifier;
import com.troblecodings.signals.handler.SignalBoxHandler;
import com.troblecodings.signals.signalbox.Point;
import com.troblecodings.signals.tileentitys.PathwayRequesterTileEntity;

Expand All @@ -18,6 +22,9 @@ public class PathwayRequesterContainer extends ContainerBase {
protected Point start = null;
protected Point end = null;
protected BlockPos linkedPos;
private boolean dataSend = false;
private Consumer<String> infoUpdates = s -> {
};

public PathwayRequesterContainer(GuiInfo info) {
super(info);
Expand All @@ -39,19 +46,38 @@ public void sendAllDataToRemote() {

@Override
public void deserializeClient(ReadBuffer buffer) {
this.linkedPos = buffer.getBlockPos();
if (this.linkedPos.equals(BlockPos.ZERO))
this.linkedPos = null;
start = Point.of(buffer);
end = Point.of(buffer);
update();
if (!dataSend) {
this.linkedPos = buffer.getBlockPos();
if (this.linkedPos.equals(BlockPos.ZERO))
this.linkedPos = null;
start = Point.of(buffer);
end = Point.of(buffer);
dataSend = true;
update();
return;
}
final int message = buffer.getByteToUnsignedInt();
infoUpdates.accept(message == 1 ? I18Wrapper.format("gui.accepted")
: I18Wrapper.format("gui.notvalid"));
}

@Override
public void deserializeServer(ReadBuffer buffer) {
final Point start = Point.of(buffer);
final Point end = Point.of(buffer);
tile.setNextPathway(start, end);
final WriteBuffer awnser = new WriteBuffer();
if (SignalBoxHandler.arePointsValidStartAndEnd(
new PosIdentifier(tile.getLinkedSignalBox(), info.world), start, end)) {
tile.setNextPathway(start, end);
awnser.putByte((byte) 1);
} else {
awnser.putByte((byte) 0);
}
OpenSignalsMain.network.sendTo(info.player, awnser);
}

public void setConsumer(final Consumer<String> consumer) {
this.infoUpdates = consumer;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.troblecodings.guilib.ecs.entitys.UIEntity;
import com.troblecodings.guilib.ecs.entitys.UITextInput;
import com.troblecodings.guilib.ecs.entitys.render.UILabel;
import com.troblecodings.guilib.ecs.entitys.render.UIToolTip;
import com.troblecodings.signals.OpenSignalsMain;
import com.troblecodings.signals.signalbox.Point;

Expand All @@ -23,6 +24,20 @@ public PathwayRequesterGui(GuiInfo info) {
super(info);
this.container = (PathwayRequesterContainer) info.base;
this.player = info.player;
this.container.setConsumer(this::update);
}

private void update(final String str) {
final UIToolTip tooltip = new UIToolTip(str, true);
entity.add(tooltip);
new Thread(() -> {
try {
Thread.sleep(4000);
} catch (final InterruptedException e) {
e.printStackTrace();
}
entity.remove(tooltip);
}).start();
}

private void initOwn() {
Expand Down Expand Up @@ -72,9 +87,9 @@ private void initOwn() {
startHbox.setHeight(25);
startHbox.setInheritWidth(true);

startHbox.add(GuiElements.createLabel("Start X:"));
startHbox.add(GuiElements.createLabel("Start x:"));
startHbox.add(startXEntity);
startHbox.add(GuiElements.createLabel("Start Y:"));
startHbox.add(GuiElements.createLabel("Start y:"));
startHbox.add(startYEntity);
startHbox.add(GuiElements.createButton(I18Wrapper.format("btn.parse"), e -> {
try {
Expand All @@ -93,9 +108,9 @@ private void initOwn() {
endHbox.setHeight(25);
endHbox.setInheritWidth(true);

endHbox.add(GuiElements.createLabel("End X:"));
endHbox.add(GuiElements.createLabel("End x:"));
endHbox.add(endXEntity);
endHbox.add(GuiElements.createLabel("End Y:"));
endHbox.add(GuiElements.createLabel("End y:"));
endHbox.add(endYEntity);
endHbox.add(GuiElements.createButton(I18Wrapper.format("btn.parse"), e -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,19 @@ public static void updateModeGrid(final PosIdentifier identifier, final SignalBo
holder.updateModeGrid(grid);
}

public static boolean arePointsValidStartAndEnd(final PosIdentifier identifier,
final Point start, final Point end) {
if (identifier.world.isClientSide)
return false;
PathwayHolder grid;
synchronized (ALL_GRIDS) {
grid = ALL_GRIDS.get(identifier);
}
if (grid == null)
return false;
return grid.isValidStart(start) && grid.isValidEnd(end);
}

public static List<Map.Entry<Point, Point>> getNextPathways(final PosIdentifier identifier) {
if (identifier.world.isClientSide)
return new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ public void updateModeGrid(final SignalBoxGrid grid) {
this.modeGrid = grid.modeGrid;
}

public boolean isValidStart(final Point point) {
final SignalBoxNode node = modeGrid.get(point);
if (node == null)
return false;
return node.isValidStart();
}

public boolean isValidEnd(final Point point) {
final SignalBoxNode node = modeGrid.get(point);
if (node == null)
return false;
return node.isValidEnd();
}

public boolean requestWay(final Point p1, final Point p2) {
if (startsToPath.containsKey(p1) || endsToPath.containsKey(p2))
return false;
Expand Down

0 comments on commit f2cc40d

Please sign in to comment.