use of org.spongepowered.api.block.tileentity.Jukebox in project SpongeCommon by SpongePowered.
the class JukeboxDataProcessor method remove.
private boolean remove(BlockJukebox.TileEntityJukebox jukebox) {
((Jukebox) jukebox).ejectRecord();
IBlockState block = jukebox.getWorld().getBlockState(jukebox.getPos());
return block.getBlock() instanceof BlockJukebox && !block.getValue(BlockJukebox.HAS_RECORD);
}
use of org.spongepowered.api.block.tileentity.Jukebox in project SpongeCommon by SpongePowered.
the class JukeboxDataProcessor method set.
@Override
protected boolean set(BlockJukebox.TileEntityJukebox jukebox, ItemStackSnapshot stackSnapshot) {
IBlockState block = jukebox.getWorld().getBlockState(jukebox.getPos());
if (stackSnapshot == ItemStackSnapshot.NONE) {
if (jukebox.getRecord() == null) {
return true;
}
return remove(jukebox);
}
if (!(stackSnapshot.getType() instanceof ItemRecord)) {
return false;
}
((Jukebox) jukebox).insertRecord(stackSnapshot.createStack());
block = jukebox.getWorld().getBlockState(jukebox.getPos());
return block.getBlock() instanceof BlockJukebox && block.getValue(BlockJukebox.HAS_RECORD);
}
use of org.spongepowered.api.block.tileentity.Jukebox in project LanternServer by LanternPowered.
the class JukeboxInteractionBehavior method tryInteract.
@Override
public BehaviorResult tryInteract(BehaviorPipeline<Behavior> pipeline, BehaviorContext context) {
final Location<World> location = context.requireContext(ContextKeys.INTERACTION_LOCATION);
final Optional<TileEntity> optTile = location.getTileEntity();
if (optTile.isPresent()) {
final TileEntity tile = optTile.get();
if (tile instanceof Jukebox) {
final LanternJukebox jukebox = (LanternJukebox) tile;
final Optional<Entity> optEjectedItem = jukebox.ejectRecordItem();
boolean success = false;
if (optEjectedItem.isPresent()) {
final Entity entity = optEjectedItem.get();
entity.getWorld().spawnEntity(optEjectedItem.get());
// TODO: Include the entity in the behavior context
success = true;
}
final Optional<ItemStack> optItemStack = context.getContext(ContextKeys.USED_ITEM_STACK);
if (optItemStack.isPresent()) {
final ItemStack itemStack = optItemStack.get();
final RecordProperty property = itemStack.getProperty(RecordProperty.class).orElse(null);
final RecordType recordType = property == null ? null : property.getValue();
if (recordType != null) {
final ItemStackSnapshot oldSnapshot = itemStack.createSnapshot();
itemStack.setQuantity(itemStack.getQuantity() - 1);
final ItemStackSnapshot newSnapshot = itemStack.createSnapshot();
context.getContext(ContextKeys.PLAYER).ifPresent(player -> {
if (!player.get(Keys.GAME_MODE).orElse(GameModes.NOT_SET).equals(GameModes.CREATIVE)) {
context.getContext(ContextKeys.USED_SLOT).ifPresent(slot -> context.addSlotChange(new SlotTransaction(slot, oldSnapshot, newSnapshot)));
}
});
itemStack.setQuantity(1);
jukebox.insertRecord(itemStack);
jukebox.playRecord();
success = true;
}
}
if (success) {
return BehaviorResult.SUCCESS;
}
}
}
return BehaviorResult.PASS;
}
Aggregations