Clean up retrieving tags by string

This commit is contained in:
Brady 2018-08-27 20:06:38 -05:00
parent af3bc18079
commit 65bfe466bb
No known key found for this signature in database
GPG Key ID: 73A788379A197567
2 changed files with 19 additions and 17 deletions

View File

@ -17,12 +17,11 @@
package baritone.chunk;
import com.google.common.collect.ImmutableList;
import net.minecraft.util.math.BlockPos;
import org.apache.commons.lang3.ArrayUtils;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/**
* A single waypoint
@ -30,6 +29,7 @@ import java.util.Map;
* @author leijurv
*/
public class Waypoint {
public final String name;
public final Tag tag;
private final long creationTimestamp;
@ -72,19 +72,21 @@ public class Waypoint {
}
public enum Tag {
HOME, DEATH, BED, USER;
HOME("home", "base"),
DEATH("death"),
BED("bed", "spawn"),
USER();
}
private static final List<Tag> TAG_LIST = ImmutableList.<Tag>builder().add(Tag.values()).build();
public static final Map<String, Tag> TAG_MAP;
private final String[] names;
static {
HashMap<String, Tag> map = new HashMap<>();
map.put("home", Tag.HOME);
map.put("base", Tag.HOME);
map.put("bed", Tag.BED);
map.put("spawn", Tag.BED);
map.put("death", Tag.DEATH);
TAG_MAP = Collections.unmodifiableMap(map);
Tag(String... names) {
this.names = names;
}
public static Tag fromString(String name) {
return TAG_LIST.stream().filter(tag -> ArrayUtils.contains(tag.names, name.toLowerCase())).findFirst().orElse(null);
}
}
}

View File

@ -195,7 +195,7 @@ public class ExampleBaritoneControl extends Behavior {
// for example, "show deaths"
waypointType = waypointType.substring(0, waypointType.length() - 1);
}
Waypoint.Tag tag = Waypoint.TAG_MAP.get(waypointType);
Waypoint.Tag tag = Waypoint.Tag.fromString(waypointType);
if (tag == null) {
displayChatMessageRaw("Not a valid tag. Tags are: " + Arrays.asList(Waypoint.Tag.values()).toString().toLowerCase());
event.cancel();
@ -218,7 +218,7 @@ public class ExampleBaritoneControl extends Behavior {
// for example, "show deaths"
waypointType = waypointType.substring(0, waypointType.length() - 1);
}
Waypoint.Tag tag = Waypoint.TAG_MAP.get(waypointType);
Waypoint.Tag tag = Waypoint.Tag.fromString(waypointType);
if (tag == null) {
displayChatMessageRaw("Not a valid tag. Tags are: " + Arrays.asList(Waypoint.Tag.values()).toString().toLowerCase());
event.cancel();