Skip to content

Instantly share code, notes, and snippets.

@StarCheater
Created June 4, 2020 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save StarCheater/d595391eaac69c33f445cb2127f4d4af to your computer and use it in GitHub Desktop.
Save StarCheater/d595391eaac69c33f445cb2127f4d4af to your computer and use it in GitHub Desktop.
VintageStory Teleport any Entities
public class TrampolineBlock : Block
{
public override void OnEntityCollide(IWorldAccessor world, Entity entity, BlockPos pos, BlockFacing facing,
Vec3d collideSpeed, bool isImpact)
{
base.OnEntityCollide(world, entity, pos, facing, collideSpeed, isImpact);
TrampolineBlockE be = world.BlockAccessor.GetBlockEntity(pos) as TrampolineBlockE;
if (be == null) return;
be.OnEntityCollide(entity);
}
}
public class TrampolineBlockE : BlockEntity
{
public override void Initialize(ICoreAPI api)
{
base.Initialize(api);
RegisterGameTickListener(OnServerGameTick, 250);
}
Dictionary<long, TeleportingEntity> tpingEntities = new Dictionary<long, TeleportingEntity>();
List<long> toremove = new List<long>();
bool somebodyIsTeleporting;
private void OnServerGameTick(float dt)
{
toremove.Clear();
bool wasTeleporting = somebodyIsTeleporting;
somebodyIsTeleporting &= tpingEntities.Count > 0;
foreach (var val in tpingEntities)
{
if (val.Value.Entity.Teleporting) continue;
var tpLocation = val.Value.Entity.ServerPos.AsBlockPos;
val.Value.SecondsPassed += Math.Min(0.5f, dt);
if (Api.World.ElapsedMilliseconds - val.Value.LastCollideMs > 100)
{
toremove.Add(val.Key);
continue;
}
if (val.Value.SecondsPassed > 0.5 && tpLocation != null)
{
val.Value.Entity.TeleportTo(tpLocation.ToVec3d().Add(0.5,0.5,0.5));
toremove.Add(val.Key);
somebodyIsTeleporting = false;
MarkDirty();
}
}
foreach (long entityid in toremove)
{
tpingEntities.Remove(entityid);
}
if (wasTeleporting && !somebodyIsTeleporting)
{
MarkDirty();
}
}
public void OnEntityCollide(Entity entity)
{
TeleportingEntity tpe = null;
if (!tpingEntities.TryGetValue(entity.EntityId, out tpe))
{
tpingEntities[entity.EntityId] = tpe = new TeleportingEntity()
{
Entity = entity
};
}
tpe.LastCollideMs = Api.World.ElapsedMilliseconds;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment