Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
Gaz492 committed Mar 11, 2024
1 parent 76062c3 commit b4d4f83
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 131 deletions.
16 changes: 15 additions & 1 deletion common/src/main/java/dev/wuffs/ifly/blocks/TbdBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
Expand Down Expand Up @@ -34,10 +36,22 @@ public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, Block
return level.isClientSide() ? null : TbdBlockEntity::ticker;
}

@Override
public void setPlacedBy(Level level, BlockPos blockPos, BlockState blockState, @Nullable LivingEntity livingEntity, ItemStack itemStack) {
super.setPlacedBy(level, blockPos, blockState, livingEntity, itemStack);
if (level.isClientSide) {
return;
}
BlockEntity blockEntity = level.getBlockEntity(blockPos);
if (blockEntity instanceof TbdBlockEntity tbdBlockEntity) {
tbdBlockEntity.ownerUUID = livingEntity.getUUID();
tbdBlockEntity.setChanged();
}
}

@Override
public InteractionResult use(BlockState blockState, Level level, BlockPos blockPos, Player player, InteractionHand interactionHand, BlockHitResult blockHitResult) {
if(level.isClientSide){
// new TBDScreen(level.getBlockEntity(blockPos)).openGui();
Network.CHANNEL.sendToServer(new C2SOpenIflyScreen(blockPos));
return InteractionResult.CONSUME;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.util.Mth;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.effect.MobEffects;
Expand All @@ -32,6 +34,7 @@ public class TbdBlockEntity extends BlockEntity {

public static final AABB DETECT_BOX = Shapes.block().bounds();
public List<StoredPlayers> storedPlayers = new ArrayList<>();
public UUID ownerUUID;

public TbdBlockEntity(BlockPos blockPos, BlockState blockState) {
super(Blocks.TBDE.get(), blockPos, blockState);
Expand Down Expand Up @@ -131,20 +134,24 @@ protected void saveAdditional(CompoundTag compoundTag) {
super.saveAdditional(compoundTag);
Tag storePlayersCompound = StoredPlayers.LIST_CODEC.encodeStart(NbtOps.INSTANCE, storedPlayers).getOrThrow(false, RuntimeException::new);
compoundTag.put("storedPlayers", storePlayersCompound);
compoundTag.putUUID("ownerUUID", ownerUUID);
}

@Override
public void load(CompoundTag compoundTag) {
super.load(compoundTag);
storedPlayers = StoredPlayers.LIST_CODEC.parse(NbtOps.INSTANCE, compoundTag.get("storedPlayers")).getOrThrow(false, RuntimeException::new);
ownerUUID = compoundTag.getUUID("ownerUUID");
}

public record StoredPlayers(
UUID playerUUID,
Component playerName,
boolean allowed
){
public static final Codec<StoredPlayers> CODEC = RecordCodecBuilder.create(instance -> instance.group(
UUIDUtil.CODEC.fieldOf("playerUUID").forGetter(StoredPlayers::playerUUID),
ComponentSerialization.CODEC.fieldOf("playerName").forGetter(StoredPlayers::playerName),
Codec.BOOL.fieldOf("allowed").forGetter(StoredPlayers::allowed)
).apply(instance, StoredPlayers::new));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import dev.ftb.mods.ftblibrary.ui.input.MouseButton;
import dev.ftb.mods.ftblibrary.ui.misc.NordColors;
import dev.wuffs.ifly.blocks.TbdBlockEntity;
import dev.wuffs.ifly.network.C2SGUIInteract;
import dev.wuffs.ifly.network.Network;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.player.AbstractClientPlayer;
Expand All @@ -14,6 +16,7 @@
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.UUID;

public class TBDScreen extends BaseScreen implements NordColors {

Expand All @@ -26,7 +29,7 @@ public class TBDScreen extends BaseScreen implements NordColors {
public BlockPos blockPos;
public List<TbdBlockEntity.StoredPlayers> storedPlayers;

private static final int scrollWidth = 12;
private static final int scrollWidth = 10;

public TBDScreen(BlockPos blockPos, List<TbdBlockEntity.StoredPlayers> storedPlayers) {
super();
Expand All @@ -36,22 +39,6 @@ public TBDScreen(BlockPos blockPos, List<TbdBlockEntity.StoredPlayers> storedPla

@Override
public boolean onInit() {
whitelistedPlayersPanel = new BlankPanel(this) {
@Override
public void alignWidgets() {
align(WidgetLayout.VERTICAL);
}
};
whitelistedPlayersScroll = new PanelScrollBar(this, whitelistedPlayersPanel);

onlinePlayersPanel = new BlankPanel(this) {
@Override
public void alignWidgets() {
align(WidgetLayout.VERTICAL);
}
};
onlinePlayersScroll = new PanelScrollBar(this, onlinePlayersPanel);

setSizeProportional(0.70f, 0.7f);

return super.onInit();
Expand All @@ -66,40 +53,52 @@ public void alignWidgets() {

@Override
public void addWidgets() {
this.add(new SimpleButton(this, Component.empty(), Icons.ADD, (btn, mb) -> {
new PlayerListScreen(this, blockPos).openGui();
}));
var things = List.of("Fuck you mikey w");
for (int i = 0; i < 10; i++) {
for (String thing : things) {
var playerEntry = getEntry(whitelistedPlayersPanel,thing, Icons.REMOVE);
whitelistedPlayersPanel.add(playerEntry);

whitelistedPlayersPanel = new BlankPanel(this) {
@Override
public void alignWidgets() {
align(WidgetLayout.VERTICAL);
}
};
whitelistedPlayersScroll = new PanelScrollBar(this, whitelistedPlayersPanel);

onlinePlayersPanel = new BlankPanel(this) {
@Override
public void alignWidgets() {
align(WidgetLayout.VERTICAL);
}
};
onlinePlayersScroll = new PanelScrollBar(this, onlinePlayersPanel);

for (TbdBlockEntity.StoredPlayers player : storedPlayers) {
var playerEntry = getEntry(whitelistedPlayersPanel, player.playerName().getString(), Icons.REMOVE, player.playerUUID(), false);
whitelistedPlayersPanel.add(playerEntry);
}

whitelistedPlayersPanel.setPosAndSize(5, 23, width / 2, height - 24);
whitelistedPlayersScroll.setPosAndSize(width / 2 - scrollWidth, 20, scrollWidth, height - 21);
whitelistedPlayersPanel.setPosAndSize(5, 43, width / 2, height - 44);
whitelistedPlayersScroll.setPosAndSize(width / 2 - scrollWidth, 40, scrollWidth, height - 41);

this.add(whitelistedPlayersPanel);
this.add(whitelistedPlayersScroll);

var playerList = Minecraft.getInstance().level.players();
for (int i = 0; i < 10; i++) {
for (AbstractClientPlayer player : playerList) {
var playerEntry = getEntry(onlinePlayersPanel, player.getDisplayName().getString(), Icons.ADD);
playerEntry.setWidth(this.width - 5);
onlinePlayersPanel.add(playerEntry);
for (AbstractClientPlayer player : playerList) {
if (storedPlayers.stream().anyMatch(storedPlayer -> storedPlayer.playerUUID().equals(player.getUUID()))) {
continue;
}
var playerEntry = getEntry(onlinePlayersPanel, player.getDisplayName().getString(), Icons.ADD, player.getUUID(), true);
playerEntry.setWidth(this.width - 5);
onlinePlayersPanel.add(playerEntry);
}

onlinePlayersPanel.setPosAndSize(width / 2 + 5, 23, width / 2, height - 24);
onlinePlayersScroll.setPosAndSize(width - scrollWidth, 20, scrollWidth, height - 21);
onlinePlayersPanel.setPosAndSize(width / 2 + 5, 43, width / 2, height - 44);
onlinePlayersScroll.setPosAndSize(width - scrollWidth, 40, scrollWidth, height - 41);
this.add(onlinePlayersPanel);
this.add(onlinePlayersScroll);
}

@NotNull
private BlankPanel getEntry(Panel panel, String btnText, Icon icon) {
private BlankPanel getEntry(Panel panel, String btnText, Icon icon, UUID playerUUID, boolean isAdding) {
var playerEntry = new BlankPanel(panel) {
@Override
public void alignWidgets() {
Expand All @@ -111,7 +110,15 @@ public void addWidgets() {
SimpleTextButton playerBtn = new SimpleTextButton(this, Component.literal(btnText), icon) {
@Override
public void onClicked(MouseButton button) {

Network.CHANNEL.sendToServer(new C2SGUIInteract(blockPos, playerUUID, isAdding));
if (isAdding) {
storedPlayers.add(new TbdBlockEntity.StoredPlayers(playerUUID, Component.literal(btnText), isAdding));
} else {
storedPlayers.removeIf(storedPlayer -> storedPlayer.playerUUID().equals(playerUUID));
}
// TBDScreen.this.clearWidgets();
TBDScreen.this.refreshWidgets();
// TBDScreen.this.addWidgets();
}
};
playerBtn.setWidth(panel.width - scrollWidth - 7);
Expand All @@ -127,13 +134,17 @@ public void onClicked(MouseButton button) {
public void drawForeground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
super.drawForeground(graphics, theme, x, y, w, h);
theme.drawString(graphics, "Player Manager", x + w / 2, y + 7, SNOW_STORM_1, Theme.CENTERED);
theme.drawString(graphics, "Whitelisted Players", x + w / 4, y + 27, SNOW_STORM_1, Theme.CENTERED);
theme.drawString(graphics, "Online Players", x + w / 2 + Minecraft.getInstance().font.width("Online Players"), y + 27, SNOW_STORM_1, 0);
}

@Override
public void drawBackground(GuiGraphics graphics, Theme theme, int x, int y, int w, int h) {
GuiHelper.drawHollowRect(graphics, x, y, w, h, POLAR_NIGHT_0, true);
POLAR_NIGHT_1.draw(graphics, x + 1, y + 1, w - 2, h - 2);
POLAR_NIGHT_0.draw(graphics, x + 1, y + 21, w - 2, 1);
POLAR_NIGHT_0.draw(graphics, x + 1, y + 41, w - 2, 1);
POLAR_NIGHT_0.draw(graphics, x + w / 2, y + 22, 1, h - 22);
}


Expand Down
67 changes: 67 additions & 0 deletions common/src/main/java/dev/wuffs/ifly/network/C2SGUIInteract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package dev.wuffs.ifly.network;

import dev.architectury.networking.NetworkManager;
import dev.wuffs.ifly.blocks.TbdBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.block.entity.BlockEntity;

import java.util.List;
import java.util.UUID;
import java.util.function.Supplier;

public class C2SGUIInteract {

BlockPos blockPos;
UUID playerUUID;
boolean added;

public C2SGUIInteract(FriendlyByteBuf buf) {
blockPos = buf.readBlockPos();
playerUUID = buf.readUUID();
added = buf.readBoolean();
}

public C2SGUIInteract(BlockPos pos, UUID playerUUID, boolean added) {
// Message creation
this.blockPos = pos;
this.playerUUID = playerUUID;
this.added = added;

}

public void encode(FriendlyByteBuf buf) {
// Encode data into the buf
buf.writeBlockPos(blockPos);
buf.writeUUID(playerUUID);
buf.writeBoolean(added);
}

public void apply(Supplier<NetworkManager.PacketContext> contextSupplier) {
// On receive
contextSupplier.get().queue(() -> {
BlockEntity blockEntity = contextSupplier.get().getPlayer().level().getBlockEntity(blockPos);
if (blockEntity instanceof TbdBlockEntity tbdBlockEntity) {
if (!tbdBlockEntity.ownerUUID.equals(contextSupplier.get().getPlayer().getUUID())) {
return;
}
Player playerByUUID = contextSupplier.get().getPlayer().level().getPlayerByUUID(playerUUID);
if (playerByUUID == null) {
return;
}

List<TbdBlockEntity.StoredPlayers> storedPlayers = tbdBlockEntity.storedPlayers;
if (added) {
if (storedPlayers.stream().anyMatch(storedPlayer -> storedPlayer.playerUUID().equals(playerUUID))) {
return;
}
storedPlayers.add(new TbdBlockEntity.StoredPlayers(playerUUID, playerByUUID.getDisplayName(), true));
} else {
storedPlayers.removeIf(storedPlayer -> storedPlayer.playerUUID().equals(playerUUID));
}
tbdBlockEntity.setChanged();
}
});
}
}
Loading

0 comments on commit b4d4f83

Please sign in to comment.