001/* 002 * Colonel -- a brigadier expansion library 003 * Copyright (C) zml and Colonel contributors 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 ca.stellardrift.colonel.impl; 018 019import ca.stellardrift.colonel.api.ServerArgumentType; 020import com.mojang.brigadier.arguments.ArgumentType; 021import net.minecraft.entity.player.PlayerEntity; 022import net.minecraft.server.network.ServerPlayerEntity; 023import net.minecraft.util.Identifier; 024 025import java.util.Collections; 026import java.util.HashMap; 027import java.util.Map; 028import java.util.Set; 029import java.util.concurrent.ConcurrentHashMap; 030 031import static java.util.Objects.requireNonNull; 032 033public class ServerArgumentTypes { 034 private static final Map<Class<?>, ServerArgumentType<?>> BY_TYPE = new HashMap<>(); 035 private static final Map<Identifier, ServerArgumentType<?>> BY_ID = new ConcurrentHashMap<>(); 036 037 @SuppressWarnings("unchecked") 038 public static <T extends ArgumentType<?>> ServerArgumentType<T> byClass(final Class<T> clazz) { 039 return (ServerArgumentType<T>)BY_TYPE.get(requireNonNull(clazz, "clazz")); 040 } 041 042 public static void register(final ServerArgumentType<?> type) { 043 BY_TYPE.put(type.type(), type); 044 BY_ID.put(type.id(), type); 045 } 046 047 public static Set<Identifier> getIds() { 048 return Collections.unmodifiableSet(BY_ID.keySet()); 049 } 050 051 public static void setKnownArgumentTypes(final PlayerEntity player, final Set<Identifier> ids) { 052 if(player instanceof ServerPlayerEntity) { 053 final ServerPlayerEntity spe = (ServerPlayerEntity) player; 054 ((ServerPlayerBridge) player).colonel$knownArguments(ids); 055 if (!ids.isEmpty()) { // TODO: Avoid resending the whole command tree, find a way to receive the packet before sending? 056 spe.server.getPlayerManager().sendCommandTree(spe); 057 } 058 } 059 } 060 061 public static Set<Identifier> getKnownArgumentTypes(final ServerPlayerEntity player) { 062 return ((ServerPlayerBridge) player).colonel$knownArguments(); 063 } 064}