Skip to content

Commit

Permalink
Display when pylon is disabled due to world spawn distance
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahwinsley committed Dec 10, 2021
1 parent 0b8657d commit a4c8931
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions src/generated/resources/assets/pylons/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"block.pylons.infusion_pylon": "Infusion Pylon",
"chat.pylons.expelled": "You have been expelled from %s's chunk!",
"gui.pylons.effects": "Active potion effects:",
"gui.pylons.insideWorldSpawn": "Too close to world spawn.",
"gui.pylons.noOwner": "Owner not found. Pylon disabled.",
"gui.pylons.owner": "Owner: %s",
"gui.pylons.whitelist": "Add players to whitelist:",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@ public ExpulsionPylonScreen(ExpulsionPylonContainer container, Inventory inv, Co
@Override
protected void renderLabels(PoseStack matrixStack, int mouseX, int mouseY) {
super.renderLabels(matrixStack, mouseX, mouseY);
if (this.menu.isAllowedDimension()) {
drawText(matrixStack, translate("whitelist"), 36);
} else {

if (!this.menu.isAllowedDimension()) {
drawText(matrixStack, translate("wrongDimension").withStyle(ChatFormatting.RED), 36);
} else if (!this.menu.isAllowedLocation()) {
drawText(matrixStack, translate("insideWorldSpawn").withStyle(ChatFormatting.RED), 36);
} else {
drawText(matrixStack, translate("whitelist"), 36);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected void addTranslations() {
add(gui("owner"), "Owner: %s");
add(gui("noOwner"), "Owner not found. Pylon disabled.");
add(gui("wrongDimension"), "This dimension is disabled.");
add(gui("insideWorldSpawn"), "Too close to world spawn.");
add(gui("whitelist"), "Add players to whitelist:");
add(gui("effects"), "Active potion effects:");
add(tab(), "Pylons");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
public class ExpulsionPylonContainer extends AbstractPylonContainer {

private final boolean allowedDimension;
private final boolean allowedLocation;

public ExpulsionPylonContainer(int windowId, Inventory playerInventory, FriendlyByteBuf packetBuffer) {
super(ModRegistry.EXPULSION_PYLON_CONTAINER.get(), windowId, playerInventory, packetBuffer);
allowedDimension = packetBuffer.readBoolean();
allowedLocation = packetBuffer.readBoolean();
}

@Override
Expand All @@ -23,4 +25,8 @@ protected RegistryObject<Block> getBlock() {
public boolean isAllowedDimension() {
return allowedDimension;
}

public boolean isAllowedLocation() {
return allowedLocation;
}
}
31 changes: 17 additions & 14 deletions src/main/java/net/permutated/pylons/tile/ExpulsionPylonTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.minecraft.util.Mth;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.GameRules;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -50,7 +51,7 @@ protected boolean isItemValid(ItemStack stack) {

@Override
public void tick() {
if (level != null && !level.isClientSide && canTick(10) && owner != null && isAllowedDimension()) {
if (level != null && !level.isClientSide && canTick(10) && owner != null && isAllowedDimension() && isAllowedLocation()) {
LevelChunk chunk = level.getChunkAt(worldPosition);

var chunkPos = chunk.getPos();
Expand Down Expand Up @@ -83,6 +84,7 @@ public void tick() {
public void updateContainer(FriendlyByteBuf packetBuffer) {
super.updateContainer(packetBuffer);
packetBuffer.writeBoolean(isAllowedDimension());
packetBuffer.writeBoolean(isAllowedLocation());
}

public boolean isAllowedDimension() {
Expand All @@ -100,6 +102,20 @@ public boolean isAllowedDimension() {
return false;
}

public boolean isAllowedLocation() {
if (level instanceof ServerLevel serverLevel) {
int spawnRadius = serverLevel.getGameRules().getInt(GameRules.RULE_SPAWN_RADIUS);
int configRadius = ConfigManager.COMMON.expulsionWorldSpawnRadius.get();

var bb = new BoundingBox(serverLevel.getSharedSpawnPos());
var area = bb.inflatedBy(Math.max(configRadius, spawnRadius));

return !area.intersects(getBlockPos().getX(), getBlockPos().getZ(), getBlockPos().getX(), getBlockPos().getZ());
} else {
return false;
}
}

/**
* Iterates over Player Filters in the inventory and returns a list with all found UUIDs
*
Expand Down Expand Up @@ -156,11 +172,6 @@ private void doRespawn(MinecraftServer server, ServerPlayer player) {
}
}

// is the pylon within the world spawn radius?
if (inWorldSpawn(server, actualLevel)) {
return;
}

while (!actualLevel.noCollision(dummyPlayer) && dummyPlayer.getY() < 256.0D) {
dummyPlayer.setPos(dummyPlayer.getX(), dummyPlayer.getY() + 1.0D, dummyPlayer.getZ());
}
Expand All @@ -169,15 +180,7 @@ private void doRespawn(MinecraftServer server, ServerPlayer player) {
player.sendMessage(new TranslatableComponent(TranslationKey.chat("expelled"), getOwnerName()).withStyle(ChatFormatting.RED), player.getUUID());
}

private boolean inWorldSpawn(MinecraftServer server, ServerLevel actualLevel) {
int spawnRadius = server.getSpawnRadius(actualLevel);
int configRadius = ConfigManager.COMMON.expulsionWorldSpawnRadius.get();

var bb = new BoundingBox(actualLevel.getSharedSpawnPos());
var area = bb.inflatedBy(Math.max(configRadius, spawnRadius));

return area.intersects(getBlockPos().getX(), getBlockPos().getZ(), getBlockPos().getX(), getBlockPos().getZ());
}

private boolean sameChunk(Level world, BlockPos target) {
if (level != null && level.dimension() == world.dimension()) {
Expand Down

0 comments on commit a4c8931

Please sign in to comment.