-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathSearchResult.java
More file actions
127 lines (109 loc) · 3.74 KB
/
SearchResult.java
File metadata and controls
127 lines (109 loc) · 3.74 KB
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
package de.themoep.entitydetection.searcher;
import com.tcoded.folialib.impl.PlatformScheduler;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.BlockState;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Copyright 2016 Max Lee (https://github.com/Phoenix616/)
* <p/>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Mozilla Public License as published by
* the Mozilla Foundation, version 2.
* <p/>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Mozilla Public License v2.0 for more details.
* <p/>
* You should have received a copy of the Mozilla Public License v2.0
* along with this program. If not, see <http://mozilla.org/MPL/2.0/>.
*/
public abstract class SearchResult<T> {
protected final PlatformScheduler scheduler;
private SearchType type;
private Set<String> searched;
private long startTime;
private long endTime = 0;
/**
* Working search map, use resultEntryList after sorting this result!
*/
protected Map<T, SearchResultEntry<T>> resultEntryMap = new HashMap<>();
/**
* Sorted, highest entity count per chunks first, only propagated after running .sort()
*/
protected List<SearchResultEntry<T>> resultEntryList = new ArrayList<>();
public SearchResult(EntitySearch search) {
scheduler = search.getScheduler();
type = search.getType();
searched = new HashSet<>();
for (EntityType e : search.getSearchedEntities()) {
searched.add(e.toString());
}
for (Material m : search.getSearchedMaterial()) {
searched.add(m.toString());
}
for (Class<?> b : search.getSearchedBlockStates()) {
searched.add(b.getSimpleName());
}
startTime = search.getStartTime();
}
/**
* Add an entity to this result
* @param entity The entity to add
*/
public abstract void addEntity(Entity entity);
/**
* Add a BlockState to this result
* @param blockState The entity to add
*/
public abstract void addBlockState(BlockState blockState);
public abstract void add(Location location, String type);
public SearchType getType() {
return type;
}
public long getStartTime() {
return startTime;
}
public long getEndTime() {
return endTime == 0 ? System.currentTimeMillis() : endTime;
}
/**
* Get the time this search took to finish
* @return The duration in milliseconds
*/
public long getDuration() {
return getEndTime() - getStartTime();
}
public Set<String> getSearched() {
return searched;
}
/**
* Get a list of entries for every chunk, only propagated after calling sort()
* @return An ArrayList of the chunks sorted from the highest
*/
public List<SearchResultEntry<T>> getSortedEntries() {
return resultEntryList;
}
/**
* Sort the results and set the end time
*/
public void sort() {
for (SearchResultEntry<?> chunkEntry : resultEntryMap.values()) {
chunkEntry.sort();
}
resultEntryList = new ArrayList<>(resultEntryMap.values());
resultEntryList.sort(Collections.reverseOrder());
endTime = System.currentTimeMillis();
}
public abstract void teleport(Player sender, SearchResultEntry<T> entry, int i);
}