|
| 1 | +package fr.maxlego08.essentials.commands.commands.utils; |
| 2 | + |
| 3 | +import fr.maxlego08.essentials.api.EssentialsPlugin; |
| 4 | +import fr.maxlego08.essentials.api.commands.CommandResultType; |
| 5 | +import fr.maxlego08.essentials.api.commands.Permission; |
| 6 | +import fr.maxlego08.essentials.api.messages.Message; |
| 7 | +import fr.maxlego08.essentials.zutils.utils.commands.VCommand; |
| 8 | +import org.bukkit.FluidCollisionMode; |
| 9 | +import org.bukkit.Location; |
| 10 | +import org.bukkit.entity.Entity; |
| 11 | +import org.bukkit.entity.ItemFrame; |
| 12 | +import org.bukkit.util.RayTraceResult; |
| 13 | +import org.bukkit.util.Vector; |
| 14 | + |
| 15 | +public class CommandItemFrame extends VCommand { |
| 16 | + |
| 17 | + public CommandItemFrame(EssentialsPlugin plugin) { |
| 18 | + super(plugin); |
| 19 | + this.setPermission(Permission.ESSENTIALS_ITEMFRAME); |
| 20 | + this.setDescription(Message.DESCRIPTION_ITEMFRAME); |
| 21 | + this.onlyPlayers(); |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + protected CommandResultType perform(EssentialsPlugin plugin) { |
| 26 | + Location eyeLocation = player.getEyeLocation(); |
| 27 | + Vector direction = eyeLocation.getDirection(); |
| 28 | + |
| 29 | + RayTraceResult result = player.getWorld().rayTraceEntities( |
| 30 | + eyeLocation, |
| 31 | + direction, |
| 32 | + 5.0, |
| 33 | + entity -> entity instanceof ItemFrame |
| 34 | + ); |
| 35 | + |
| 36 | + if (result == null || result.getHitEntity() == null) { |
| 37 | + message(sender, Message.COMMAND_ITEMFRAME_NOT_FOUND); |
| 38 | + return CommandResultType.DEFAULT; |
| 39 | + } |
| 40 | + |
| 41 | + Entity entity = result.getHitEntity(); |
| 42 | + if (entity instanceof ItemFrame itemFrame) { |
| 43 | + boolean newVisibility = !itemFrame.isVisible(); |
| 44 | + itemFrame.setVisible(newVisibility); |
| 45 | + |
| 46 | + if (newVisibility) { |
| 47 | + message(sender, Message.COMMAND_ITEMFRAME_VISIBLE); |
| 48 | + } else { |
| 49 | + message(sender, Message.COMMAND_ITEMFRAME_INVISIBLE); |
| 50 | + } |
| 51 | + return CommandResultType.SUCCESS; |
| 52 | + } |
| 53 | + |
| 54 | + message(sender, Message.COMMAND_ITEMFRAME_NOT_FOUND); |
| 55 | + return CommandResultType.DEFAULT; |
| 56 | + } |
| 57 | +} |
0 commit comments