use of net.glowstone.entity.GlowEntity in project Glowstone by GlowstoneMC.
the class ItemSpawn method rightClickBlock.
@Override
public void rightClickBlock(GlowPlayer player, GlowBlock against, BlockFace face, ItemStack holding, Vector clickedLoc) {
Location location = against.getLocation().add(face.getModX(), face.getModY(), face.getModZ());
// TODO: change mob spawner when clicked by monster egg
if (holding.hasItemMeta() && holding.getItemMeta() instanceof GlowMetaSpawn) {
GlowMetaSpawn meta = (GlowMetaSpawn) holding.getItemMeta();
EntityType type = meta.getSpawnedType();
CompoundTag tag = meta.getEntityTag();
if (type != null) {
GlowEntity entity = against.getWorld().spawn(location.add(0.5, 0, 0.5), EntityRegistry.getEntity(type), SpawnReason.SPAWNER_EGG);
if (tag != null) {
EntityStorage.load(entity, tag);
}
}
}
}
use of net.glowstone.entity.GlowEntity in project Glowstone by GlowstoneMC.
the class VehicleMoveHandler method handle.
@Override
public void handle(GlowSession session, VehicleMoveMessage message) {
GlowPlayer player = session.getPlayer();
if (!player.isInsideVehicle() || !(player.getVehicle() instanceof Vehicle)) {
return;
}
GlowEntity vehicle = (GlowEntity) player.getVehicle();
Location oldLocation = vehicle.getLocation();
Location newLocation = oldLocation.clone();
message.update(newLocation);
// don't let players reach an illegal position
if (Math.abs(newLocation.getBlockX()) > 32000000 || Math.abs(newLocation.getBlockZ()) > 32000000) {
session.getPlayer().kickPlayer("Illegal position");
return;
}
/*
don't let players move more than 100 blocks in a single packet
if they move greater than 10 blocks, but less than 100, just warn
this is NOT robust hack prevention - only to prevent client
confusion about where its actual location is (e.g. during login)
*/
if (Position.hasMoved(oldLocation, newLocation) && !player.isDead() && !vehicle.isDead()) {
double distance = newLocation.distanceSquared(oldLocation);
if (distance > 100 * 100) {
session.getPlayer().kickPlayer("You moved too quickly :( (Hacking?)");
return;
} else if (distance > 100) {
GlowServer.logger.warning(session.getPlayer().getName() + " moved too quickly!");
}
}
if (!isValidMovement(vehicle, oldLocation, newLocation)) {
vehicle.teleport(oldLocation);
return;
}
// call move event if movement actually occurred and there are handlers registered
if (!oldLocation.equals(newLocation) && VehicleMoveEvent.getHandlerList().getRegisteredListeners().length > 0) {
EventFactory.getInstance().callEvent(new VehicleMoveEvent((Vehicle) vehicle, oldLocation, newLocation.clone()));
}
// simply update location
vehicle.setRawLocation(newLocation);
player.setRawLocation(vehicle.getMountLocation());
// track movement stats
Vector delta = newLocation.clone().subtract(oldLocation).toVector();
delta.setX(Math.abs(delta.getX()));
delta.setY(Math.abs(delta.getY()));
delta.setZ(Math.abs(delta.getZ()));
int flatDistance = (int) Math.round(Math.hypot(delta.getX(), delta.getZ()) * 100.0);
if (vehicle instanceof Boat) {
player.incrementStatistic(Statistic.BOAT_ONE_CM, flatDistance);
} else if (vehicle instanceof Minecart) {
player.incrementStatistic(Statistic.MINECART_ONE_CM, flatDistance);
}
}
use of net.glowstone.entity.GlowEntity in project Glowstone by GlowstoneMC.
the class EntityStore method savePassengers.
private void savePassengers(GlowEntity vehicle, CompoundTag tag) {
List<CompoundTag> passengers = new ArrayList<>();
for (Entity passenger : vehicle.getPassengers()) {
if (!(passenger instanceof GlowEntity)) {
continue;
}
GlowEntity glowEntity = (GlowEntity) passenger;
if (!glowEntity.shouldSave()) {
continue;
}
try {
CompoundTag compound = new CompoundTag();
EntityStorage.save(glowEntity, compound);
passengers.add(compound);
savePassengers(glowEntity, compound);
} catch (Exception e) {
ConsoleMessages.Warn.Entity.SAVE_FAILED_PASSENGER.log(passenger, e);
}
}
if (!passengers.isEmpty()) {
tag.putCompoundList("Passengers", passengers);
}
}
use of net.glowstone.entity.GlowEntity in project Glowstone by GlowstoneMC.
the class GlowWorld method pulse.
/**
* Updates all the entities within this world.
*/
public void pulse() {
List<GlowEntity> allEntities = new ArrayList<>(entityManager.getAll());
List<GlowPlayer> players = new LinkedList<>();
activeChunksSet.clear();
// We should pulse our tickmap, so blocks get updated.
pulseTickMap();
// their position is modified by session ticking.
for (GlowEntity entity : entityManager) {
if (entity instanceof GlowPlayer) {
players.add((GlowPlayer) entity);
updateActiveChunkCollection(entity);
} else {
entity.pulse();
}
}
updateBlocksInActiveChunks();
// why update blocks before Players or Entities? if there is a specific reason we should
// document it here.
pulsePlayers(players);
chunkManager.clearChunkBlockChanges();
resetEntities(allEntities);
worldBorder.pulse();
updateWorldTime();
informPlayersOfTime();
updateOverworldWeather();
handleSleepAndWake(players);
saveWorld();
}
use of net.glowstone.entity.GlowEntity in project Glowstone by GlowstoneMC.
the class GlowWorld method spawnGlowEntity.
/**
* Spawns an entity.
*
* @param location the {@link Location} to spawn the entity at
* @param clazz the class of the {@link Entity} to spawn
* @param reason the reason for the spawning of the entity
* @return an instance of the spawned {@link Entity}
* @throws IllegalArgumentException TODO: document the reason this can happen
*/
public <T extends GlowEntity, E extends Entity> GlowEntity spawnGlowEntity(Location location, Class<T> clazz, SpawnReason reason, @Nullable Consumer<E> function) throws IllegalArgumentException {
checkNotNull(location);
checkNotNull(clazz);
GlowEntity entity = null;
try {
if (EntityRegistry.getEntity(clazz) != null) {
entity = EntityStorage.create(clazz, location);
}
if (entity != null && function != null) {
function.accept((E) entity);
}
EntitySpawnEvent spawnEvent = null;
if (entity instanceof LivingEntity) {
spawnEvent = EventFactory.getInstance().callEvent(new CreatureSpawnEvent((LivingEntity) entity, reason));
} else if (!(entity instanceof Item)) {
// ItemSpawnEvent is called elsewhere
spawnEvent = EventFactory.getInstance().callEvent(new EntitySpawnEvent(entity));
}
if (spawnEvent != null && spawnEvent.isCancelled()) {
// TODO: separate spawning and construction for better event cancellation
entity.remove();
} else {
List<Message> spawnMessage = entity.createSpawnMessage();
final GlowEntity finalEntity = entity;
getRawPlayers().stream().filter(player -> player.canSeeEntity(finalEntity)).forEach(player -> player.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
}
} catch (NoSuchMethodError | IllegalAccessError e) {
GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e);
} catch (Throwable t) {
GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", t);
}
if (entity != null) {
return entity;
}
throw new UnsupportedOperationException("Not supported yet.");
}
Aggregations