Skip to content

Commit

Permalink
Merge pull request #912 from Prunoideae/1.21.1/spawner-gui-fixes
Browse files Browse the repository at this point in the history
Fixed NPE due to `spawnerEither` is `null` and chest GUI deleting items picked up
  • Loading branch information
LatvianModder authored Oct 21, 2024
2 parents b1b0e0e + a8a945b commit ea692f2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Info("""
Expand Down Expand Up @@ -60,8 +61,8 @@ public MobSpawnType getType() {
return type;
}

@Info("The spawner that spawned the entity. Can be null if the entity was spawned by worldgen.")
@Nullable
@Info("The spawner that spawned the entity.")
@NotNull
public SpawnerJS getSpawner() {
if (spawner == null) {
spawner = SpawnerJS.of(spawnerEither);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/level/SpawnerJS.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

public record SpawnerJS(@Nullable Entity entity, @Nullable BlockContainerJS block) {
public static SpawnerJS of(Either<BlockEntity, Entity> spawner) {
if (spawner == null) {
return new SpawnerJS(null, null);
}

var e = spawner.right().orElse(null);

if (e != null) {
Expand All @@ -21,4 +25,8 @@ public static SpawnerJS of(Either<BlockEntity, Entity> spawner) {

return new SpawnerJS(null, null);
}

public boolean isWorldgen() {
return entity == null && block == null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.latvian.mods.kubejs.bindings.event.ServerEvents;
import dev.latvian.mods.kubejs.command.CommandRegistryKubeEvent;
import dev.latvian.mods.kubejs.command.KubeJSCommands;
import dev.latvian.mods.kubejs.gui.chest.CustomChestMenu;
import dev.latvian.mods.kubejs.level.SimpleLevelKubeEvent;
import dev.latvian.mods.kubejs.script.PlatformWrapper;
import dev.latvian.mods.kubejs.script.ScriptType;
Expand All @@ -17,16 +18,19 @@
import net.minecraft.nbt.NbtAccounter;
import net.minecraft.nbt.NbtIo;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.storage.LevelResource;
import net.neoforged.api.distmarker.Dist;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.common.util.TriState;
import net.neoforged.neoforge.event.AddReloadListenerEvent;
import net.neoforged.neoforge.event.CommandEvent;
import net.neoforged.neoforge.event.RegisterCommandsEvent;
import net.neoforged.neoforge.event.entity.player.ItemEntityPickupEvent;
import net.neoforged.neoforge.event.level.LevelEvent;
import net.neoforged.neoforge.event.server.ServerAboutToStartEvent;
import net.neoforged.neoforge.event.server.ServerStartingEvent;
Expand Down Expand Up @@ -173,4 +177,13 @@ public static void command(CommandEvent event) {
public static void addReloadListeners(AddReloadListenerEvent event) {
event.addListener(new KubeJSReloadListener(event.getServerResources()));
}

@SubscribeEvent
public static void preventPickupDuringChestGUI(ItemEntityPickupEvent.Pre event) {
var e = event.getPlayer();

if (e instanceof ServerPlayer player && player.isAlive() && !player.hasDisconnected() && player.containerMenu instanceof CustomChestMenu) {
event.setCanPickup(TriState.FALSE);
}
}
}

0 comments on commit ea692f2

Please sign in to comment.