|
| 1 | +package com.easyclaims.systems; |
| 2 | + |
| 3 | +import com.hypixel.hytale.component.ArchetypeChunk; |
| 4 | +import com.hypixel.hytale.component.CommandBuffer; |
| 5 | +import com.hypixel.hytale.component.Ref; |
| 6 | +import com.hypixel.hytale.component.Store; |
| 7 | +import com.hypixel.hytale.component.dependency.Dependency; |
| 8 | +import com.hypixel.hytale.component.dependency.RootDependency; |
| 9 | +import com.hypixel.hytale.component.query.Query; |
| 10 | +import com.hypixel.hytale.component.system.EntityEventSystem; |
| 11 | +import com.hypixel.hytale.logger.HytaleLogger; |
| 12 | +import com.hypixel.hytale.math.vector.Vector3d; |
| 13 | +import com.hypixel.hytale.server.core.entity.entities.Player; |
| 14 | +import com.hypixel.hytale.server.core.event.events.ecs.InteractivelyPickupItemEvent; |
| 15 | +import com.hypixel.hytale.server.core.modules.entity.component.TransformComponent; |
| 16 | +import com.hypixel.hytale.server.core.universe.PlayerRef; |
| 17 | +import com.hypixel.hytale.server.core.universe.world.storage.EntityStore; |
| 18 | +import com.easyclaims.data.TrustLevel; |
| 19 | +import com.easyclaims.managers.ClaimManager; |
| 20 | +import com.easyclaims.util.Messages; |
| 21 | + |
| 22 | +import javax.annotation.Nonnull; |
| 23 | +import javax.annotation.Nullable; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.UUID; |
| 28 | +import java.util.concurrent.ConcurrentHashMap; |
| 29 | + |
| 30 | +/** |
| 31 | + * ECS System that intercepts item pickup events to protect claimed areas. |
| 32 | + * This prevents players from picking up items (like flowers) in protected chunks. |
| 33 | + */ |
| 34 | +public class ItemPickupProtectionSystem extends EntityEventSystem<EntityStore, InteractivelyPickupItemEvent> { |
| 35 | + |
| 36 | + private final ClaimManager claimManager; |
| 37 | + private final HytaleLogger logger; |
| 38 | + |
| 39 | + // Rate limit messages - don't spam players |
| 40 | + private static final Map<UUID, Long> lastMessageTime = new ConcurrentHashMap<>(); |
| 41 | + private static final long MESSAGE_COOLDOWN_MS = 2000; // 2 seconds |
| 42 | + |
| 43 | + public ItemPickupProtectionSystem(ClaimManager claimManager, HytaleLogger logger) { |
| 44 | + super(InteractivelyPickupItemEvent.class); |
| 45 | + this.claimManager = claimManager; |
| 46 | + this.logger = logger; |
| 47 | + } |
| 48 | + |
| 49 | + private boolean canSendMessage(UUID playerId) { |
| 50 | + long now = System.currentTimeMillis(); |
| 51 | + Long lastTime = lastMessageTime.get(playerId); |
| 52 | + if (lastTime == null || now - lastTime > MESSAGE_COOLDOWN_MS) { |
| 53 | + lastMessageTime.put(playerId, now); |
| 54 | + return true; |
| 55 | + } |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + @Nullable |
| 60 | + @Override |
| 61 | + public Query<EntityStore> getQuery() { |
| 62 | + return PlayerRef.getComponentType(); |
| 63 | + } |
| 64 | + |
| 65 | + @Nonnull |
| 66 | + @Override |
| 67 | + public Set<Dependency<EntityStore>> getDependencies() { |
| 68 | + return Collections.singleton(RootDependency.first()); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void handle(int entityIndex, @Nonnull ArchetypeChunk<EntityStore> chunk, @Nonnull Store<EntityStore> store, |
| 73 | + @Nonnull CommandBuffer<EntityStore> commandBuffer, @Nonnull InteractivelyPickupItemEvent event) { |
| 74 | + // Get the entity that triggered this event |
| 75 | + Ref<EntityStore> entityRef = chunk.getReferenceTo(entityIndex); |
| 76 | + if (entityRef == null) return; |
| 77 | + |
| 78 | + // Get player components |
| 79 | + Player player = store.getComponent(entityRef, Player.getComponentType()); |
| 80 | + PlayerRef playerRef = store.getComponent(entityRef, PlayerRef.getComponentType()); |
| 81 | + if (player == null || playerRef == null) return; |
| 82 | + |
| 83 | + // Get player's position from TransformComponent |
| 84 | + TransformComponent transform = store.getComponent(entityRef, TransformComponent.getComponentType()); |
| 85 | + if (transform == null) return; |
| 86 | + |
| 87 | + Vector3d position = transform.getPosition(); |
| 88 | + if (position == null) return; |
| 89 | + |
| 90 | + UUID playerId = playerRef.getUuid(); |
| 91 | + String worldName = player.getWorld().getName(); |
| 92 | + |
| 93 | + // Picking up items requires USE trust level (same as basic interaction) |
| 94 | + if (!claimManager.hasPermissionAt(playerId, worldName, position.getX(), position.getZ(), TrustLevel.USE)) { |
| 95 | + event.setCancelled(true); |
| 96 | + if (canSendMessage(playerId)) { |
| 97 | + player.sendMessage(Messages.cannotPickupItemsHere()); |
| 98 | + } |
| 99 | + logger.atFine().log("Blocked item pickup: player=%s position=[%.1f, %.1f]", |
| 100 | + playerId, position.getX(), position.getZ()); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments