summaryrefslogtreecommitdiff
path: root/dicore3/core/src/main/java/io/dico/dicore/SpigotUtil.java
blob: afcae02fe0a2449a554406220998cf4d57b543c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package io.dico.dicore;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
import org.bukkit.entity.*;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack;
import org.bukkit.material.Attachable;
import org.bukkit.material.MaterialData;
import org.bukkit.projectiles.ProjectileSource;

import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Predicate;

public class SpigotUtil {
    
    private SpigotUtil() {
        throw new UnsupportedOperationException();
    }
    
    public static World matchWorld(String input) {
        try {
            UUID uid = UUID.fromString(input);
            World world = Bukkit.getWorld(uid);
            if (world != null) {
                return world;
            }
        } catch (IllegalArgumentException ignored) {
        }
        
        World result = Bukkit.getWorld(input);
        if (result == null) {
            input = input.toLowerCase().replace("_", "").replaceAll("[-_]", "");
            for (World world : Bukkit.getWorlds()) {
                if (world.getName().toLowerCase().equals(input)) {
                    result = world;
                    break;
                }
            }
        }
        
        return result;
    }
    
    public static Block getSupportingBlock(Block block) {
        MaterialData data = block.getState().getData();
        if (data instanceof Attachable) {
            BlockFace attachedOn = ((Attachable) data).getAttachedFace();
            return block.getRelative(attachedOn);
        }
        return null;
    }
    
    public static boolean isItemPresent(ItemStack stack) {
        return stack != null && stack.getType() != Material.AIR && stack.getAmount() > 0;
    }
    
    public static boolean removeItems(Inventory from, ItemStack item, int amount) {
        for (Map.Entry<Integer, ? extends ItemStack> entry : from.all(item.getType()).entrySet()) {
            ItemStack stack = entry.getValue();
            if (item.isSimilar(stack)) {
                amount -= stack.getAmount();
                int stackAmount = -Math.min(0, amount);
                if (stackAmount == 0) {
                    from.setItem(entry.getKey(), null);
                } else {
                    stack.setAmount(stackAmount);
                }
            }
        }
        return amount <= 0;
    }
    
    public static BlockFace yawToFace(float yaw) {
        if ((yaw %= 360) < 0)
            yaw += 360;
        if (45 <= yaw && yaw < 135)
            return BlockFace.WEST;
        if (135 <= yaw && yaw < 225)
            return BlockFace.NORTH;
        if (225 <= yaw && yaw < 315)
            return BlockFace.EAST;
        return BlockFace.SOUTH;
    }
    
    public static void addItems(InventoryHolder entity, ItemStack... items) {
        Location dropLocation;
        if (entity instanceof Entity) {
            dropLocation = ((Entity) entity).getLocation();
        } else if (entity instanceof BlockState) {
            dropLocation = ((BlockState) entity).getLocation().add(0.5, 1, 0.5);
        } else {
            throw new IllegalArgumentException("Can't find location of this InventoryHolder: " + entity);
        }
        World world = dropLocation.getWorld();
        for (ItemStack toDrop : entity.getInventory().addItem(items).values()) {
            world.dropItemNaturally(dropLocation, toDrop);
        }
    }
    
    public static String asJsonString(Object object) {
        return asJsonString(null, object, 0);
    }
    
    public static String asJsonString(String key, Object object, int indentation) {
        String indent = new String(new char[indentation * 2]).replace('\0', ' ');
        StringBuilder builder = new StringBuilder(indent);
        if (key != null) {
            builder.append(key).append(": ");
        }
        if (object instanceof ConfigurationSerializable) {
            object = ((ConfigurationSerializable) object).serialize();
        }
        if (object instanceof Map) {
            builder.append("{\n");
            Map<?, ?> map = (Map) object;
            for (Map.Entry entry : map.entrySet()) {
                builder.append(asJsonString(String.valueOf(entry.getKey()), entry.getValue(), indentation + 1));
            }
            builder.append(indent).append("}");
        } else if (object instanceof List) {
            builder.append("[\n");
            List list = (List) object;
            for (Object entry : list) {
                builder.append(asJsonString(null, entry, indentation + 1));
            }
            builder.append(indent).append("]");
        } else {
            builder.append(String.valueOf(object));
        }
        return builder.append(",\n").toString();
    }
    
    public static String asJsonString(String key, Object object, int indentation, BiConsumer<List<?>, StringBuilder> listHeader) {
        String indent = new String(new char[indentation * 2]).replace('\0', ' ');
        StringBuilder builder = new StringBuilder(indent);
        if (key != null) {
            builder.append(key).append(": ");
        }
        if (object instanceof ConfigurationSerializable) {
            object = ((ConfigurationSerializable) object).serialize();
        }
        if (object instanceof Map) {
            builder.append("{\n");
            Map<?, ?> map = (Map) object;
            for (Map.Entry entry : map.entrySet()) {
                builder.append(asJsonString(String.valueOf(entry.getKey()), entry.getValue(), indentation + 1, listHeader));
            }
            builder.append(indent).append("}");
        } else if (object instanceof List) {
            builder.append("[");
            List list = (List) object;
            listHeader.accept(list, builder);
            builder.append("\n");
            for (Object entry : list) {
                builder.append(asJsonString(null, entry, indentation + 1, listHeader));
            }
            builder.append(indent).append("]");
        } else {
            builder.append(String.valueOf(object));
        }
        return builder.append(",\n").toString();
    }
    
    public static BlockFace estimateDirectionTo(Location from, Location to) {
        double dx = from.getX() - to.getX();
        double dz = from.getZ() - to.getZ();
    
        boolean xGreater = Math.abs(dx) - Math.abs(dz) > 0;
        double f = xGreater ? 2 / Math.abs(dx) : 2 / Math.abs(dz);
        dx *= f;
        dz *= f;
    
        double other = Math.abs(xGreater ? dz : dx);
    
        if (other <= .5) {
            return xGreater ? (dx < 0 ? BlockFace.WEST : BlockFace.EAST) : (dz < 0 ? BlockFace.NORTH : BlockFace.SOUTH);
        }
    
        if (other < 1.5) {
            if (xGreater) {
                return dx < 0 ? (dz < 0 ? BlockFace.WEST_NORTH_WEST : BlockFace.WEST_SOUTH_WEST) : (dz < 0 ? BlockFace.EAST_NORTH_EAST : BlockFace.EAST_SOUTH_EAST);
            }
            return dx < 0 ? (dz < 0 ? BlockFace.NORTH_NORTH_WEST : BlockFace.SOUTH_SOUTH_WEST) : (dz < 0 ? BlockFace.NORTH_NORTH_EAST : BlockFace.SOUTH_SOUTH_EAST);
        }
    
        return dx < 0 ? (dz < 0 ? BlockFace.NORTH_WEST : BlockFace.SOUTH_WEST) : (dz < 0 ? BlockFace.NORTH_EAST : BlockFace.SOUTH_EAST);
    }
    
    public static Entity findEntityFromDamager(Entity damager, EntityType searched) {
        if (damager.getType() == searched) {
            return damager;
        }
        
        if (damager instanceof Projectile) {
            ProjectileSource shooter = ((Projectile) damager).getShooter();
            if (shooter instanceof Entity && ((Entity) shooter).getType() == searched) {
                return (Entity) shooter;
            }
            return null;
        }
        
        if (damager.getType() == EntityType.PRIMED_TNT) {
            Entity source = ((TNTPrimed) damager).getSource();
            if (source.getType() == searched) {
                return source;
            }
        }
    
        return null;
    }
    
    public static int xpForNextLevel(int currentLevel) {
        if (currentLevel >= 30) {
            return 112 + (currentLevel - 30) * 9;
        }
        
        if (currentLevel >= 15) {
            return 37 + (currentLevel - 15) * 5;
        }
        
        return 7 + currentLevel * 2;
    }
    
    public static int removeExp(Player entity, int xp) {
        int total = entity.getTotalExperience();
        if (xp > total) {
            xp = total;
        }
        
        int level = entity.getLevel();
        if (level < 0) {
            return 0;
        }
        
        int removed = 0;
        int xpForNextLevel = xpForNextLevel(level);
        int current = (int) entity.getExp() * xpForNextLevel;
        
        if (xp > current) {
            xp -= current;
            total -= current;
            removed += current;
            
            if (level == 0) {
                entity.setExp(0F);
                entity.setTotalExperience(total);
                return removed;
            }
        } else {
            current -= xp;
            total -= xp;
            removed += xp;
            
            entity.setExp((float) current / xpForNextLevel);
            entity.setTotalExperience(total);
            return removed;
        }
        
        do {
            xpForNextLevel = xpForNextLevel(--level);
            if (xpForNextLevel >= xp) {
                total -= xp;
                removed += xp;
                
                entity.setExp(1F / xpForNextLevel * (xpForNextLevel - xp));
                entity.setTotalExperience(total);
                entity.setLevel(level);
                return removed;
            }
            
            xp -= xpForNextLevel;
            total -= xpForNextLevel;
            removed += xpForNextLevel;
        } while (level > 0);
        
        entity.setExp(0F);
        entity.setTotalExperience(0);
        entity.setLevel(0);
        return removed;
    }
    
    public static int getTotalExp(Player entity) {
        int rv = 0;
        int level = Math.min(entity.getLevel(), 20000);
        for (int i = 0; i < level; i++) {
            rv += xpForNextLevel(i);
        }
        rv += Math.min(1F, Math.max(0F, entity.getExp())) * xpForNextLevel(level);
        return rv;
    }
    
    public static double getTotalExpLevels(Player entity) {
        int xp = entity.getTotalExperience();
        
        int level = 0;
        while (xp > 0 && level < 20000) {
            int needed = xpForNextLevel(level);
            if (needed > xp) {
                return level + ((double) xp / needed);
            }
            xp -= needed;
            level++;
        }
        
        return level;
    }
    
    public static int getNearbyPlayerCount(Player origin, double range, Predicate<Player> predicate) {
        List<Entity> entities = origin.getNearbyEntities(range, range, range);
        int result = 0;
        for (Entity entity : entities) {
            if (entity.getType() == EntityType.PLAYER && predicate.test((Player) entity)) {
                result++;
            }
        }
        return result;
    }
    
    public static void getNearbyPlayers(Player origin, double range, Collection<Player> collection, Predicate<Player> predicate) {
        List<Entity> entities = origin.getNearbyEntities(range, range, range);
        for (Entity entity : entities) {
            if (entity.getType() == EntityType.PLAYER && predicate.test((Player) entity)) {
                collection.add((Player) entity);
            }
        }
    }
    
    public static void forEachNearbyPlayer(Player origin, double range, Consumer<Player> action) {
        List<Entity> entities = origin.getNearbyEntities(range, range, range);
        for (Entity entity : entities) {
            if (entity.getType() == EntityType.PLAYER) {
                action.accept((Player) entity);
            }
        }
    }
    
    public static double distanceSquared(Location first, Location second) {
        double dx = first.getX() - second.getX();
        double dy = first.getY() - second.getY();
        double dz = first.getZ() - second.getZ();
        
        return dx * dx + dy * dy + dz * dz;
    }
    
    
    public static <T extends Entity> Iterator<T> findNearbyEntities(Entity origin, boolean includeSelf, Predicate<Entity> predicate, double horizontalRange, double verticalRange) {
        Objects.requireNonNull(origin);
        return new Iterator<T>() {
            Entity next;
            List<Entity> nearby;
            int index = 0;
            int size;
            
            {
                if (includeSelf) {
                    next = origin;
                } else {
                    next = findNext();
                }
            }
            
            Entity findNext() {
                if (nearby == null) {
                    nearby = origin.getNearbyEntities(horizontalRange, verticalRange, horizontalRange);
                    size = nearby.size();
                }
                
                while (index < size) {
                    Entity e = nearby.get(index++);
                    if (predicate.test(e)) {
                        return e;
                    }
                }
                
                return null;
            }
            
            @Override
            public boolean hasNext() {
                return next != null;
            }
            
            @Override
            public T next() {
                if (next == null) {
                    throw new NoSuchElementException();
                }
                Entity result = next;
                next = findNext();
                //noinspection unchecked
                return (T) result;
            }
            
        };
    }
    
    
}