Skip to content

Commit

Permalink
fix: bed location might be null below 1.16.5
Browse files Browse the repository at this point in the history
  • Loading branch information
PeyaPeyaPeyang committed Aug 27, 2024
1 parent 79c0e5e commit b476e51
Showing 1 changed file with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.AllArgsConstructor;
import lombok.Data;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
Expand All @@ -11,6 +12,7 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.kunlab.scenamatica.commons.utils.MapUtils;
import org.kunlab.scenamatica.enums.MinecraftVersion;
import org.kunlab.scenamatica.interfaces.scenariofile.StructureSerializer;
import org.kunlab.scenamatica.interfaces.structures.minecraft.entity.LivingEntityStructure;
import org.kunlab.scenamatica.interfaces.structures.minecraft.entity.entities.HumanEntityStructure;
Expand All @@ -24,6 +26,8 @@
import org.kunlab.scenamatica.structures.minecraft.inventory.PlayerInventoryStructureImpl;
import org.kunlab.scenamatica.structures.minecraft.misc.LocationStructureImpl;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -229,13 +233,39 @@ public static HumanEntityStructure ofHuman(@NotNull HumanEntity entity)
entity.getMainHand(),
cooldown,
entity.getSleepTicks(),
entity.getBedSpawnLocation() == null ? null: LocationStructureImpl.of(entity.getBedSpawnLocation()),
retrievePlayerBedLocation(entity),
entity.isBlocking(),
entity.getGameMode(),
nmsHuman.getFoodLevel()
);
}

private static LocationStructure retrievePlayerBedLocation(HumanEntity human)
{
Location bedLoc;
// 1.16 からは, メソッド名が #getBedLocation に変更されている。
if (MinecraftVersion.current().isAtLeast(MinecraftVersion.V1_16))
{
if (!human.isSleeping())
return null;

try
{
Method method = HumanEntity.class.getMethod("getBedLocation");
bedLoc = (Location) method.invoke(human);
}
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
{
throw new IllegalStateException(e);
}
}
else
bedLoc = human.getBedSpawnLocation();

return bedLoc == null ? null: LocationStructureImpl.of(bedLoc);
}


@Override
public boolean equals(Object o)
{
Expand Down

0 comments on commit b476e51

Please sign in to comment.