Skip to content

Commit

Permalink
feat: ✨ Added infinity upgrade
Browse files Browse the repository at this point in the history
- makes all items in backpack infinite
- only admins can put additional items in (and make them infinite that way) once the upgrade is put in upgrade slot
- only admins can break / pickup backpack with infinity slot
  • Loading branch information
P3pp3rF1y committed Jan 29, 2025
1 parent 58003be commit d52d048
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 8 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ org.gradle.daemon=false

mod_id=sophisticatedbackpacks
mod_group_id=sophisticatedbackpacks
mod_version=3.22.5
mod_version=3.23.0
sonar_project_key=sophisticatedbackpacks:SophisticatedBackpacks
github_package_url=https://maven.pkg.github.com/P3pp3rF1y/SophisticatedBackpacks

Expand Down Expand Up @@ -31,5 +31,5 @@ crafting_tweaks_cf_file_id=4596466
chipped_cf_file_id=5077656
resourcefullib_cf_file_id=5070629
athena_cf_file_id=4764357
sc_version=[1.20.1-1.1.0,1.20.4)
sc_version=[1.20.1-1.2.0,1.20.4)
parchment_version=2023.09.03-1.20.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.p3pp3rf1y.sophisticatedbackpacks.backpack;

import com.mojang.math.Axis;
import net.minecraft.ChatFormatting;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
Expand Down Expand Up @@ -47,6 +48,7 @@
import net.minecraftforge.fluids.FluidUtil;
import net.minecraftforge.network.NetworkHooks;
import net.p3pp3rf1y.sophisticatedbackpacks.api.CapabilityBackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.client.gui.SBPTranslationHelper;
import net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContainer;
import net.p3pp3rf1y.sophisticatedbackpacks.common.gui.BackpackContext;
import net.p3pp3rf1y.sophisticatedbackpacks.init.ModBlocks;
Expand Down Expand Up @@ -144,19 +146,24 @@ public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {

@SuppressWarnings("deprecation")
@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (world.isClientSide) {
public InteractionResult use(BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
if (level.isClientSide) {
return InteractionResult.SUCCESS;
}

ItemStack heldItem = player.getItemInHand(hand);
if (player.isShiftKeyDown() && heldItem.isEmpty()) {
putInPlayersHandAndRemove(state, world, pos, player, hand);
return InteractionResult.SUCCESS;
if (hasPermissionsToPickup(player)) {
putInPlayersHandAndRemove(state, level, pos, player, InteractionHand.MAIN_HAND);
return InteractionResult.SUCCESS;
} else {
return InteractionResult.FAIL;
}

}

if (!heldItem.isEmpty() && heldItem.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).isPresent()) {
WorldHelper.getBlockEntity(world, pos, BackpackBlockEntity.class)
WorldHelper.getBlockEntity(level, pos, BackpackBlockEntity.class)
.flatMap(te -> te.getBackpackWrapper().getFluidHandler()).ifPresent(backpackFluidHandler ->
player.getCapability(ForgeCapabilities.ITEM_HANDLER).ifPresent(playerInventory -> {
FluidActionResult resultOfEmptying = FluidUtil.tryEmptyContainerAndStow(heldItem, backpackFluidHandler, playerInventory, FluidType.BUCKET_VOLUME, player, true);
Expand All @@ -174,10 +181,18 @@ public InteractionResult use(BlockState state, Level world, BlockPos pos, Player

BackpackContext.Block backpackContext = new BackpackContext.Block(pos);
NetworkHooks.openScreen((ServerPlayer) player, new SimpleMenuProvider((w, p, pl) -> new BackpackContainer(w, pl, backpackContext),
getBackpackDisplayName(world, pos)), backpackContext::toBuffer);
getBackpackDisplayName(level, pos)), backpackContext::toBuffer);
return InteractionResult.SUCCESS;
}

private static boolean hasPermissionsToPickup(Player player) {
if (player.hasPermissions(2)) {
return true;
}
player.displayClientMessage(SBPTranslationHelper.INSTANCE.translStatusMessage("infinity_upgrade_only_admin_pickup").withStyle(ChatFormatting.RED), true);
return false;
}

private Component getBackpackDisplayName(Level world, BlockPos pos) {
Component defaultDisplayName = new ItemStack(ModItems.BACKPACK.get()).getHoverName();
return WorldHelper.getBlockEntity(world, pos, BackpackBlockEntity.class).map(te -> te.getBackpackWrapper().getBackpack().getHoverName()).orElse(defaultDisplayName);
Expand Down Expand Up @@ -237,6 +252,12 @@ public static void playerInteract(PlayerInteractEvent.RightClickBlock event) {
return;
}

if (!hasPermissionsToPickup(player)) {
event.setCanceled(true);
event.setCancellationResult(InteractionResult.FAIL);
return;
}

putInPlayersHandAndRemove(state, level, pos, player, player.getMainHandItem().isEmpty() ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND);

event.setCanceled(true);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.p3pp3rf1y.sophisticatedbackpacks.common;

import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
Expand All @@ -26,13 +27,17 @@
import net.minecraftforge.event.entity.player.EntityItemPickupEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.level.BlockEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.p3pp3rf1y.sophisticatedbackpacks.Config;
import net.p3pp3rf1y.sophisticatedbackpacks.api.CapabilityBackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.api.IAttackEntityResponseUpgrade;
import net.p3pp3rf1y.sophisticatedbackpacks.api.IBlockClickResponseUpgrade;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackBlock;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.BackpackBlockEntity;
import net.p3pp3rf1y.sophisticatedbackpacks.backpack.wrapper.IBackpackWrapper;
import net.p3pp3rf1y.sophisticatedbackpacks.client.gui.SBPTranslationHelper;
import net.p3pp3rf1y.sophisticatedbackpacks.init.ModBlocks;
import net.p3pp3rf1y.sophisticatedbackpacks.init.ModItems;
import net.p3pp3rf1y.sophisticatedbackpacks.network.AnotherPlayerBackpackOpenMessage;
Expand All @@ -42,8 +47,10 @@
import net.p3pp3rf1y.sophisticatedcore.network.PacketHandler;
import net.p3pp3rf1y.sophisticatedcore.network.SyncPlayerSettingsMessage;
import net.p3pp3rf1y.sophisticatedcore.settings.SettingsManager;
import net.p3pp3rf1y.sophisticatedcore.upgrades.infinity.InfinityUpgradeItem;
import net.p3pp3rf1y.sophisticatedcore.upgrades.jukebox.ServerStorageSoundHandler;
import net.p3pp3rf1y.sophisticatedcore.util.InventoryHelper;
import net.p3pp3rf1y.sophisticatedcore.util.WorldHelper;

import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -73,6 +80,7 @@ public void registerHandlers() {
eventBus.addListener(this::onPlayerRespawn);
eventBus.addListener(this::onWorldTick);
eventBus.addListener(this::interactWithEntity);
eventBus.addListener(this::handleBreakBackpackWithInfinityUpgrade);
}

private static final int BACKPACK_CHECK_COOLDOWN = 40;
Expand Down Expand Up @@ -250,4 +258,19 @@ private void onItemPickup(EntityItemPickupEvent event) {
event.setCanceled(true); //cancelling even when the stack isn't empty at this point to prevent full stack from before pickup to be picked up by player
}
}

private void handleBreakBackpackWithInfinityUpgrade(BlockEvent.BreakEvent event) {
Player player = event.getPlayer();

if (player.hasPermissions(2) || !(event.getState().getBlock() instanceof BackpackBlock)) {
return;
}

if (WorldHelper.getBlockEntity(event.getLevel(), event.getPos(), BackpackBlockEntity.class)
.map(backpackBlockEntity -> !backpackBlockEntity.getStorageWrapper().getUpgradeHandler().getTypeWrappers(InfinityUpgradeItem.TYPE).isEmpty())
.orElse(false)) {
event.setCanceled(true);
player.displayClientMessage(SBPTranslationHelper.INSTANCE.translStatusMessage("infinity_upgrade_only_admin_break").withStyle(ChatFormatting.RED), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
import net.p3pp3rf1y.sophisticatedcore.upgrades.filter.FilterUpgradeContainer;
import net.p3pp3rf1y.sophisticatedcore.upgrades.filter.FilterUpgradeItem;
import net.p3pp3rf1y.sophisticatedcore.upgrades.filter.FilterUpgradeTab;
import net.p3pp3rf1y.sophisticatedcore.upgrades.infinity.InfinityUpgradeItem;
import net.p3pp3rf1y.sophisticatedcore.upgrades.jukebox.JukeboxUpgradeContainer;
import net.p3pp3rf1y.sophisticatedcore.upgrades.jukebox.JukeboxUpgradeItem;
import net.p3pp3rf1y.sophisticatedcore.upgrades.jukebox.JukeboxUpgradeTab;
Expand Down Expand Up @@ -247,6 +248,7 @@ private ModItems() {
public static final RegistryObject<XpPumpUpgradeItem> XP_PUMP_UPGRADE = ITEMS.register("xp_pump_upgrade", () -> new XpPumpUpgradeItem(Config.SERVER.xpPumpUpgrade, Config.SERVER.maxUpgradesPerStorage));
public static final RegistryObject<AnvilUpgradeItem> ANVIL_UPGRADE = ITEMS.register("anvil_upgrade", AnvilUpgradeItem::new);
public static final RegistryObject<SmithingUpgradeItem> SMITHING_UPGRADE = ITEMS.register("smithing_upgrade", SmithingUpgradeItem::new);
public static final RegistryObject<InfinityUpgradeItem> INFINITY_UPGRADE = ITEMS.register("infinity_upgrade", () -> new InfinityUpgradeItem(Config.SERVER.maxUpgradesPerStorage));

public static final RegistryObject<ItemBase> UPGRADE_BASE = ITEMS.register("upgrade_base", () -> new ItemBase(new Item.Properties().stacksTo(16)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@
"item.sophisticatedbackpacks.anvil_upgrade.tooltip": "Anvil in an upgrade tab",
"item.sophisticatedbackpacks.smithing_upgrade": "Smithing Upgrade",
"item.sophisticatedbackpacks.smithing_upgrade.tooltip": "Smithing Table in an upgrade tab",
"item.sophisticatedbackpacks.infinity_upgrade": "Infinity Upgrade",
"item.sophisticatedbackpacks.infinity_upgrade.tooltip": "Makes all items in backpack infinite",
"item.sophisticatedbackpacks.chipped.botanist_workbench_upgrade": "Chipped: Botanist's Workbench Upgrade",
"item.sophisticatedbackpacks.chipped.botanist_workbench_upgrade.tooltip": "Chipped Botanist's Workbench in an upgrade tab",
"item.sophisticatedbackpacks.chipped.glassblower_upgrade": "Chipped: Glassblower Upgrade",
Expand All @@ -125,6 +127,8 @@
"keybind.sophisticatedbackpacks.toggle_upgrade_3": "Switch Upgrade in the 3rd Slot On/Off",
"keybind.sophisticatedbackpacks.toggle_upgrade_4": "Switch Upgrade in the 4th Slot On/Off",
"keybind.sophisticatedbackpacks.toggle_upgrade_5": "Switch Upgrade in the 5th Slot On/Off",
"gui.sophisticatedbackpacks.status.infinity_upgrade_only_admin_break": "Only Admins can break backpack with Infinity Upgrade",
"gui.sophisticatedbackpacks.status.infinity_upgrade_only_admin_pickup": "Only Admins can pickup backpack with Infinity Upgrade",
"gui.sophisticatedbackpacks.status.upgrade_switched_off": "%s Switched Off",
"gui.sophisticatedbackpacks.status.upgrade_switched_on": "%s Switched On",
"gui.sophisticatedbackpacks.status.nothing_to_restock": "No stacks could be restocked from inventory",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"parent": "item/generated",
"textures": {
"layer0": "sophisticatedbackpacks:item/infinity_upgrade"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d52d048

Please sign in to comment.