use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class CreativeItemHandler method handle.
@Override
public void handle(GlowSession session, CreativeItemMessage message) {
GlowPlayer player = session.getPlayer();
GlowInventory inv = player.getInventory();
// CraftBukkit does use a inventory view with both inventories set to the player's inventory
// for the creative inventory as there is no second inventory (no crafting) visible for the client
InventoryView view = player.getOpenInventory();
int viewSlot = message.getSlot();
int slot = view.convertSlot(viewSlot);
ItemStack stack = ItemIds.sanitize(message.getItem());
SlotType type = inv.getSlotType(slot);
// only if creative mode
if (player.getGameMode() != GameMode.CREATIVE) {
player.kickPlayer("Illegal creative mode item selection");
return;
}
// only if default (player) inventory
if (!GlowInventoryView.isDefault(player.getOpenInventory())) {
player.kickPlayer("Illegal creative mode item selection");
return;
}
// clicking outside drops the item
if (message.getSlot() < 0) {
InventoryCreativeEvent event = EventFactory.callEvent(new InventoryCreativeEvent(view, SlotType.OUTSIDE, -999, stack));
if (event.isCancelled()) {
session.send(new SetWindowSlotMessage(-1, -1, stack));
} else {
player.drop(event.getCursor());
}
return;
}
// this happens quiet often as the client tends to update the whole inventory at once
if (Objects.equals(stack, view.getItem(viewSlot))) {
return;
}
InventoryCreativeEvent event = EventFactory.callEvent(new InventoryCreativeEvent(view, type, viewSlot, stack));
if (event.isCancelled()) {
// send original slot to player to prevent async inventories
player.sendItemChange(viewSlot, view.getItem(viewSlot));
// don't keep track of player's current item, just give them back what they tried to place
session.send(new SetWindowSlotMessage(-1, -1, stack));
return;
}
view.setItem(viewSlot, stack);
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class InteractEntityHandler method handle.
@Override
public void handle(GlowSession session, InteractEntityMessage message) {
GlowPlayer player = session.getPlayer();
// You can't do anything when you're dead
if (player.isDead()) {
GlowServer.logger.info("Player " + player.getName() + " tried to interact with an entity while dead");
return;
}
GlowEntity possibleTarget = player.getWorld().getEntityManager().getEntity(message.getId());
GlowLivingEntity target = possibleTarget instanceof GlowLivingEntity ? (GlowLivingEntity) possibleTarget : null;
if (message.getAction() == Action.ATTACK.ordinal()) {
if (target == null) {
if (possibleTarget != null) {
possibleTarget.entityInteract(player, message);
} else {
GlowServer.logger.info("Player " + player.getName() + " tried to attack an entity that does not exist");
}
} else if (!target.isDead() && target.canTakeDamage(DamageCause.ENTITY_ATTACK)) {
// Calculate damage amount
ItemStack hand = player.getItemInHand();
Material type = hand == null ? Material.AIR : hand.getType();
// todo: Actual critical hit check
float damage = AttackDamage.getMeleeDamage(type, false);
// Set entity on fire if the item has Fire Aspect
if (hand.getEnchantments().containsKey(Enchantment.FIRE_ASPECT)) {
target.setFireTicks(target.getFireTicks() + hand.getEnchantments().get(Enchantment.FIRE_ASPECT) * 80);
}
// Apply damage. Calls the EntityDamageByEntityEvent
target.damage(damage, player, DamageCause.ENTITY_ATTACK);
player.incrementStatistic(Statistic.DAMAGE_DEALT, Math.round(damage));
player.addExhaustion(0.1f);
if (target.isDead()) {
player.incrementStatistic(target.getType() == EntityType.PLAYER ? Statistic.PLAYER_KILLS : Statistic.MOB_KILLS);
}
// Apply durability loss (if applicable)
short durabilityLoss = AttackDamage.getMeleeDurabilityLoss(type);
if (durabilityLoss > 0 && !InventoryUtil.isEmpty(hand) && player.getGameMode() != GameMode.CREATIVE) {
// Yes, this actually subtracts
hand.setDurability((short) (hand.getDurability() + durabilityLoss));
}
}
} else if (message.getAction() == Action.INTERACT_AT.ordinal()) {
//todo: Handle hand variable
// todo: Interaction with entity at a specified location (X, Y, and Z are present in the message)
// used for adjusting specific portions of armor stands
} else if (message.getAction() == Action.INTERACT.ordinal()) {
//Todo: Handle hand variable
PlayerInteractEntityEvent event = new PlayerInteractEntityEvent(player, possibleTarget);
EventFactory.callEvent(event);
if (!event.isCancelled()) {
possibleTarget.entityInteract(player, message);
}
} else {
GlowServer.logger.info("Player " + player.getName() + " sent unknown interact action: " + message.getAction());
}
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class QueryTest method testFullStats.
@Test
public void testFullStats() throws Exception {
World world = mock(World.class);
when(world.getName()).thenReturn("world");
GlowPlayer p1 = mock(GlowPlayer.class);
GlowPlayer p2 = mock(GlowPlayer.class);
when(p1.getName()).thenReturn("barneygale");
when(p2.getName()).thenReturn("Vivalahelvig");
PluginManager pluginManager = mock(PluginManager.class);
when(glowServer.getMotd()).thenReturn("A Minecraft Server");
Mockito.doReturn(Arrays.asList(p1, p2)).when(glowServer).getOnlinePlayers();
when(glowServer.getMaxPlayers()).thenReturn(20);
when(glowServer.getPort()).thenReturn(25565);
when(glowServer.getWorlds()).thenReturn(Arrays.asList(world));
when(glowServer.getIp()).thenReturn("");
when(glowServer.getVersion()).thenReturn("123");
when(glowServer.getBukkitVersion()).thenReturn("xyz");
when(glowServer.getPluginManager()).thenReturn(pluginManager);
when(pluginManager.getPlugins()).thenReturn(new Plugin[0]);
QueryHandler handler = new QueryHandler(server, queryPlugins);
when(random.nextInt()).thenReturn(9513307);
server.generateChallengeToken(address);
testChannelRead(handler, FULL_STATS_RECV, FULL_STATS_SEND);
}
use of net.glowstone.entity.GlowPlayer in project Glowstone by GlowstoneMC.
the class GlowSession method setPlayer.
/**
* Sets the player associated with this session.
*
* @param profile The player's profile with name and UUID information.
* @throws IllegalStateException if there is already a player associated
* with this session.
*/
public void setPlayer(PlayerProfile profile) {
if (player != null) {
throw new IllegalStateException("Cannot set player twice");
}
// isActive check here in case player disconnected during authentication
if (!isActive()) {
// no need to call onDisconnect() since it only does anything if there's a player set
return;
}
// initialize the player
PlayerReader reader = server.getPlayerDataService().beginReadingData(profile.getUniqueId());
player = new GlowPlayer(this, profile, reader);
finalizeLogin(profile);
// but before the GlowPlayer initialization was completed
if (!isActive()) {
reader.close();
onDisconnect();
return;
}
// Kick other players with the same UUID
for (GlowPlayer other : getServer().getRawOnlinePlayers()) {
if (other != player && other.getUniqueId().equals(player.getUniqueId())) {
other.getSession().disconnect("You logged in from another location.", true);
break;
}
}
// login event
PlayerLoginEvent event = EventFactory.onPlayerLogin(player, hostname);
if (event.getResult() != Result.ALLOWED) {
disconnect(event.getKickMessage(), true);
return;
}
//joins the player
player.join(this, reader);
player.getWorld().getRawPlayers().add(player);
online = true;
GlowServer.logger.info(player.getName() + " [" + address + "] connected, UUID: " + player.getUniqueId());
// message and user list
String message = EventFactory.onPlayerJoin(player).getJoinMessage();
if (message != null && !message.isEmpty()) {
server.broadcastMessage(message);
}
Message addMessage = new UserListItemMessage(Action.ADD_PLAYER, player.getUserListEntry());
List<Entry> entries = new ArrayList<>();
for (GlowPlayer other : server.getRawOnlinePlayers()) {
if (other != player && other.canSee(player)) {
other.getSession().send(addMessage);
}
if (player.canSee(other)) {
entries.add(other.getUserListEntry());
}
}
send(new UserListItemMessage(Action.ADD_PLAYER, entries));
}
Aggregations