-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
558 additions
and
215 deletions.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/EquipBackpackPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,77 @@ | ||
package dev.cammiescorner.camsbackpacks.network.c2s; | ||
|
||
import dev.cammiescorner.camsbackpacks.block.BackpackBlock; | ||
import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; | ||
import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; | ||
import dev.cammiescorner.camsbackpacks.item.BackpackItem; | ||
import net.minecraft.ChatFormatting; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.network.chat.MutableComponent; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.sounds.SoundEvents; | ||
import net.minecraft.sounds.SoundSource; | ||
import net.minecraft.world.ContainerHelper; | ||
import net.minecraft.world.entity.EquipmentSlot; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.state.properties.BlockStateProperties; | ||
|
||
public class EquipBackpackPacket { | ||
|
||
public static void send(boolean isBlockEntity, BlockPos pos) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static void handle(ServerPlayer player, BlockPos pos, boolean isBlockEntity) { | ||
Level world = player.level(); | ||
|
||
if (!world.mayInteract(player, pos)) { | ||
player.closeContainer(); | ||
MutableComponent message = isBlockEntity | ||
? Component.translatable("error.camsbackpacks.permission_pickup_at") | ||
: Component.translatable("error.camsbackpacks.permission_place_at"); | ||
player.sendSystemMessage(message.withStyle(ChatFormatting.RED), true); | ||
return; | ||
} | ||
|
||
if (isBlockEntity) { | ||
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) { | ||
ItemStack stack = new ItemStack(world.getBlockState(pos).getBlock().asItem()); | ||
CompoundTag tag = stack.getOrCreateTag(); | ||
|
||
ContainerHelper.saveAllItems(tag, blockEntity.inventory); | ||
blockEntity.wasPickedUp = true; | ||
player.setItemSlot(EquipmentSlot.CHEST, stack); | ||
|
||
if (blockEntity.hasCustomName()) | ||
stack.setHoverName(blockEntity.getName()); | ||
|
||
world.destroyBlock(pos, false, player); | ||
|
||
if (!BackpacksConfig.allowInventoryGui) { | ||
player.closeContainer(); | ||
player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled")); | ||
} | ||
|
||
} | ||
} else { | ||
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); | ||
|
||
if (stack.getItem() instanceof BackpackItem backpackItem && BackpackBlock.isStateReplaceable(world, pos) && pos.closerThan(player.blockPosition(), 3)) { | ||
world.playSound(null, pos, SoundEvents.WOOL_PLACE, SoundSource.BLOCKS, 1F, 1F); | ||
world.setBlockAndUpdate(pos, backpackItem.getBlock().defaultBlockState().setValue(BackpackBlock.FACING, player.getDirection()).setValue(BlockStateProperties.WATERLOGGED, !world.getFluidState(pos).isEmpty())); | ||
player.closeContainer(); | ||
|
||
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) { | ||
ContainerHelper.loadAllItems(stack.getOrCreateTag(), backpack.inventory); | ||
backpack.setName(stack.getHoverName()); | ||
} | ||
|
||
player.getItemBySlot(EquipmentSlot.CHEST).shrink(1); | ||
} | ||
} | ||
} | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
...n/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/OpenBackpackScreenPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,85 @@ | ||
package dev.cammiescorner.camsbackpacks.network.c2s; | ||
|
||
import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; | ||
import dev.cammiescorner.camsbackpacks.menu.BackpackMenu; | ||
import dev.cammiescorner.camsbackpacks.util.platform.Services; | ||
import net.minecraft.core.NonNullList; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.network.FriendlyByteBuf; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.Container; | ||
import net.minecraft.world.ContainerHelper; | ||
import net.minecraft.world.entity.EquipmentSlot; | ||
import net.minecraft.world.entity.player.Inventory; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.inventory.AbstractContainerMenu; | ||
import net.minecraft.world.inventory.ContainerLevelAccess; | ||
import net.minecraft.world.item.ItemStack; | ||
|
||
public class OpenBackpackScreenPacket { | ||
|
||
public static void send() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static void handle(ServerPlayer player) { | ||
if (!BackpacksConfig.allowInventoryGui) { | ||
player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled"), true); | ||
return; | ||
} | ||
|
||
final NonNullList<ItemStack> stacks = NonNullList.withSize(36, ItemStack.EMPTY); | ||
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); | ||
CompoundTag tag = stack.getOrCreateTag(); | ||
ContainerHelper.loadAllItems(tag, stacks); | ||
Container inventory = new Container() { | ||
@Override | ||
public void clearContent() { | ||
stacks.clear(); | ||
} | ||
|
||
@Override | ||
public int getContainerSize() { | ||
return stacks.size(); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return stacks.isEmpty(); | ||
} | ||
|
||
@Override | ||
public ItemStack getItem(int slot) { | ||
return stacks.get(slot); | ||
} | ||
|
||
@Override | ||
public ItemStack removeItem(int slot, int amount) { | ||
return ContainerHelper.removeItem(stacks, slot, amount); | ||
} | ||
|
||
@Override | ||
public ItemStack removeItemNoUpdate(int slot) { | ||
return ContainerHelper.takeItem(stacks, slot); | ||
} | ||
|
||
@Override | ||
public void setItem(int slot, ItemStack stack) { | ||
stacks.set(slot, stack); | ||
} | ||
|
||
@Override | ||
public void setChanged() { | ||
|
||
} | ||
|
||
@Override | ||
public boolean stillValid(Player player) { | ||
return true; | ||
} | ||
}; | ||
|
||
Services.MENU.openMenu(player, stack, inventory); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
Common/src/main/java/dev/cammiescorner/camsbackpacks/network/c2s/PlaceBackpackPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,49 @@ | ||
package dev.cammiescorner.camsbackpacks.network.c2s; | ||
|
||
import dev.cammiescorner.camsbackpacks.block.BackpackBlock; | ||
import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity; | ||
import dev.cammiescorner.camsbackpacks.item.BackpackItem; | ||
import net.minecraft.ChatFormatting; | ||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.sounds.SoundEvents; | ||
import net.minecraft.sounds.SoundSource; | ||
import net.minecraft.world.ContainerHelper; | ||
import net.minecraft.world.entity.EquipmentSlot; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.level.block.state.properties.BlockStateProperties; | ||
import net.minecraft.world.phys.BlockHitResult; | ||
|
||
public class PlaceBackpackPacket { | ||
|
||
public static void send(BlockHitResult hitResult) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static void handle(ServerPlayer player, BlockHitResult hitResult) { | ||
Level world = player.level(); | ||
BlockPos pos = BackpackBlock.isStateReplaceable(world, hitResult.getBlockPos()) ? hitResult.getBlockPos() : hitResult.getBlockPos().relative(hitResult.getDirection()); | ||
|
||
if (!world.mayInteract(player, pos)) { | ||
player.closeContainer(); | ||
player.sendSystemMessage(Component.translatable("error.camsbackpacks.permission_place_at").withStyle(ChatFormatting.RED), true); | ||
return; | ||
} | ||
|
||
ItemStack stack = player.getItemBySlot(EquipmentSlot.CHEST); | ||
|
||
if (BackpackBlock.isStateReplaceable(world, pos)) { | ||
world.playSound(null, pos, SoundEvents.WOOL_PLACE, SoundSource.BLOCKS, 1F, 1F); | ||
world.setBlockAndUpdate(pos, ((BackpackItem) stack.getItem()).getBlock().defaultBlockState().setValue(BackpackBlock.FACING, player.getDirection()).setValue(BlockStateProperties.WATERLOGGED, !world.getFluidState(pos).isEmpty())); | ||
|
||
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) { | ||
ContainerHelper.loadAllItems(stack.getOrCreateTag(), backpack.inventory); | ||
backpack.setName(stack.getHoverName()); | ||
} | ||
|
||
player.getItemBySlot(EquipmentSlot.CHEST).shrink(1); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
.../src/main/java/dev/cammiescorner/camsbackpacks/network/s2c/UpdateConfigurationPacket.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,33 @@ | ||
package dev.cammiescorner.camsbackpacks.network.s2c; | ||
|
||
import dev.cammiescorner.camsbackpacks.client.CamsBackpacksClient; | ||
import dev.cammiescorner.camsbackpacks.client.screen.BackpackScreen; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.network.chat.Component; | ||
import net.minecraft.server.level.ServerPlayer; | ||
|
||
import java.util.Collection; | ||
|
||
public class UpdateConfigurationPacket { | ||
|
||
public static void sendTo(Collection<ServerPlayer> players) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static void sendTo(ServerPlayer player) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public static void handle(boolean allowInventoryGui) { | ||
CamsBackpacksClient.chestSlotUiEnabled = allowInventoryGui; | ||
if (CamsBackpacksClient.chestSlotUiEnabled) { | ||
var minecraft = Minecraft.getInstance(); | ||
CamsBackpacksClient.backpackScreenIsOpen = false; | ||
|
||
if (minecraft.screen instanceof BackpackScreen screen && !screen.getMenu().isBlockEntity && minecraft.player != null) { | ||
minecraft.setScreen(null); | ||
minecraft.player.sendSystemMessage(Component.translatable("error.camsbackpacks.chest_slot_ui_disabled")); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[versions] | ||
neogradle = "6.+" | ||
mixingradle = "0.7-SNAPSHOT" | ||
mixingradle = "0.7.+" | ||
|
||
neoforge = "1.20.1-47.1.85" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/ConfigPacketMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package dev.cammiescorner.camsbackpacks.neoforge.mixin; | ||
|
||
import dev.cammiescorner.camsbackpacks.config.BackpacksConfig; | ||
import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; | ||
import dev.cammiescorner.camsbackpacks.neoforge.network.s2c.NFUpdateConfigurationPacket; | ||
import dev.cammiescorner.camsbackpacks.network.s2c.UpdateConfigurationPacket; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraftforge.network.PacketDistributor; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
|
||
import java.util.Collection; | ||
|
||
@Mixin(UpdateConfigurationPacket.class) | ||
public class ConfigPacketMixin { | ||
|
||
/** | ||
* @author Up | ||
* @reason I'm too lazy to set up multi loader networking rn | ||
*/ | ||
@Overwrite | ||
public static void sendTo(Collection<ServerPlayer> players) { | ||
NetworkHandler.INSTANCE.send(PacketDistributor.ALL.noArg(), new NFUpdateConfigurationPacket(BackpacksConfig.allowInventoryGui)); | ||
} | ||
|
||
/** | ||
* @author Up | ||
* @reason I'm too lazy to set up multi loader networking rn | ||
*/ | ||
@Overwrite | ||
public static void sendTo(ServerPlayer player) { | ||
NetworkHandler.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player), new NFUpdateConfigurationPacket(BackpacksConfig.allowInventoryGui)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/EquipPacketMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package dev.cammiescorner.camsbackpacks.neoforge.mixin; | ||
|
||
import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; | ||
import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFEquipBackpackPacket; | ||
import dev.cammiescorner.camsbackpacks.network.c2s.EquipBackpackPacket; | ||
import net.minecraft.core.BlockPos; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
|
||
@Mixin(EquipBackpackPacket.class) | ||
public class EquipPacketMixin { | ||
|
||
/** | ||
* @author Up | ||
* @reason I'm too lazy to set up multi loader networking rn | ||
*/ | ||
@Overwrite | ||
public static void send(boolean isBlockEntity, BlockPos pos) { | ||
NetworkHandler.INSTANCE.sendToServer(new NFEquipBackpackPacket(pos, isBlockEntity)); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
NeoForge/src/main/java/dev/cammiescorner/camsbackpacks/neoforge/mixin/OpenPacketMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.cammiescorner.camsbackpacks.neoforge.mixin; | ||
|
||
import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler; | ||
import dev.cammiescorner.camsbackpacks.neoforge.network.c2s.NFOpenBackpackScreenPacket; | ||
import dev.cammiescorner.camsbackpacks.network.c2s.OpenBackpackScreenPacket; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Overwrite; | ||
|
||
@Mixin(OpenBackpackScreenPacket.class) | ||
public class OpenPacketMixin { | ||
|
||
/** | ||
* @author Up | ||
* @reason I'm too lazy to set up multi loader networking rn | ||
*/ | ||
@Overwrite | ||
public static void send() { | ||
NetworkHandler.INSTANCE.sendToServer(new NFOpenBackpackScreenPacket()); | ||
} | ||
} |
Oops, something went wrong.