Skip to content

Commit

Permalink
Fix crashes and otherwise poor UI behavior when in spectator mode (#5005
Browse files Browse the repository at this point in the history
)
  • Loading branch information
KnightMiner committed Dec 27, 2022
1 parent f8e5b5d commit f6e74f7
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/java/slimeknights/tconstruct/shared/CommonsEvents.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickBlock;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.NetworkHooks;
import slimeknights.mantle.inventory.BaseContainerMenu;
import slimeknights.tconstruct.TConstruct;
import slimeknights.tconstruct.common.Sounds;
import slimeknights.tconstruct.world.TinkerWorld;

import java.util.Objects;

@SuppressWarnings("unused")
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Mod.EventBusSubscriber(modid = TConstruct.MOD_ID)
Expand Down Expand Up @@ -40,6 +50,32 @@ static void onLivingJump(LivingEvent.LivingJumpEvent event) {
}
}

/** Handles opening our containers as the vanilla logic does not grant TE access */
@SubscribeEvent
static void openSpectatorMenu(RightClickBlock event) {
Player player = event.getPlayer();
if (player.isSpectator()) {
BlockPos pos = event.getPos();
Level world = event.getWorld();
BlockState state = world.getBlockState(pos);
// only handle our blocks, no guarantee this will work with other mods
if (TConstruct.MOD_ID.equals(Objects.requireNonNull(state.getBlock().getRegistryName()).getNamespace())) {
MenuProvider provider = state.getMenuProvider(world, pos);
event.setCanceled(true);
if (provider != null) {
if (player instanceof ServerPlayer serverPlayer) {
NetworkHooks.openGui(serverPlayer, provider, pos);
if (player.containerMenu instanceof BaseContainerMenu<?> menu) {
menu.syncOnOpen(serverPlayer);
}
}
event.setCancellationResult(InteractionResult.SUCCESS);
}
event.setCancellationResult(InteractionResult.PASS);
}
}
}

private static void bounce(Entity entity, float amount) {
entity.setDeltaMovement(entity.getDeltaMovement().add(0.0D, amount, 0.0D));
entity.playSound(Sounds.SLIMY_BOUNCE.getSound(), 0.5f + amount, 1f);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.MenuProvider;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -25,6 +26,12 @@ protected AbstractCastingBlock(Properties builder, boolean requireCast) {
this.requireCast = requireCast;
}

@Override
@Deprecated
public MenuProvider getMenuProvider(BlockState pState, Level pLevel, BlockPos pPos) {
return null;
}

@Deprecated
@Override
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult rayTraceResult) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public void slotsChanged(Container inventoryIn) {
*/
@Override
public boolean clickMenuButton(Player playerIn, int id) {
// no letting ghosts choose modifiers
if (playerIn.isSpectator()) {
return false;
}
if (id >= 0 && tile != null) {
tile.selectModifier(id);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public void slotsChanged(Container inventoryIn) {}
*/
@Override
public boolean clickMenuButton(Player playerIn, int id) {
// no letting ghosts choose patterns
if (playerIn.isSpectator()) {
return false;
}
if (id >= 0 && tile != null) {
tile.selectRecipe(id);
}
Expand Down

0 comments on commit f6e74f7

Please sign in to comment.