Skip to content

Commit

Permalink
finish neo port
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Jan 13, 2024
1 parent c0c3634 commit 3bbbdc0
Show file tree
Hide file tree
Showing 25 changed files with 558 additions and 215 deletions.
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);
}
}
}

}
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);
}
}
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);
}
}
}
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"));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.item.ItemStack;

public interface MenuHelper {

MenuProvider getMenuProvider(BackpackBlockEntity backpack);

void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity backpack);

void openMenu(ServerPlayer player, ItemStack backpackStack, Container inventory);
}
7 changes: 7 additions & 0 deletions NeoForge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ dependencies {
}
}

mixin {
add(sourceSets.main, "${mod_id}.refmap.json")

config("${mod_id}.mixins.json")
config("${mod_id}.neoforge.mixins.json")
}

jar {
archiveClassifier.set("slim")
}
Expand Down
2 changes: 1 addition & 1 deletion NeoForge/libs.versions.toml
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"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class Client {

public static void init() {
//FIXME this is handled by resourcefulconfig in 1.20.4+
ModLoadingContext.get().registerExtensionPoint(ConfigScreenHandler.ConfigScreenFactory.class,
() -> new ConfigScreenHandler.ConfigScreenFactory(
(minecraft, parent) -> new ConfigScreen(parent, null, CamsBackpacks.CONFIGURATOR.getConfig(BackpacksConfig.class))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.cammiescorner.camsbackpacks.neoforge.entrypoints;

import dev.cammiescorner.camsbackpacks.CamsBackpacks;
import dev.cammiescorner.camsbackpacks.neoforge.network.NetworkHandler;
import dev.cammiescorner.camsbackpacks.neoforge.services.NFRegistryHelper;
import dev.cammiescorner.camsbackpacks.util.platform.Services;
import net.minecraft.world.item.CreativeModeTabs;
Expand All @@ -9,7 +10,6 @@
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

Expand All @@ -25,6 +25,8 @@ public Main() {
bus.register(this);
CamsBackpacks.init();

NetworkHandler.registerMessages();

DistExecutor.safeRunWhenOn(Dist.CLIENT, () -> Client::init);
}

Expand Down
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));
}
}
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));
}
}
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());
}
}
Loading

0 comments on commit 3bbbdc0

Please sign in to comment.