use of net.glowstone.block.itemtype.ItemType in project Dragonet-Legacy by DragonetMC.
the class TestItemTypes method hasAllMaterials.
@Test
public void hasAllMaterials() {
ItemType type = table.getItem(material);
// special cases
if (material == Material.AIR) {
assertNull("ItemType exists for air: " + type, type);
return;
}
// check that it exists
assertNotNull("ItemType does not exist for " + material, type);
// check that its block status is correct
assertTrue("Block status mismatch between " + material + "(" + material.isBlock() + ") and " + type, (type instanceof BlockType) == material.isBlock());
// check that material returned matches
assertEquals("ItemType returned wrong material", material, type.getMaterial());
// check that max stack size matches
assertEquals("Maximum stack size was incorrect", material.getMaxStackSize(), type.getMaxStackSize());
}
use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.
the class FlintAndSteelDispenseBehavior method dispenseStack.
@Override
protected ItemStack dispenseStack(GlowBlock block, ItemStack stack) {
GlowBlock target = block.getRelative(BlockDispenser.getFacing(block));
successful = true;
if (target.getType() == Material.AIR) {
target.setType(Material.FIRE);
stack.setDurability((short) (stack.getDurability() + 1));
ItemType type = ItemTable.instance().getItem(stack.getType());
if (!(type instanceof ItemTool)) {
return stack;
}
ItemTool toolType = (ItemTool) type;
if (stack.getDurability() > toolType.getMaxDurability()) {
stack.setAmount(0);
}
} else if (target.getType() == Material.TNT) {
BlockTNT.igniteBlock(target, false);
} else {
successful = false;
}
return stack.getAmount() > 0 ? stack : null;
}
use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.
the class GlowPlayer method pulse.
@Override
public void pulse() {
super.pulse();
if (usageItem != null) {
if (usageItem == getItemInHand()) {
if (--usageTime == 0) {
ItemType item = ItemTable.instance().getItem(usageItem.getType());
if (item instanceof ItemFood) {
((ItemFood) item).eat(this, usageItem);
}
}
} else {
usageItem = null;
usageTime = 0;
}
}
if (digging != null) {
pulseDigging();
}
if (exhaustion > 4.0f) {
exhaustion -= 4.0f;
if (saturation > 0f) {
saturation = Math.max(saturation - 1f, 0f);
sendHealth();
} else if (world.getDifficulty() != Difficulty.PEACEFUL) {
FoodLevelChangeEvent event = EventFactory.callEvent(new FoodLevelChangeEvent(this, Math.max(food - 1, 0)));
if (!event.isCancelled()) {
food = event.getFoodLevel();
}
sendHealth();
}
}
if (getHealth() < getMaxHealth() && !isDead()) {
if (food > 18 && ticksLived % 80 == 0 || world.getDifficulty() == Difficulty.PEACEFUL) {
EntityRegainHealthEvent event1 = new EntityRegainHealthEvent(this, 1f, RegainReason.SATIATED);
EventFactory.callEvent(event1);
if (!event1.isCancelled()) {
setHealth(getHealth() + 1);
}
exhaustion = Math.min(exhaustion + 3.0f, 40.0f);
saturation -= 3;
}
}
if (food == 0 && getHealth() > 1 && ticksLived % 80 == 0) {
damage(1, DamageCause.STARVATION);
}
// stream world
streamBlocks();
processBlockChanges();
// add to playtime
incrementStatistic(Statistic.PLAY_ONE_TICK);
if (isSneaking()) {
incrementStatistic(Statistic.SNEAK_TIME);
}
// update inventory
for (InventoryMonitor.Entry entry : invMonitor.getChanges()) {
sendItemChange(entry.slot, entry.item);
}
// send changed metadata
List<MetadataMap.Entry> changes = metadata.getChanges();
if (!changes.isEmpty()) {
session.send(new EntityMetadataMessage(SELF_ID, changes));
}
// update or remove entities
List<Integer> destroyIds = new LinkedList<>();
for (Iterator<GlowEntity> it = knownEntities.iterator(); it.hasNext(); ) {
GlowEntity entity = it.next();
if (!isWithinDistance(entity) || entity.isRemoved()) {
destroyIds.add(entity.getEntityId());
it.remove();
} else {
entity.createUpdateMessage().forEach(session::send);
}
}
if (!destroyIds.isEmpty()) {
session.send(new DestroyEntitiesMessage(destroyIds));
}
// add entities
knownChunks.parallelStream().forEach(key -> {
GlowChunk chunk = world.getChunkAt(key.getX(), key.getZ());
chunk.getRawEntities().stream().filter(entity -> this != entity).filter(this::isWithinDistance).filter(entity -> !entity.isDead()).filter(entity -> !knownEntities.contains(entity)).filter(entity -> !hiddenEntities.contains(entity.getUniqueId())).forEach((entity) -> {
knownEntities.add(entity);
entity.createSpawnMessage().forEach(session::send);
});
});
if (passengerChanged) {
session.send(new SetPassengerMessage(SELF_ID, getPassengers().stream().mapToInt(Entity::getEntityId).toArray()));
}
getAttributeManager().sendMessages(session);
}
use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.
the class BlockPlacementHandler method handle.
@Override
public void handle(GlowSession session, BlockPlacementMessage message) {
//TODO: Hand handling instead of .getHeldItem()
GlowPlayer player = session.getPlayer();
if (player == null)
return;
//GlowServer.logger.info(session + ": " + message);
/*
* The client sends this packet for the following cases:
* Right click air:
* - Send direction=-1 packet for any non-null item
* Right click block:
* - Send packet with all values filled
* - If client DOES NOT expect a block placement to result:
* - Send direction=-1 packet (unless item is null)
*
* Client will expect a block placement to result from blocks and from
* certain items (e.g. sugarcane, sign). We *could* opt to trust the
* client on this, but the server's view of events (particularly under
* the Bukkit API, or custom ItemTypes) may differ from the client's.
*
* In order to avoid firing two events for one interact, the two
* packet case must be handled here. Care must also be taken that a
* right-click air of an expected-place item immediately after is
* not considered part of the same action.
*/
Action action = Action.RIGHT_CLICK_BLOCK;
GlowBlock clicked = player.getWorld().getBlockAt(message.getX(), message.getY(), message.getZ());
/*
* Check if the message is a -1. If we *just* got a message with the
* values filled, discard it, otherwise perform right-click-air.
*/
if (message.getDirection() == -1) {
BlockPlacementMessage previous = session.getPreviousPlacement();
// session.setPreviousPlacement(null);
return;
// }
}
// Set previous placement message
session.setPreviousPlacement(message);
// Get values from the message
Vector clickedLoc = new Vector(message.getCursorX(), message.getCursorY(), message.getCursorZ());
BlockFace face = convertFace(message.getDirection());
ItemStack holding = player.getItemInHand();
boolean rightClickedAir = false;
// check that a block-click wasn't against air
if (clicked == null || clicked.getType() == Material.AIR) {
action = Action.RIGHT_CLICK_AIR;
// inform the player their perception of reality is wrong
if (holding.getType().isBlock()) {
player.sendBlockChange(clicked.getLocation(), Material.AIR, (byte) 0);
return;
} else {
rightClickedAir = true;
}
}
// call interact event
PlayerInteractEvent event = EventFactory.onPlayerInteract(player, action, rightClickedAir ? null : clicked, face);
//GlowServer.logger.info("Interact: " + action + " " + clicked + " " + face);
// attempt to use interacted block
// DEFAULT is treated as ALLOW, and sneaking is always considered
boolean useInteractedBlock = event.useInteractedBlock() != Result.DENY;
if (useInteractedBlock && !rightClickedAir && (!player.isSneaking() || InventoryUtil.isEmpty(holding))) {
BlockType blockType = ItemTable.instance().getBlock(clicked.getType());
if (blockType != null) {
useInteractedBlock = blockType.blockInteract(player, clicked, face, clickedLoc);
} else {
GlowServer.logger.info("Unknown clicked block, " + clicked.getType());
}
} else {
useInteractedBlock = false;
}
// follows ALLOW/DENY: default to if no block was interacted with
if (selectResult(event.useItemInHand(), !useInteractedBlock) && holding != null) {
ItemType type = ItemTable.instance().getItem(holding.getType());
if (!rightClickedAir && holding.getType() != Material.AIR && !type.canOnlyUseSelf()) {
type.rightClickBlock(player, clicked, face, holding, clickedLoc);
}
}
// in case something is unimplemented or otherwise screwy on our side
if (!rightClickedAir) {
revert(player, clicked);
revert(player, clicked.getRelative(face));
}
// if there's been a change in the held item, make it valid again
if (!InventoryUtil.isEmpty(holding)) {
if (holding.getType().getMaxDurability() > 0 && holding.getDurability() > holding.getType().getMaxDurability()) {
holding.setAmount(holding.getAmount() - 1);
holding.setDurability((short) 0);
}
if (holding.getAmount() <= 0) {
holding = null;
}
}
player.setItemInHand(holding);
}
use of net.glowstone.block.itemtype.ItemType in project Glowstone by GlowstoneMC.
the class DiggingHandler method handle.
@Override
public void handle(GlowSession session, DiggingMessage message) {
//Todo: Implement SHOOT_ARROW_FINISH_EATING
//Todo: Implement SWAP_ITEM_IN_HAND
GlowPlayer player = session.getPlayer();
GlowWorld world = player.getWorld();
GlowBlock block = world.getBlockAt(message.getX(), message.getY(), message.getZ());
BlockFace face = BlockPlacementHandler.convertFace(message.getFace());
ItemStack holding = player.getItemInHand();
if (block.getRelative(face).getType() == Material.FIRE) {
block.getRelative(face).breakNaturally();
// returns to avoid breaking block in creative
return;
}
boolean blockBroken = false;
boolean revert = false;
if (message.getState() == DiggingMessage.START_DIGGING) {
// call interact event
Action action = Action.LEFT_CLICK_BLOCK;
Block eventBlock = block;
if (player.getLocation().distanceSquared(block.getLocation()) > 36 || block.getTypeId() == 0) {
action = Action.LEFT_CLICK_AIR;
eventBlock = null;
}
PlayerInteractEvent interactEvent = EventFactory.onPlayerInteract(player, action, eventBlock, face);
// attempt to use item in hand, that is, dig up the block
if (!BlockPlacementHandler.selectResult(interactEvent.useItemInHand(), true)) {
// the event was cancelled, get out of here
revert = true;
} else if (player.getGameMode() != GameMode.SPECTATOR) {
player.setDigging(null);
// emit damage event - cancel by default if holding a sword
boolean instaBreak = player.getGameMode() == GameMode.CREATIVE || block.getMaterialValues().getHardness() == 0;
BlockDamageEvent damageEvent = new BlockDamageEvent(player, block, player.getItemInHand(), instaBreak);
if (player.getGameMode() == GameMode.CREATIVE && holding != null && EnchantmentTarget.WEAPON.includes(holding.getType())) {
damageEvent.setCancelled(true);
}
EventFactory.callEvent(damageEvent);
// follow orders
if (damageEvent.isCancelled()) {
revert = true;
} else {
// in creative, break even if denied in the event, or the block
// can never be broken (client does not send DONE_DIGGING).
blockBroken = damageEvent.getInstaBreak();
if (!blockBroken) {
/// TODO: add a delay here based on hardness
player.setDigging(block);
}
}
}
} else if (message.getState() == DiggingMessage.CANCEL_DIGGING) {
player.setDigging(null);
} else if (message.getState() == DiggingMessage.FINISH_DIGGING) {
// shouldn't happen in creative mode
// todo: verification against malicious clients
blockBroken = block.equals(player.getDigging());
if (blockBroken && holding.getType().getMaxDurability() != 0 && holding.getType() != Material.AIR && holding.getDurability() != holding.getType().getMaxDurability()) {
switch(block.getType()) {
case GRASS:
case DIRT:
case SAND:
case GRAVEL:
case MYCEL:
case SOUL_SAND:
switch(holding.getType()) {
case WOOD_SPADE:
case STONE_SPADE:
case IRON_SPADE:
case GOLD_SPADE:
case DIAMOND_SPADE:
holding.setDurability((short) (holding.getDurability() + 1));
break;
default:
holding.setDurability((short) (holding.getDurability() + 2));
break;
}
break;
case LOG:
case LOG_2:
case WOOD:
case CHEST:
switch(holding.getType()) {
case WOOD_AXE:
case STONE_AXE:
case IRON_AXE:
case GOLD_AXE:
case DIAMOND_AXE:
holding.setDurability((short) (holding.getDurability() + 1));
break;
default:
holding.setDurability((short) (holding.getDurability() + 2));
break;
}
break;
case STONE:
case COBBLESTONE:
switch(holding.getType()) {
case WOOD_PICKAXE:
case STONE_PICKAXE:
case IRON_PICKAXE:
case GOLD_PICKAXE:
case DIAMOND_PICKAXE:
holding.setDurability((short) (holding.getDurability() + 1));
break;
default:
holding.setDurability((short) (holding.getDurability() + 2));
break;
}
break;
default:
holding.setDurability((short) (holding.getDurability() + 2));
break;
}
if (holding.getType().getMaxDurability() != 0 && holding.getDurability() >= holding.getType().getMaxDurability()) {
player.getItemInHand().setType(Material.AIR);
}
}
player.setDigging(null);
} else if (message.getState() == DiggingMessage.STATE_DROP_ITEM) {
player.dropItemInHand(false);
return;
} else if (message.getState() == DiggingMessage.STATE_DROP_ITEMSTACK) {
player.dropItemInHand(true);
return;
} else if (message.getState() == DiggingMessage.STATE_SHOT_ARROW_FINISH_EATING && player.getUsageItem() != null) {
if (Objects.equals(player.getUsageItem(), holding)) {
ItemType type = ItemTable.instance().getItem(player.getUsageItem().getType());
if (type != null && type instanceof ItemTimedUsage) {
((ItemTimedUsage) type).endUse(player, player.getUsageItem());
} else {
// todo: inform the player that this item cannot be consumed/used
}
} else {
// todo: verification against malicious clients
// todo: inform player their item is wrong
}
return;
} else if (message.getState() == DiggingMessage.SWAP_ITEM_IN_HAND) {
ItemStack main = player.getInventory().getItemInMainHand();
ItemStack off = player.getInventory().getItemInOffHand();
player.getInventory().setItemInOffHand(main);
player.getInventory().setItemInMainHand(off);
player.updateInventory();
return;
} else {
return;
}
if (blockBroken && !revert) {
// fire the block break event
BlockBreakEvent breakEvent = EventFactory.callEvent(new BlockBreakEvent(block, player));
if (breakEvent.isCancelled()) {
BlockPlacementHandler.revert(player, block);
return;
}
MaterialData data = block.getState().getData();
if (data instanceof DoublePlant) {
if (((DoublePlant) data).getSpecies() == DoublePlantSpecies.PLANT_APEX && block.getRelative(BlockFace.DOWN).getState().getData() instanceof DoublePlant) {
block = block.getRelative(BlockFace.DOWN);
}
}
BlockType blockType = ItemTable.instance().getBlock(block.getType());
if (blockType != null) {
blockType.blockDestroy(player, block, face);
}
// destroy the block
if (!block.isEmpty() && !block.isLiquid() && (player.getGameMode() != GameMode.CREATIVE || blockType instanceof BlockContainer) && world.getGameRuleMap().getBoolean("doTileDrops")) {
Collection<ItemStack> drops = blockType.getDrops(block, holding);
if (blockType instanceof BlockContainer && player.getGameMode() == GameMode.CREATIVE) {
drops = ((BlockContainer) blockType).getContentDrops(block);
}
for (ItemStack drop : drops) {
GlowItem item = world.dropItemNaturally(block.getLocation(), drop);
item.setPickupDelay(30);
item.setBias(player);
}
}
player.addExhaustion(0.005f);
// STEP_SOUND actually is the block break particles
world.playEffectExceptTo(block.getLocation(), Effect.STEP_SOUND, block.getTypeId(), 64, player);
GlowBlockState state = block.getState();
block.setType(Material.AIR);
if (blockType != null) {
blockType.afterDestroy(player, block, face, state);
}
} else if (revert) {
// replace the block that wasn't really dug
BlockPlacementHandler.revert(player, block);
} else if (block.getType() != Material.AIR) {
BlockType blockType = ItemTable.instance().getBlock(block.getType());
blockType.leftClickBlock(player, block, holding);
}
}
Aggregations