Skip to content

Commit

Permalink
Fixed "Obstructed" bug in paired checkpoints outside render distance,…
Browse files Browse the repository at this point in the history
… added support for French and Chinese
  • Loading branch information
hanyuone authored and Hanyuan Li committed Oct 31, 2020
1 parent 501df20 commit 3357c8b
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 15 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/hanyuone/checkpoint/block/CheckpointBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,23 @@ public ITextComponent getDisplayName() {

@Override
public Container createMenu(int i, PlayerInventory playerInventory, PlayerEntity playerEntity) {
return new CheckpointContainer(i, worldIn, pos, playerInventory);
BlockPos suitablePos = ((CheckpointTileEntity) tileEntity).suitablePos();
return new CheckpointContainer(i, worldIn, pos, playerInventory, suitablePos);
}
};

NetworkHooks.openGui((ServerPlayerEntity) player, containerProvider, tileEntity.getPos());
NetworkHooks.openGui((ServerPlayerEntity) player, containerProvider, (buffer) -> {
buffer.writeBlockPos(tileEntity.getPos());

BlockPos suitablePos = ((CheckpointTileEntity) tileEntity).suitablePos();

if (suitablePos != null) {
buffer.writeBoolean(true);
buffer.writeBlockPos(suitablePos);
} else {
buffer.writeBoolean(false);
}
});
} else {
throw new IllegalStateException("Our named container provider is missing!");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hanyuone.checkpoint.client.gui;

import com.hanyuone.checkpoint.Checkpoint;
import com.hanyuone.checkpoint.capability.checkpoint.CheckpointPairProvider;
import com.hanyuone.checkpoint.container.CheckpointContainer;
import com.hanyuone.checkpoint.network.CheckpointPacketHandler;
Expand Down Expand Up @@ -40,7 +41,8 @@ protected void init() {
CheckpointTileEntity checkpointEntity = (CheckpointTileEntity) containerEntity;

this.cost = checkpointEntity.calculateCost();
this.suitablePos = checkpointEntity.suitablePos();
this.suitablePos = this.container.getSuitablePos();
Checkpoint.LOGGER.debug(this.suitablePos);
}

// Display button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;

import javax.annotation.Nullable;


public class CheckpointContainer extends Container {
private final TileEntity tileEntity;
private final IItemHandler inventory;

private final BlockPos suitablePos;

// GUI constants
private static final int SLOT_SIZE = 18;
private static final int INVENTORY_LEFT = 10;
Expand All @@ -32,7 +36,7 @@ public class CheckpointContainer extends Container {
private static final int INVENTORY_ROWS = 3;
private static final int INVENTORY_COLS = 9;

public CheckpointContainer(int id, World world, BlockPos pos, PlayerInventory inventory) {
public CheckpointContainer(int id, World world, BlockPos pos, PlayerInventory inventory, BlockPos suitablePos) {
super(ContainerRegister.CHECKPOINT.get(), id);

this.tileEntity = world.getTileEntity(pos);
Expand All @@ -44,6 +48,8 @@ public CheckpointContainer(int id, World world, BlockPos pos, PlayerInventory in
);
}

this.suitablePos = suitablePos;

addInventorySlots();
}

Expand Down Expand Up @@ -142,4 +148,8 @@ public ItemStack transferStackInSlot(PlayerEntity playerIn, int index) {
public TileEntity getTileEntity() {
return this.tileEntity;
}

public BlockPos getSuitablePos() {
return this.suitablePos;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ public class ContainerRegister {
BlockPos pos = data.readBlockPos();
World world = inventory.player.getEntityWorld();

return new CheckpointContainer(id, world, pos, inventory);
if (data.readBoolean()) {
BlockPos suitablePos = data.readBlockPos();
return new CheckpointContainer(id, world, pos, inventory, suitablePos);
} else {
return new CheckpointContainer(id, world, pos, inventory, null);
}
}));
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.hanyuone.checkpoint.tileentity;

import com.hanyuone.checkpoint.Checkpoint;
import com.hanyuone.checkpoint.capability.checkpoint.CheckpointPairHandler;
import com.hanyuone.checkpoint.capability.checkpoint.CheckpointPairProvider;
import com.hanyuone.checkpoint.capability.checkpoint.ICheckpointPair;
Expand All @@ -14,11 +15,17 @@
import net.minecraft.item.Items;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.play.server.SChunkDataPacket;
import net.minecraft.network.play.server.SUpdateTileEntityPacket;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.AbstractChunkProvider;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.dimension.DimensionType;
import net.minecraft.world.server.ServerWorld;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.LazyOptional;
import net.minecraftforge.items.CapabilityItemHandler;
Expand Down Expand Up @@ -172,22 +179,36 @@ public void disablePair(World worldIn, BlockPos pos) {
}
}

// Finds a suitable block to spawn on

// Assumes there is a suitable pair
// Finds a suitable block to spawn on (should be server-side)
@Nullable
public BlockPos suitablePos() {
BlockPos currentPos = this.pairHandler.getBlockPos();
BlockPos[] neighbours = {currentPos.north(), currentPos.east(), currentPos.south(), currentPos.west()};
if (!this.pairHandler.hasPair()) {
return null;
}

BlockPos pairPos = this.pairHandler.getBlockPos();
BlockPos[] neighbours = {pairPos.north(), pairPos.east(), pairPos.south(), pairPos.west()};

AbstractChunkProvider chunkProvider = this.world.getChunkProvider();
ServerWorld serverWorld = this.world.getServer().getWorld(DimensionType.OVERWORLD);

for (BlockPos neighbour: neighbours) {
BlockState currentState = this.world.getBlockState(neighbour);
Block currentBlock = currentState.getBlock();
ChunkPos chunkPos = new ChunkPos(neighbour);
boolean needToLoad = !chunkProvider.isChunkLoaded(chunkPos);

if (needToLoad) {
serverWorld.forceChunk(chunkPos.x, chunkPos.z, true);
}

BlockState currentState = this.world.getBlockState(neighbour);
BlockState belowState = this.world.getBlockState(neighbour.down());
Block belowBlock = belowState.getBlock();
BlockState aboveState = this.world.getBlockState(neighbour.up());

if (needToLoad) {
serverWorld.forceChunk(chunkPos.x, chunkPos.z, false);
}

if (currentBlock.isAir(currentState, this.world, neighbour) && belowBlock.isSolid(belowState)) {
if (aboveState.isAir() && currentState.isAir() && belowState.isSolid()) {
return neighbour;
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/main/resources/assets/checkpoint/lang/fr_FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@

"advancement.construct": "Un moitié d'une paire",
"advancement.construct.desc": "Fabriquer un point de contrôle",
"advancement.distance": "Plus vite que la métro",
"advancement.distance": "Plus vite que la lumière",
"advancement.distance.desc": "Voyager 1000m avec les points de contrôle",

"gui.checkpoint.cost": "Coûte: %d",
"gui.checkpoint.not_paired": "Non couplé !",
"gui.checkpoint.obstructed": "Obstrué !",
"gui.checkpoint.warp": "Téléporter",

"action.already_paired": "Ce point est déjà couplé !",
"action.initiate_pair": "En train de coupler ce point !",
"action.pairing_itself": "Ne pas pouvoir coupler un point avec lui-même !",
"action.pairing_mode": "Ce point est déjà en train de couplé par un autre !",
"action.pairing_success": "Couplé avec succès !",
"action.too_far": "Ne pas pouvoir coupler, vous êtes trop loin !",

"tooltip.checkpoint.pair_location": "Sera couplé avec %s",
"tooltip.checkpoint.not_paired": "Ne pas être couplé !"
}
7 changes: 7 additions & 0 deletions src/main/resources/assets/checkpoint/lang/zh_CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"gui.checkpoint.obstructed": "没有空位!",
"gui.checkpoint.warp": "穿梭",

"action.already_paired": "此穿梭点已经成对过!",
"action.initiate_pair": "正在成对!",
"action.pairing_itself": "穿梭点无法与自己成对!",
"action.pairing_mode": "此穿梭点正在被其他人成对中!",
"action.pairing_success": "成对成功!",
"action.too_far": "您太远了,无法成对!",

"tooltip.checkpoint.pair_location": "会与 %s 成对",
"tooltip.checkpoint.not_paired": "尚未成对!"
}

0 comments on commit 3357c8b

Please sign in to comment.