Skip to content

Commit

Permalink
Add range support to expulsion pylon
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremiahwinsley committed Dec 27, 2021
1 parent d211c64 commit 50b2885
Show file tree
Hide file tree
Showing 13 changed files with 258 additions and 68 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 @@ -7,6 +7,7 @@
"gui.pylons.noOwner": "Owner not found. Pylon disabled.",
"gui.pylons.owner": "Owner: %s",
"gui.pylons.whitelist": "Add players to whitelist:",
"gui.pylons.workArea": "Work area (in chunks)",
"gui.pylons.wrongDimension": "This dimension is disabled.",
"item.pylons.player_filter": "Player Filter",
"item.pylons.potion_filter": "Potion Filter",
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/permutated/pylons/Pylons.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.permutated.pylons.block.AbstractPylonBlock;
import net.permutated.pylons.client.ClientSetup;
import net.permutated.pylons.item.PlayerFilterCard;
import net.permutated.pylons.network.NetworkDispatcher;
import net.permutated.pylons.util.ChunkManager;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -30,6 +31,7 @@ public Pylons() {
LOGGER.info("Registering mod: {}", MODID);

ModRegistry.register();
NetworkDispatcher.register();

ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, ConfigManager.COMMON_SPEC);
FMLJavaModLoadingContext.get().getModEventBus().addListener(this::onCommonSetupEvent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package net.permutated.pylons.client.gui;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Component;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import net.permutated.pylons.inventory.container.AbstractPylonContainer;
import net.permutated.pylons.util.Constants;
import net.permutated.pylons.util.ResourceUtil;
import net.permutated.pylons.util.TranslationKey;

public abstract class AbstractPylonScreen<T extends AbstractPylonContainer> extends AbstractContainerScreen<T> {
protected final ResourceLocation gui;
protected Button rangeButton;

protected AbstractPylonScreen(T container, Inventory inv, Component name, String pylonType) {
super(container, inv, name);
Expand All @@ -23,6 +25,32 @@ protected AbstractPylonScreen(T container, Inventory inv, Component name, String
this.imageHeight = 166;
}

@Override
protected void init() {
super.init();
// x, y, width, height
rangeButton = new Button(this.leftPos + 140, this.height / 2 - 77, 30, 20,
this.menu.getRangeComponent(), this.menu::sendRangePacket, this::buttonTooltip);
if (this.menu.shouldRenderRange()) {
addRenderableWidget(rangeButton);
updateMessages();
}
}

private void buttonTooltip(Button button, PoseStack poseStack, int p_169460_, int p_169461_) {
this.renderTooltip(poseStack, translate("workArea"), p_169460_, p_169461_);
}

public void updateMessages() {
this.rangeButton.setMessage(this.menu.getRangeComponent());
}

@Override
protected void containerTick() {
updateMessages();
super.containerTick();
}

@Override
public void render(PoseStack matrixStack, int mouseX, int mouseY, float partialTicks) {
this.renderBackground(matrixStack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ protected void addTranslations() {
add(gui("insideWorldSpawn"), "Too close to world spawn.");
add(gui("whitelist"), "Add players to whitelist:");
add(gui("effects"), "Active potion effects:");
add(gui("workArea"), "Work area (in chunks)");
add(tab(), "Pylons");

add(chat("expelled"), "You have been expelled from %s's chunk!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package net.permutated.pylons.inventory.container;

import net.minecraft.world.level.block.Block;
import net.minecraft.world.entity.player.Player;
import net.minecraft.client.gui.components.Button;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraftforge.registries.RegistryObject;
import net.minecraft.world.level.block.Block;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.SlotItemHandler;
import net.minecraftforge.items.wrapper.InvWrapper;
import net.minecraftforge.registries.RegistryObject;
import net.permutated.pylons.network.NetworkDispatcher;
import net.permutated.pylons.network.PacketButtonClicked;
import net.permutated.pylons.tile.AbstractPylonTile;

import javax.annotation.Nullable;
Expand All @@ -25,14 +27,15 @@ public abstract class AbstractPylonContainer extends AbstractContainerMenu {
@Nullable // should only be accessed from server
private final AbstractPylonTile tileEntity;
protected final String ownerName;
protected final BlockPos blockPos;

protected AbstractPylonContainer(@Nullable MenuType<?> containerType, int windowId, Inventory playerInventory, FriendlyByteBuf packetBuffer) {
super(containerType, windowId);

BlockPos pos = packetBuffer.readBlockPos();
blockPos = packetBuffer.readBlockPos();
Level world = playerInventory.player.getCommandSenderWorld();

tileEntity = (AbstractPylonTile) world.getBlockEntity(pos);
tileEntity = (AbstractPylonTile) world.getBlockEntity(blockPos);
IItemHandler wrappedInventory = new InvWrapper(playerInventory);

if (tileEntity != null) {
Expand All @@ -59,6 +62,19 @@ public String getOwnerName() {
return ownerName;
}

public boolean shouldRenderRange() {
return false;
}

public Component getRangeComponent() {
var range = tileEntity != null ? tileEntity.getSelectedRange() : 0;
return new TextComponent(String.format("%dx%d", range, range));
}

public void sendRangePacket(Button button) {
NetworkDispatcher.INSTANCE.sendToServer(new PacketButtonClicked(blockPos));
}

@Override
public boolean stillValid(Player playerEntity) {
if (tileEntity != null) {
Expand Down Expand Up @@ -100,16 +116,13 @@ public ItemStack quickMoveStack(Player playerIn, int index) {
}

public void registerPlayerSlots(IItemHandler wrappedInventory) {
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 9; j++)
{
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 9; j++) {
addSlot(new SlotItemHandler(wrappedInventory, j + i * 9 + 9, 8 + j * 18, 84 + i * 18));
}
}

for (int i = 0; i < 9; i++)
{
for (int i = 0; i < 9; i++) {
addSlot(new SlotItemHandler(wrappedInventory, i, 8 + i * 18, 142));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ public boolean isAllowedDimension() {
public boolean isAllowedLocation() {
return allowedLocation;
}

@Override
public boolean shouldRenderRange() {
return true;
}
}
25 changes: 25 additions & 0 deletions src/main/java/net/permutated/pylons/network/NetworkDispatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package net.permutated.pylons.network;

import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.simple.SimpleChannel;
import net.permutated.pylons.Pylons;


public class NetworkDispatcher {
private NetworkDispatcher() {
// nothing to do
}

private static final String PROTOCOL_VERSION = "1";

public static final SimpleChannel INSTANCE = NetworkRegistry.newSimpleChannel(new ResourceLocation(Pylons.MODID, "main"),
() -> PROTOCOL_VERSION, PROTOCOL_VERSION::equals, PROTOCOL_VERSION::equals);

public static void register() {
int packetIndex = 0;
INSTANCE.registerMessage(packetIndex++, PacketButtonClicked.class, PacketButtonClicked::toBytes, PacketButtonClicked::new, PacketButtonClicked::handle);

Pylons.LOGGER.info("Registered {} network packets", packetIndex);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package net.permutated.pylons.network;

import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkEvent;
import net.permutated.pylons.tile.AbstractPylonTile;

import java.util.Optional;
import java.util.function.Supplier;

public class PacketButtonClicked {
private final BlockPos blockPos;

public PacketButtonClicked(BlockPos blockPos) {
this.blockPos = blockPos;
}

public PacketButtonClicked(FriendlyByteBuf buffer) {
blockPos = buffer.readBlockPos();
}

public void toBytes(FriendlyByteBuf buffer) {
buffer.writeBlockPos(this.blockPos);
}

@SuppressWarnings("java:S1172")
public static void handle(PacketButtonClicked event, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
Optional<Player> player = Optional.ofNullable(ctx.get().getSender());
Optional<Level> world = player.map(Player::getCommandSenderWorld);

if (player.isPresent() && world.get() instanceof ServerLevel serverLevel && serverLevel.isLoaded(event.blockPos)) {
var be = serverLevel.getBlockEntity(event.blockPos);
if (be instanceof AbstractPylonTile pylonTile) {
pylonTile.handleRangePacket();
}
}
});
ctx.get().setPacketHandled(true);
}
}
7 changes: 7 additions & 0 deletions src/main/java/net/permutated/pylons/network/package-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
@ParametersAreNonnullByDefault
@MethodsReturnNonnullByDefault
package net.permutated.pylons.network;

import net.minecraft.MethodsReturnNonnullByDefault;

import javax.annotation.ParametersAreNonnullByDefault;
27 changes: 26 additions & 1 deletion src/main/java/net/permutated/pylons/tile/AbstractPylonTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.minecraftforge.items.ItemStackHandler;
import net.permutated.pylons.util.ChunkManager;
import net.permutated.pylons.util.Constants;
import net.permutated.pylons.util.Range;
import org.apache.commons.lang3.StringUtils;

import javax.annotation.Nonnull;
Expand Down Expand Up @@ -128,8 +129,9 @@ public String getOwnerName() {

/**
* Serialize data to be sent to the GUI on the client.
*
* <p>
* Overrides MUST call the super method first to ensure correct deserialization.
*
* @param packetBuffer the packet ready to be filled
*/
public void updateContainer(FriendlyByteBuf packetBuffer) {
Expand All @@ -144,6 +146,7 @@ public void updateContainer(FriendlyByteBuf packetBuffer) {
@Override
protected void saveAdditional(CompoundTag tag) {
tag.put(Constants.NBT.INV, itemStackHandler.serializeNBT());
tag.put(Constants.NBT.RANGE, range.serializeNBT());
writeOwner(tag);
}

Expand All @@ -158,6 +161,7 @@ private void writeOwner(CompoundTag tag) {
@Override
public void load(CompoundTag tag) {
itemStackHandler.deserializeNBT(tag.getCompound(Constants.NBT.INV));
range.deserializeNBT(tag.getCompound(Constants.NBT.RANGE));
readOwner(tag);
super.load(tag);
}
Expand All @@ -173,12 +177,16 @@ private void readOwner(@Nullable CompoundTag tag) {
@Override
public CompoundTag getUpdateTag() {
CompoundTag tag = super.getUpdateTag();
tag.put(Constants.NBT.RANGE, range.serializeNBT());
writeOwner(tag);
return tag;
}

@Override
public void handleUpdateTag(@Nullable CompoundTag tag) {
if (tag != null) {
range.deserializeNBT(tag.getCompound(Constants.NBT.RANGE));
}
readOwner(tag);
}

Expand All @@ -205,4 +213,21 @@ protected void onContentsChanged(int slot) {
setChanged();
}
}

protected final Range range = new Range(getRange());

protected byte[] getRange() {
return new byte[]{1};
}

public int getSelectedRange() {
return range.get();
}

public void handleRangePacket() {
if (getRange().length > 1 && this.level != null) {
this.range.next();
this.level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), 2);
}
}
}
Loading

0 comments on commit 50b2885

Please sign in to comment.