Skip to content

Commit

Permalink
start porting to neoforge
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Jan 12, 2024
1 parent 5d955b1 commit fc3abf7
Show file tree
Hide file tree
Showing 20 changed files with 378 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public VoxelShape getShape(BlockState state, BlockGetter world, BlockPos pos, Co
}



@Override
public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack stack) {
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) {
Expand All @@ -80,32 +79,32 @@ public void setPlacedBy(Level world, BlockPos pos, BlockState state, @Nullable L
@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (!world.isClientSide()) {
if(!world.mayInteract(player, pos)) {
if (!world.mayInteract(player, pos)) {
((ServerPlayer) player).sendSystemMessage(Component.translatable("error.camsbackpacks.permission_use").withStyle(ChatFormatting.RED), true);
return InteractionResult.FAIL;
}

if (world.getBlockEntity(pos) instanceof BackpackBlockEntity backpack) {
if (ClientConfig.sneakPlaceBackpack && player.isShiftKeyDown() && player.getItemBySlot(EquipmentSlot.CHEST).isEmpty()) {

if (ClientConfig.sneakPlaceBackpack && player.isShiftKeyDown() && player.getItemBySlot(EquipmentSlot.CHEST).isEmpty()) {
if (world.getBlockEntity(pos) instanceof BackpackBlockEntity blockEntity) {
ItemStack stack = new ItemStack(this);
CompoundTag tag = stack.getOrCreateTag();

ContainerHelper.saveAllItems(tag, blockEntity.inventory);
blockEntity.wasPickedUp = true;
ContainerHelper.saveAllItems(tag, backpack.inventory);
backpack.wasPickedUp = true;

if (blockEntity.hasCustomName())
stack.setHoverName(blockEntity.getName());
if (backpack.hasCustomName())
stack.setHoverName(backpack.getName());

player.setItemSlot(EquipmentSlot.CHEST, stack);
world.destroyBlock(pos, false, player);
}
} else {
MenuProvider screenHandlerFactory = state.getMenuProvider(world, pos);
} else {
MenuProvider menuProvider = state.getMenuProvider(world, pos);

if (screenHandlerFactory != null) {
world.playSound(null, pos, SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.BLOCKS, 1F, 1F);
player.openMenu(screenHandlerFactory);
if (menuProvider != null) {
world.playSound(null, pos, SoundEvents.ARMOR_EQUIP_LEATHER, SoundSource.BLOCKS, 1F, 1F);
backpack.openMenu((ServerPlayer) player, menuProvider);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ public MenuProvider getMenuProvider() {
return Services.MENU.getMenuProvider(this);
}

public void openMenu(ServerPlayer player, MenuProvider menu) {
Services.MENU.openMenu(player, menu, this);
}

public @Nullable AbstractContainerMenu createMenu(int syncId, Inventory inv, Player player) {
return new BackpackMenu(syncId, inv, this, ContainerLevelAccess.create(player.level(), getBlockPos()), getBlockPos(), true);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package dev.cammiescorner.camsbackpacks.util.platform.service;

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

public interface MenuHelper {

MenuProvider getMenuProvider(BackpackBlockEntity backpack);

void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity backpack);
}
100 changes: 100 additions & 0 deletions NeoForge/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
plugins {
id 'idea'
alias neoforge.plugins.neogradle
alias neoforge.plugins.mixingradle
}

dependencies {
minecraft "${neoforge.neoforge.get()}"
compileOnly project(":Common")
annotationProcessor "${libs.mixin.asProvider().get()}:processor"

implementation fg.deobf(libs.resourcefulconfig.forge.get())
}

minecraft {
mappings channel: 'official', version: libs.versions.minecraft.get()

copyIdeResources = true //Calls processResources when in dev

// Automatically enable forge AccessTransformers if the file exists
// This location is hardcoded in Forge and can not be changed.
// https://github.com/MinecraftForge/MinecraftForge/blob/be1698bb1554f9c8fa2f58e32b9ab70bc4385e60/fmlloader/src/main/java/net/minecraftforge/fml/loading/moddiscovery/ModFile.java#L123
if (file('src/main/resources/META-INF/accesstransformer.cfg').exists()) {
accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')
}

runs {
configureEach {
property 'forge.logging.markers', 'REGISTRIES'
property 'forge.logging.console.level', 'debug'
}
client {
workingDirectory project.file('run')
ideaModule "${rootProject.name}.${project.name}.main"
mods {
modClientRun {
source sourceSets.main
source project(":Common").sourceSets.main
}
}

if (rootProject.hasProperty('mc_uuid')) {
args("--uuid", rootProject.mc_uuid)
}

if (rootProject.hasProperty('mc_username')) {
args("--username", rootProject.mc_username)
}

if (rootProject.hasProperty('mc_java_agent_path')) {
jvmArg("-javaagent:${rootProject.mc_java_agent_path}")
}
}

server {
workingDirectory project.file('run')
ideaModule "${rootProject.name}.${project.name}.main"
args '--nogui'
mods {
modServerRun {
source sourceSets.main
source project(":Common").sourceSets.main
}
}
}

data {
workingDirectory project.file('run')
ideaModule "${rootProject.name}.${project.name}.main"
args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/')
mods {
modDataRun {
source sourceSets.main
source project(":Common").sourceSets.main
}
}
}
}
}

sourceSets.main.resources { srcDir 'src/generated/resources' }

// NeoGradle compiles the game, but we don't want to add our common code to the game's code
Spec<Task> notNeoTask = { Task it -> !it.name.startsWith("neo") } as Spec<Task>

tasks.withType(JavaCompile).matching(notNeoTask).configureEach {
source(project(":Common").sourceSets.main.allSource)
}

tasks.withType(Javadoc).matching(notNeoTask).configureEach {
source(project(":Common").sourceSets.main.allJava)
}

tasks.named("sourcesJar", Jar) {
from(project(":Common").sourceSets.main.allSource)
}

tasks.withType(ProcessResources).matching(notNeoTask).configureEach {
from project(":Common").sourceSets.main.resources
}
10 changes: 9 additions & 1 deletion NeoForge/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
[versions]
neoforge = "47.1.65"
neogradle = "6.+"
mixingradle = "0.7-SNAPSHOT"

neoforge = "1.20.1-47.1.65"

[libraries]
neoforge = { module = "net.neoforged:forge", version.ref = "neoforge" }

[plugins]
neogradle = { id = "net.neoforged.gradle", version.ref = "neogradle" }
mixingradle = { id = "org.spongepowered.mixin", version.ref = "mixingradle" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package dev.cammiescorner.camsbackpacks.neoforge;

import dev.cammiescorner.camsbackpacks.CamsBackpacks;
import dev.cammiescorner.camsbackpacks.network.s2c.UpdateConfigurationPacket;
import net.minecraftforge.event.OnDatapackSyncEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(modid = CamsBackpacks.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class EventHandler {

@SubscribeEvent
public static void onDatapackReload(OnDatapackSyncEvent event) {
if(event.getPlayer() != null) {
UpdateConfigurationPacket.sendTo(event.getPlayer());
}
else {
UpdateConfigurationPacket.sendTo(event.getPlayerList().getPlayers());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package dev.cammiescorner.camsbackpacks.neoforge;

import dev.cammiescorner.camsbackpacks.CamsBackpacks;
import dev.cammiescorner.camsbackpacks.neoforge.services.NFRegistryHelper;
import dev.cammiescorner.camsbackpacks.util.platform.Services;
import net.minecraft.world.item.CreativeModeTabs;
import net.minecraftforge.event.BuildCreativeModeTabContentsEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;

@Mod(CamsBackpacks.MOD_ID)
public class Main {

public Main() {
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
NFRegistryHelper.BLOCKS.register(bus);
NFRegistryHelper.ITEMS.register(bus);
NFRegistryHelper.BLOCK_ENTITY_TYPES.register(bus);
NFRegistryHelper.MENU_TYPES.register(bus);
bus.register(this);
CamsBackpacks.init();
}

@SubscribeEvent
public void onCreativeTabSetup(BuildCreativeModeTabContentsEvent event) {
if(event.getTabKey() == CreativeModeTabs.TOOLS_AND_UTILITIES) {
Services.REGISTRY.getModBlocks().forEach(event::accept);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package dev.cammiescorner.camsbackpacks.neoforge.services;

import dev.cammiescorner.camsbackpacks.block.entity.BackpackBlockEntity;
import dev.cammiescorner.camsbackpacks.util.platform.service.MenuHelper;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraftforge.network.NetworkHooks;

public class NFMenuHelper implements MenuHelper {
@Override
public MenuProvider getMenuProvider(BackpackBlockEntity backpack) {
return new SimpleMenuProvider(backpack::createMenu, backpack.getDisplayName());
}

@Override
public void openMenu(ServerPlayer player, MenuProvider menu, BackpackBlockEntity backpack) {
NetworkHooks.openScreen(player, menu, buf -> backpack.writeInitMenuData(player, buf));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.cammiescorner.camsbackpacks.neoforge.services;

import dev.cammiescorner.camsbackpacks.CamsBackpacks;
import dev.cammiescorner.camsbackpacks.util.platform.service.PlatformHelper;
import net.minecraftforge.fml.ModList;

public class NFPlatformHelper implements PlatformHelper {
@Override
public boolean isModLoaded(String modid) {
return ModList.get().isLoaded(modid);
}

@Override
public String getIssuesUrl() {
return ModList.get().getModContainerById(CamsBackpacks.MOD_ID).orElseThrow().getModInfo().getOwningFile().getFileProperties().get("issueTrackerURL") instanceof String value ? value : CamsBackpacks.MOD_ID;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package dev.cammiescorner.camsbackpacks.neoforge.services;

import dev.cammiescorner.camsbackpacks.CamsBackpacks;
import dev.cammiescorner.camsbackpacks.util.platform.service.RegistryHelper;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraftforge.common.extensions.IForgeMenuType;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

public class NFRegistryHelper implements RegistryHelper {

public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, CamsBackpacks.MOD_ID);
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, CamsBackpacks.MOD_ID);
public static final DeferredRegister<BlockEntityType<?>> BLOCK_ENTITY_TYPES = DeferredRegister.create(ForgeRegistries.BLOCK_ENTITY_TYPES, CamsBackpacks.MOD_ID);
public static final DeferredRegister<MenuType<?>> MENU_TYPES = DeferredRegister.create(ForgeRegistries.MENU_TYPES, CamsBackpacks.MOD_ID);

@Override
public <T extends BlockEntity> Supplier<BlockEntityType<T>> createBlockEntityType(String name, Supplier<BlockEntityType.BlockEntitySupplier<T>> factory, Supplier<Block>[] supportedBlocks) {
return BLOCK_ENTITY_TYPES.register(name, () -> {
var blocks = new Block[supportedBlocks.length];
for (int i = 0; i < supportedBlocks.length; i++) {
blocks[i] = supportedBlocks[i].get();
}
//noinspection DataFlowIssue
return BlockEntityType.Builder.of(factory.get(), blocks).build(null);
});
}

@Override
public Stream<Supplier<? extends Block>> getModBlocks() {
return BLOCKS.getEntries().stream().map(UnaryOperator.identity());
}

@Override
public <T extends Item> Supplier<T> createItem(String name, Supplier<T> item) {
return ITEMS.register(name, item);
}

@Override
public <T extends Block> Supplier<T> createBlock(String name, Supplier<T> block) {
return BLOCKS.register(name, block);
}

@Override
public <T extends AbstractContainerMenu> Supplier<MenuType<T>> registerMenu(String name, Supplier<MenuExtendedFactory<T>> factory) {
return MENU_TYPES.register(name, () -> IForgeMenuType.create((windowId, inv, data) -> factory.get().create(windowId, inv, data)));
}
}
10 changes: 10 additions & 0 deletions NeoForge/src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copied from accesswidener file
#TODO find a way to automate this

public net.minecraft.client.gui.screens.MenuScreens m_96206_(Lnet/minecraft/world/inventory/MenuType;Lnet/minecraft/client/gui/screens/MenuScreens$ScreenConstructor;)V # register
public net.minecraft.world.level.block.entity.BlockEntityType$BlockEntitySupplier
public net.minecraft.client.gui.screens.MenuScreens$ScreenConstructor

public net.minecraft.world.inventory.InventoryMenu f_39699_ # TEXTURE_EMPTY_SLOTS
public net.minecraft.world.inventory.InventoryMenu m_269535_(Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/entity/EquipmentSlot;Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/item/ItemStack;)V # onEquipItem
public net.minecraft.world.inventory.CraftingMenu m_150546_(Lnet/minecraft/world/inventory/AbstractContainerMenu;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/player/Player;Lnet/minecraft/world/inventory/CraftingContainer;Lnet/minecraft/world/inventory/ResultContainer;)V # slotChangedCraftingGrid
Loading

0 comments on commit fc3abf7

Please sign in to comment.