001/* 002 * Trident - A Multithreaded Server Alternative 003 * Copyright 2014 The TridentSDK Team 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package net.tridentsdk.base; 018 019import java.util.Map; 020 021import com.google.common.collect.Maps; 022 023public enum Enchantment { 024 025 ; 026 027 private static final Map<Short, Enchantment> enchsById = Maps.newConcurrentMap(); 028 private static final Map<String, Enchantment> enchsByName = Maps.newConcurrentMap(); 029 030 static { 031 for (Enchantment ench : values()) { 032 if (!enchsById.containsKey(ench.id)) { 033 enchsById.put(ench.id, ench); 034 } 035 if (!Enchantment.enchsByName.containsKey(ench.name.toLowerCase())) { 036 Enchantment.enchsByName.put(ench.name.toLowerCase(), ench); 037 } 038 } 039 } 040 041 public static Enchantment fromId(short id) { 042 return Enchantment.enchsById.get(id); 043 } 044 045 public static Enchantment fromName(String name) { 046 return Enchantment.enchsByName.get(name.toLowerCase()); 047 } 048 049 private final short id; 050 private final String name; 051 052 // accepts int because you don't need to cast it in calls 053 private Enchantment(int id, String name) { 054 this.id = (short) id; 055 this.name = name; 056 } 057 058 public short id() { 059 return this.id; 060 } 061 062 @Override 063 public String toString() { 064 return this.name; 065 } 066}