Skip to content

Commit

Permalink
fixes some cases of recursive wormhole generation
Browse files Browse the repository at this point in the history
wormhole teleports were spawning wormholes, and then the old wormhole could go through the new one...
  • Loading branch information
Thutmose committed Nov 7, 2022
1 parent 1c93cfb commit 4b889fc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
23 changes: 17 additions & 6 deletions src/main/java/pokecube/legends/entity/WormholeEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import pokecube.api.PokecubeAPI;
import pokecube.api.entity.pokemob.IPokemob;
import pokecube.api.entity.pokemob.PokemobCaps;
import pokecube.core.PokecubeCore;
import pokecube.core.eventhandlers.EventsHandler;
import pokecube.core.utils.EntityTools;
import pokecube.legends.init.EntityInit;
Expand Down Expand Up @@ -160,15 +161,21 @@ public void deserializeNBT(final Tag nbt)

public static void onTeleport(final EntityTeleportEvent event)
{
final Level world = event.getEntity().level;
Entity entity = event.getEntity();
final Level world = entity.level;
if (world.isClientSide()) return;
if (!(world instanceof ServerLevel)) return;

final long lastTp = entity.getPersistentData().getLong("pokecube_legends:uwh_use")
+ WormholeEntity.wormholeReUseDelay;
final long now = Tracker.instance().getTick();

if (now < lastTp) return;

final IWormholeWorld holes = world.getCapability(WormholeSpawns.WORMHOLES_CAP).orElse(null);
if (holes == null) return;

final double chance = ItemList.is(WormholeSpawns.SPACE_WORMS, event.getEntity())
? WormholeSpawns.teleWormholeChanceWorms
final double chance = ItemList.is(WormholeSpawns.SPACE_WORMS, entity) ? WormholeSpawns.teleWormholeChanceWorms
: WormholeSpawns.teleWormholeChanceNormal;

final Random rand = world.getRandom();
Expand All @@ -181,7 +188,7 @@ public static void onTeleport(final EntityTeleportEvent event)

// If it is a pokemob, check if holding a location linker, if so, use
// that for destination of the wormhole!
final IPokemob pokemob = PokemobCaps.getPokemobFor(event.getEntity());
final IPokemob pokemob = PokemobCaps.getPokemobFor(entity);
if (pokemob != null)
{
ILinkStorage link = null;
Expand All @@ -193,7 +200,7 @@ public static void onTeleport(final EntityTeleportEvent event)
}
if (link != null)
{
GlobalPos linked_pos = link.getLinkedPos(event.getEntity());
GlobalPos linked_pos = link.getLinkedPos(entity);
if (linked_pos != null)
{
linked_pos = GlobalPos.of(linked_pos.dimension(), linked_pos.pos().above(2));
Expand Down Expand Up @@ -438,12 +445,16 @@ protected void pushEntities()
if (!list.isEmpty()) for (Entity entity : list)
{
entity = EntityTools.getCoreEntity(entity);

// These cannot go through a wormhole.
if (ItemList.is(WormholeSpawns.SPACE_ANCHORED, entity)) continue;

final long lastTp = entity.getPersistentData().getLong("pokecube_legends:uwh_use")
+ WormholeEntity.wormholeReUseDelay;
final long now = Tracker.instance().getTick();
final UUID uuid = entity.getUUID();
if (now < lastTp || tpd.contains(uuid)) continue;
PokecubeAPI.logDebug("Transfering {} through a wormhole!", entity);
if (PokecubeCore.getConfig().debug_misc) PokecubeAPI.logInfo("Transfering {} through a wormhole!", entity);
tpd.add(uuid);
entity.getPersistentData().putLong("pokecube_legends:uwh_use", now);

Expand Down
16 changes: 7 additions & 9 deletions src/main/java/pokecube/legends/spawns/WormholeSpawns.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public CompoundTag serializeNBT()
{
final CompoundTag nbt = new CompoundTag();
final ListTag list = new ListTag();
for (final BlockPos pos : this.getWormholes())
list.add(NbtUtils.writeBlockPos(pos));
for (final BlockPos pos : this.getWormholes()) list.add(NbtUtils.writeBlockPos(pos));
nbt.put("wormholes", list);
return nbt;
}
Expand All @@ -78,8 +77,7 @@ public void deserializeNBT(final CompoundTag nbt)
{
this.getWormholes().clear();
final ListTag list = nbt.getList("wormholes", 10);
for (final Tag tag : list)
this.getWormholes().add(NbtUtils.readBlockPos((CompoundTag) tag));
for (final Tag tag : list) this.getWormholes().add(NbtUtils.readBlockPos((CompoundTag) tag));
}

@Override
Expand Down Expand Up @@ -108,14 +106,15 @@ public void removeWormhole(final BlockPos pos)

static WormholeSpawns INSTANCE = new WormholeSpawns();

public static double randomWormholeChance = 0.00001;
public static double randomWormholeChance = 0.00001;
public static double randomWormholeDistance = 64;
public static double randomWormholeSpacing = 128;
public static double randomWormholeSpacing = 128;

public static double teleWormholeChanceNormal = 0.01;
public static double teleWormholeChanceWorms = 0.75;
public static double teleWormholeChanceWorms = 0.75;

public static final ResourceLocation SPACE_WORMS = new ResourceLocation(Reference.ID, "space_worm");
public static final ResourceLocation SPACE_ANCHORED = new ResourceLocation(Reference.ID, "space_anchored");

public static void init()
{
Expand Down Expand Up @@ -182,8 +181,7 @@ public void onTickEnd(final ServerLevel world)
final BlockPos p = WormholeSpawns.getWormholePos(world, v.getPos());
final Vector3 pos = new Vector3().set(p);

for (final BlockPos p2 : holes.getWormholes())
if (p2.closerThan(pos.getPos(), wormholeSpacing)) return;
for (final BlockPos p2 : holes.getWormholes()) if (p2.closerThan(pos.getPos(), wormholeSpacing)) return;

final WormholeEntity wormhole = EntityInit.WORMHOLE.get().create(world);
pos.moveEntity(wormhole);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"replace": false,
"values": [
"pokecube_legends:wormhole",
"minecraft:ender_dragon"
]
}

0 comments on commit 4b889fc

Please sign in to comment.