Search in sources :

Example 6 with Dye

use of org.bukkit.material.Dye in project Glowstone by GlowstoneMC.

the class GlowWolf method entityInteract.

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
    GlowServer.logger.info(String.valueOf(message));
    boolean result = super.entityInteract(player, message);
    if (message.getAction() == InteractEntityMessage.Action.INTERACT.ordinal()) {
        if (result) {
            return false;
        }
        ItemStack hand = InventoryUtil.itemOrEmpty(player.getInventory().getItem(message.getHandSlot()));
        if (!isTamed() && hand.getType() == Material.BONE) {
            // One in 3 chances of taming
            if (ThreadLocalRandom.current().nextInt(3) == 0 && fireEntityTameEvent(player)) {
                setCollarColor(DyeColor.RED);
                setTamed(true);
                setOwner(player);
                world.spawnParticle(Particle.HEART, location, 1);
                // Replenish health
                setHealth(getMaxHealth());
                setSitting(true);
            }
            player.getInventory().consumeItemInHand(message.getHandSlot());
            return true;
        }
        if (isTamed() && Objects.equals(getOwnerUniqueId(), player.getUniqueId())) {
            if (hand.getType() == Material.INK_SAC) {
                Dye dye = (Dye) hand.getData();
                DyeColor color = dye.getColor();
                setCollarColor(color);
                player.getInventory().consumeItemInHand(message.getHandSlot());
                return true;
            }
            setSitting(!isSitting());
            return true;
        }
    }
    return false;
}
Also used : Dye(org.bukkit.material.Dye) ItemStack(org.bukkit.inventory.ItemStack) DyeColor(org.bukkit.DyeColor)

Example 7 with Dye

use of org.bukkit.material.Dye in project Glowstone by GlowstoneMC.

the class GlowSheep method entityInteract.

@Override
public boolean entityInteract(GlowPlayer player, InteractEntityMessage message) {
    super.entityInteract(player, message);
    if (message.getAction() == InteractEntityMessage.Action.INTERACT.ordinal()) {
        // if the sheep is not adult or the player is spectator abort
        if (!isAdult() || player.getGameMode().equals(GameMode.SPECTATOR)) {
            return false;
        }
        final ItemStack handItem = InventoryUtil.itemOrEmpty(player.getInventory().getItem(message.getHandSlot()));
        if (InventoryUtil.isEmpty(handItem)) {
            return false;
        }
        switch(handItem.getType()) {
            case SHEARS:
                // If already sheared return
                if (isSheared()) {
                    return false;
                }
                // noinspection deprecation
                PlayerShearEntityEvent shearEvent = new PlayerShearEntityEvent(player, this);
                shearEvent = EventFactory.getInstance().callEvent(shearEvent);
                if (shearEvent.isCancelled()) {
                    return false;
                }
                if (!player.getGameMode().equals(GameMode.CREATIVE)) {
                    if (handItem.getDurability() < 238) {
                        handItem.setDurability((short) (handItem.getDurability() + 1));
                        player.getInventory().setItem(message.getHandSlot(), handItem);
                    } else {
                        player.getInventory().setItem(message.getHandSlot(), InventoryUtil.createEmptyStack());
                    }
                }
                final GlowWorld world = getWorld();
                world.playSound(getLocation(), Sound.ENTITY_SHEEP_SHEAR, 1, 1);
                final Material mat = WoolUtil.getWoolMaterialByDye(getColor());
                world.dropItemNaturally(getLocation(), new ItemStack(mat, ThreadLocalRandom.current().nextInt(3) + 1));
                setSheared(true);
                return true;
            case INK_SAC:
                {
                    final Dye dye = (Dye) handItem.getData();
                    DyeColor color = dye.getColor();
                    SheepDyeWoolEvent dyeEvent = new SheepDyeWoolEvent(this, color);
                    dyeEvent = EventFactory.getInstance().callEvent(dyeEvent);
                    if (dyeEvent.isCancelled()) {
                        metadata.set(MetadataIndex.SHEEP_DATA, getColorByte(), true);
                        player.updateInventory();
                        return false;
                    }
                    color = dyeEvent.getColor();
                    // If same color, we're done
                    if (color.equals(getColor())) {
                        return false;
                    }
                    if (!player.getGameMode().equals(GameMode.CREATIVE)) {
                        player.getInventory().consumeItemInHand(message.getHandSlot());
                    }
                    setColor(color);
                    return true;
                }
            default:
                return false;
        }
    }
    return false;
}
Also used : Dye(org.bukkit.material.Dye) SheepDyeWoolEvent(org.bukkit.event.entity.SheepDyeWoolEvent) PlayerShearEntityEvent(org.bukkit.event.player.PlayerShearEntityEvent) GlowWorld(net.glowstone.GlowWorld) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) DyeColor(org.bukkit.DyeColor)

Example 8 with Dye

use of org.bukkit.material.Dye in project modules-extra by CubeEngine.

the class ListenerPlayerBlockInteract method onPlayerInteract.

@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) {
    // TODO put item into itemframe
    if (event.getAction() == RIGHT_CLICK_BLOCK) {
        ItemStack itemInHand = event.getPlayer().getItemInHand();
        Location location = event.getClickedBlock().getLocation();
        BlockState state = event.getClickedBlock().getState();
        ActionPlayerBlock action;
        BlockState newState = state.getBlock().getState();
        if (state instanceof InventoryHolder) {
            action = this.newAction(UseContainer.class, state.getWorld());
        } else if (state.getData() instanceof Openable) {
            action = this.newAction(UseDoor.class, state.getWorld());
            if (action != null) {
                state = adjustBlockForDoubleBlocks(state);
                Openable openable = (Openable) state.getBlock().getState().getData();
                openable.setOpen(!openable.isOpen());
                newState.setData((MaterialData) openable);
            }
        } else if (state.getData() instanceof Lever) {
            action = this.newAction(UseLever.class, state.getWorld());
            if (action != null) {
                Lever leverData = (Lever) state.getBlock().getState().getData();
                leverData.setPowered(!leverData.isPowered());
                newState.setData(leverData);
            }
        } else if (state.getType() == REDSTONE_COMPARATOR_ON || state.getType() == REDSTONE_COMPARATOR_OFF) {
            action = this.newAction(UseComparator.class, state.getWorld());
            if (action != null) {
                newState.setType(state.getType() == REDSTONE_COMPARATOR_ON ? REDSTONE_COMPARATOR_OFF : REDSTONE_COMPARATOR_ON);
            }
        } else if (state.getData() instanceof Button) {
            action = this.newAction(UseButton.class, state.getWorld());
            if (action != null) {
                Button button = (Button) state.getBlock().getState().getData();
                button.setPowered(true);
                newState.setData(button);
            }
        } else if (state.getData() instanceof Rails) {
            if (itemInHand.getType() == MINECART || itemInHand.getType() == STORAGE_MINECART || itemInHand.getType() == POWERED_MINECART || itemInHand.getType() == HOPPER_MINECART || // BOAT is done down below
            itemInHand.getType() == EXPLOSIVE_MINECART) {
                VehiclePrePlaceEvent vEvent = new VehiclePrePlaceEvent(event.getClickedBlock().getRelative(UP).getLocation(), event.getPlayer());
                this.module.getCore().getEventManager().fireEvent(vEvent);
            }
            action = null;
        } else if (itemInHand.getType().equals(BOAT)) {
            VehiclePrePlaceEvent vEvent = new VehiclePrePlaceEvent(event.getClickedBlock().getRelative(UP).getLocation(), event.getPlayer());
            this.module.getCore().getEventManager().fireEvent(vEvent);
            action = null;
        } else if (state.getData() instanceof Cake) {
            action = null;
            if (event.getPlayer().getFoodLevel() < 20 && !(event.getPlayer().getGameMode() == CREATIVE)) {
                action = this.newAction(UseCake.class, state.getWorld());
                if (action != null) {
                    Cake cake = (Cake) state.getBlock().getState().getData();
                    cake.setSlicesEaten(cake.getSlicesEaten() + 1);
                    if (cake.getSlicesRemaining() == 0) {
                        action.setNewBlock(AIR);
                        newState = null;
                    } else {
                        newState.setData(cake);
                    }
                }
            }
        } else if (state instanceof NoteBlock) {
            action = this.newAction(UseNoteblock.class, state.getWorld());
            if (action != null) {
                try {
                    ((NoteBlock) newState).setNote(((NoteBlock) newState).getNote().sharped());
                } catch (IllegalArgumentException e) {
                    ((NoteBlock) newState).setNote(new Note(1, Tone.F, true));
                }
            }
        } else if (state.getData() instanceof Diode) {
            action = this.newAction(UseRepeater.class, state.getWorld());
            if (action != null) {
                Diode diode = (Diode) state.getBlock().getState().getData();
                Integer delay = diode.getDelay() + 1;
                if (delay == 5) {
                    delay = 1;
                }
                diode.setDelay(delay);
                newState.setData(diode);
            }
        } else if (state.getType() == TNT) {
            action = null;
            if (itemInHand.getType() == FLINT_AND_STEEL) {
                action = this.newAction(UseTnt.class, state.getWorld());
                if (action != null) {
                    action.setNewBlock(AIR);
                    newState = null;
                }
            }
        } else if (state.getData() instanceof Crops || state.getType() == GRASS) {
            if (itemInHand.getData() instanceof Dye && ((Dye) itemInHand.getData()).getColor() == WHITE) {
                // TODO THIS IS BULLSHIT I need an event for bonemealUse...
                action = this.newAction(UseBonemeal.class, state.getWorld());
                if (action != null) {
                    // TODO adjust growth stage
                    // TODO do not log if fully grown
                    action.setNewBlock(state);
                    action.setLocation(state.getLocation());
                    action.setOldBlock(state);
                    action.setPlayer(event.getPlayer());
                    this.logAction(action);
                }
            }
            return;
        } else if (state instanceof Jukebox) {
            // TODO
            action = null;
        } else {
            action = null;
        }
        if (action != null) {
            action.setLocation(state.getLocation());
            action.setOldBlock(state);
            if (newState != null) {
                action.setNewBlock(newState);
            }
            action.setPlayer(event.getPlayer());
            this.logAction(action);
        }
    } else if (event.getAction() == PHYSICAL) {
        Block block = event.getClickedBlock();
        if (block.getType() == SOIL) {
            BlockTrample action = this.newAction(BlockTrample.class, block.getWorld());
            if (action != null) {
                BlockState stateUp = block.getRelative(UP).getState();
                if (stateUp.getData() instanceof Crops) {
                    BlockTrample actionUp = this.newAction(BlockTrample.class, block.getWorld());
                    actionUp.setLocation(stateUp.getLocation());
                    actionUp.setOldBlock(stateUp);
                    actionUp.setNewBlock(AIR);
                    actionUp.setPlayer(event.getPlayer());
                    this.logAction(actionUp);
                }
                action.setLocation(block.getLocation());
                action.setOldBlock(block.getState());
                action.setNewBlock(DIRT);
                action.setPlayer(event.getPlayer());
                this.logAction(action);
            }
        } else if (block.getState().getData() instanceof PressurePlate) {
            UsePlate action = this.newAction(UsePlate.class, block.getWorld());
            if (action != null) {
                // BlockState newState = block.getState();
                // PressurePlate plate = (PressurePlate)newState.getData();
                action.setLocation(block.getLocation());
                action.setOldBlock(block.getState());
                action.setNewBlock(block.getState());
                action.setPlayer(event.getPlayer());
                this.logAction(action);
            }
        }
    }
}
Also used : Diode(org.bukkit.material.Diode) Cake(org.bukkit.material.Cake) Openable(org.bukkit.material.Openable) Dye(org.bukkit.material.Dye) Button(org.bukkit.material.Button) PressurePlate(org.bukkit.material.PressurePlate) InventoryHolder(org.bukkit.inventory.InventoryHolder) Jukebox(org.bukkit.block.Jukebox) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) NoteBlock(org.bukkit.block.NoteBlock) Crops(org.bukkit.material.Crops) Rails(org.bukkit.material.Rails) BlockState(org.bukkit.block.BlockState) Lever(org.bukkit.material.Lever) Note(org.bukkit.Note) VehiclePrePlaceEvent(org.cubeengine.module.log.action.vehicle.VehiclePrePlaceEvent) Block(org.bukkit.block.Block) NoteBlock(org.bukkit.block.NoteBlock) ActionPlayerBlock(org.cubeengine.module.log.action.block.player.ActionPlayerBlock) MaterialData(org.bukkit.material.MaterialData) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Location(org.spongepowered.api.world.Location) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Dye (org.bukkit.material.Dye)8 ItemStack (org.bukkit.inventory.ItemStack)6 DyeColor (org.bukkit.DyeColor)4 ArrayList (java.util.ArrayList)2 Color (org.bukkit.Color)2 FireworkEffect (org.bukkit.FireworkEffect)2 Sheep (org.bukkit.entity.Sheep)2 EventHandler (org.bukkit.event.EventHandler)2 FireworkEffectMeta (org.bukkit.inventory.meta.FireworkEffectMeta)2 SheepTrait (net.citizensnpcs.trait.SheepTrait)1 GlowWorld (net.glowstone.GlowWorld)1 Type (org.bukkit.FireworkEffect.Type)1 Material (org.bukkit.Material)1 Note (org.bukkit.Note)1 Block (org.bukkit.block.Block)1 BlockState (org.bukkit.block.BlockState)1 Jukebox (org.bukkit.block.Jukebox)1 NoteBlock (org.bukkit.block.NoteBlock)1 Pattern (org.bukkit.block.banner.Pattern)1 LivingEntity (org.bukkit.entity.LivingEntity)1