|
| 1 | +package com.github.skriptdev.skript.plugin.elements.events.player; |
| 2 | + |
| 3 | +import com.github.skriptdev.skript.api.hytale.Block; |
| 4 | +import com.github.skriptdev.skript.api.skript.event.CancellableContext; |
| 5 | +import com.github.skriptdev.skript.api.skript.event.PlayerContext; |
| 6 | +import com.github.skriptdev.skript.api.skript.event.SystemEvent; |
| 7 | +import com.github.skriptdev.skript.api.skript.registration.SkriptRegistration; |
| 8 | +import com.hypixel.hytale.component.ArchetypeChunk; |
| 9 | +import com.hypixel.hytale.component.CommandBuffer; |
| 10 | +import com.hypixel.hytale.component.Ref; |
| 11 | +import com.hypixel.hytale.component.Store; |
| 12 | +import com.hypixel.hytale.component.query.Query; |
| 13 | +import com.hypixel.hytale.component.system.EntityEventSystem; |
| 14 | +import com.hypixel.hytale.protocol.InteractionType; |
| 15 | +import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType; |
| 16 | +import com.hypixel.hytale.server.core.entity.entities.Player; |
| 17 | +import com.hypixel.hytale.server.core.event.events.ecs.UseBlockEvent; |
| 18 | +import com.hypixel.hytale.server.core.universe.world.World; |
| 19 | +import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
| 20 | +import io.github.syst3ms.skriptparser.lang.Expression; |
| 21 | +import io.github.syst3ms.skriptparser.lang.TriggerContext; |
| 22 | +import io.github.syst3ms.skriptparser.lang.TriggerMap; |
| 23 | +import io.github.syst3ms.skriptparser.parsing.ParseContext; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | +import org.jetbrains.annotations.Nullable; |
| 26 | + |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +public class EvtPlayerPostUseBlock extends SystemEvent<EntityEventSystem<EntityStore, ? extends UseBlockEvent>> { |
| 30 | + |
| 31 | + public static void register(SkriptRegistration reg) { |
| 32 | + reg.newEvent(EvtPlayerPostUseBlock.class, |
| 33 | + "player use block", |
| 34 | + "pre player use block", |
| 35 | + "before player uses block", |
| 36 | + "post player use block", |
| 37 | + "after player uses block") |
| 38 | + .setHandledContexts(PreUseBlockContext.class, PostUseBlockContext.class) |
| 39 | + .name("Player Use Block") |
| 40 | + .description("Called when a player uses a block.", |
| 41 | + "Pre is cancellable, post is not.") |
| 42 | + .since("INSERT VERSION") |
| 43 | + .register(); |
| 44 | + |
| 45 | + reg.addContextValue(UseBlockContext.class, BlockType.class, true, "blocktype", UseBlockContext::getBlockType); |
| 46 | + reg.addContextValue(UseBlockContext.class, Block.class, true, "block", UseBlockContext::getBlock); |
| 47 | + reg.addContextValue(UseBlockContext.class, InteractionType.class, true, "interaction-type", UseBlockContext::getInteractionType); |
| 48 | + } |
| 49 | + |
| 50 | + private static PreUseBlockSystem PRE_SYSTEM; |
| 51 | + private static PostUseBlockSystem POST_SYSTEM; |
| 52 | + private boolean pre; |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) { |
| 56 | + if (PRE_SYSTEM == null) { |
| 57 | + PRE_SYSTEM = new PreUseBlockSystem(); |
| 58 | + applySystem(PRE_SYSTEM); |
| 59 | + } |
| 60 | + if (POST_SYSTEM == null) { |
| 61 | + POST_SYSTEM = new PostUseBlockSystem(); |
| 62 | + applySystem(POST_SYSTEM); |
| 63 | + } |
| 64 | + this.pre = matchedPattern < 3; |
| 65 | + parseContext.getParserState().setCurrentContexts(this.pre ? Collections.singleton(PreUseBlockContext.class) : Collections.singleton(PostUseBlockContext.class)); |
| 66 | + return true; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean check(TriggerContext ctx) { |
| 71 | + if (this.pre) return ctx instanceof PreUseBlockContext; |
| 72 | + return ctx instanceof PostUseBlockContext; |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public String toString(@NotNull TriggerContext ctx, boolean debug) { |
| 77 | + return "player use block"; |
| 78 | + } |
| 79 | + |
| 80 | + public static abstract class UseBlockContext<T extends UseBlockEvent> implements PlayerContext { |
| 81 | + |
| 82 | + final T event; |
| 83 | + final Player player; |
| 84 | + |
| 85 | + public UseBlockContext(T event, Player player) { |
| 86 | + this.event = event; |
| 87 | + this.player = player; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Player[] getPlayer() { |
| 92 | + return new Player[]{this.player}; |
| 93 | + } |
| 94 | + |
| 95 | + public BlockType[] getBlockType() { |
| 96 | + return new BlockType[]{this.event.getBlockType()}; |
| 97 | + } |
| 98 | + |
| 99 | + public InteractionType[] getInteractionType() { |
| 100 | + return new InteractionType[]{this.event.getInteractionType()}; |
| 101 | + } |
| 102 | + |
| 103 | + public Block[] getBlock() { |
| 104 | + World world = this.player.getWorld(); |
| 105 | + if (world == null) return null; |
| 106 | + return new Block[]{new Block(world, this.event.getTargetBlock(), this.event.getBlockType())}; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public static class PreUseBlockContext extends UseBlockContext<UseBlockEvent.Pre> implements CancellableContext { |
| 111 | + |
| 112 | + public PreUseBlockContext(UseBlockEvent.Pre event, Player player) { |
| 113 | + super(event, player); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public boolean isCancelled() { |
| 118 | + return this.event.isCancelled(); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void setCancelled(boolean cancelled) { |
| 123 | + this.event.setCancelled(cancelled); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String getName() { |
| 128 | + return "pre use block context"; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + public static class PostUseBlockContext extends UseBlockContext<UseBlockEvent.Post> { |
| 133 | + |
| 134 | + public PostUseBlockContext(UseBlockEvent.Post event, Player player) { |
| 135 | + super(event, player); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String getName() { |
| 140 | + return "post use block context"; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + public static class PreUseBlockSystem extends EntityEventSystem<EntityStore, UseBlockEvent.Pre> { |
| 145 | + |
| 146 | + protected PreUseBlockSystem() { |
| 147 | + super(UseBlockEvent.Pre.class); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public @Nullable Query<EntityStore> getQuery() { |
| 152 | + return Player.getComponentType(); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void handle(int i, @NotNull ArchetypeChunk<EntityStore> archetypeChunk, @NotNull Store<EntityStore> store, @NotNull CommandBuffer<EntityStore> commandBuffer, @NotNull UseBlockEvent.Pre pre) { |
| 157 | + |
| 158 | + Ref<EntityStore> ref = archetypeChunk.getReferenceTo(i); |
| 159 | + Player player = store.getComponent(ref, Player.getComponentType()); |
| 160 | + if (player == null) return; |
| 161 | + |
| 162 | + TriggerMap.callTriggersByContext(new PreUseBlockContext(pre, player)); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + public static class PostUseBlockSystem extends EntityEventSystem<EntityStore, UseBlockEvent.Post> { |
| 167 | + |
| 168 | + protected PostUseBlockSystem() { |
| 169 | + super(UseBlockEvent.Post.class); |
| 170 | + } |
| 171 | + |
| 172 | + @Override |
| 173 | + public @Nullable Query<EntityStore> getQuery() { |
| 174 | + return Player.getComponentType(); |
| 175 | + } |
| 176 | + |
| 177 | + @Override |
| 178 | + public void handle(int i, @NotNull ArchetypeChunk<EntityStore> archetypeChunk, @NotNull Store<EntityStore> store, |
| 179 | + @NotNull CommandBuffer<EntityStore> commandBuffer, @NotNull UseBlockEvent.Post post) { |
| 180 | + |
| 181 | + Ref<EntityStore> ref = archetypeChunk.getReferenceTo(i); |
| 182 | + Player player = store.getComponent(ref, Player.getComponentType()); |
| 183 | + if (player == null) return; |
| 184 | + |
| 185 | + TriggerMap.callTriggersByContext(new PostUseBlockContext(post, player)); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments