2012-09-13 21:04:46 +02:00
|
|
|
/*
|
|
|
|
* ProtocolLib - Bukkit server library that allows access to the Minecraft protocol.
|
|
|
|
* Copyright (C) 2012 Kristian S. Stangeland
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 2 of
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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 GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with this program;
|
|
|
|
* if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
* 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
2012-09-12 19:04:53 +02:00
|
|
|
package com.comphenix.protocol.injector;
|
|
|
|
|
2012-10-01 00:57:14 +02:00
|
|
|
import java.util.HashSet;
|
|
|
|
import java.util.Set;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
import java.util.concurrent.ConcurrentMap;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
import net.minecraft.server.Packet;
|
|
|
|
|
|
|
|
import com.comphenix.protocol.reflect.StructureModifier;
|
2012-10-01 00:57:14 +02:00
|
|
|
import com.comphenix.protocol.reflect.compiler.BackgroundCompiler;
|
|
|
|
import com.comphenix.protocol.reflect.compiler.CompileListener;
|
|
|
|
import com.comphenix.protocol.reflect.compiler.CompiledStructureModifier;
|
2012-09-12 19:04:53 +02:00
|
|
|
|
2012-09-13 21:15:30 +02:00
|
|
|
/**
|
|
|
|
* Caches structure modifiers.
|
|
|
|
* @author Kristian
|
|
|
|
*/
|
2012-09-14 12:41:57 +02:00
|
|
|
public class StructureCache {
|
2012-09-12 19:04:53 +02:00
|
|
|
// Structure modifiers
|
2012-10-01 00:57:14 +02:00
|
|
|
private static ConcurrentMap<Integer, StructureModifier<Object>> structureModifiers =
|
|
|
|
new ConcurrentHashMap<Integer, StructureModifier<Object>>();
|
|
|
|
|
|
|
|
private static Set<Integer> compiling = new HashSet<Integer>();
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an empty Minecraft packet of the given ID.
|
|
|
|
* @param id - packet ID.
|
|
|
|
* @return Created packet.
|
|
|
|
*/
|
|
|
|
public static Packet newPacket(int id) {
|
|
|
|
try {
|
|
|
|
return (Packet) MinecraftRegistry.getPacketClassFromID(id, true).newInstance();
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
return null;
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
throw new RuntimeException("Access denied.", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a cached structure modifier for the given packet id.
|
|
|
|
* @param id - packet ID.
|
|
|
|
* @return A structure modifier.
|
|
|
|
*/
|
|
|
|
public static StructureModifier<Object> getStructure(int id) {
|
2012-10-01 00:57:14 +02:00
|
|
|
// Compile structures by default
|
|
|
|
return getStructure(id, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a cached structure modifier for the given packet id.
|
|
|
|
* @param id - packet ID.
|
|
|
|
* @param compile - whether or not to asynchronously compile the structure modifier.
|
|
|
|
* @return A structure modifier.
|
|
|
|
*/
|
|
|
|
public static StructureModifier<Object> getStructure(int id, boolean compile) {
|
2012-09-12 19:04:53 +02:00
|
|
|
|
|
|
|
StructureModifier<Object> result = structureModifiers.get(id);
|
2012-10-01 00:57:14 +02:00
|
|
|
|
|
|
|
// We don't want to create this for every lookup
|
2012-09-12 19:04:53 +02:00
|
|
|
if (result == null) {
|
2012-10-01 00:57:14 +02:00
|
|
|
// Use the vanilla class definition
|
|
|
|
final StructureModifier<Object> value = new StructureModifier<Object>(
|
2012-09-13 11:34:47 +02:00
|
|
|
MinecraftRegistry.getPacketClassFromID(id, true), Packet.class, true);
|
|
|
|
|
2012-10-01 00:57:14 +02:00
|
|
|
result = structureModifiers.putIfAbsent(id, value);
|
|
|
|
|
|
|
|
// We may end up creating multiple modifiers, but we'll agree on which to use
|
|
|
|
if (result == null) {
|
|
|
|
result = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Automatically compile the structure modifier
|
|
|
|
if (compile && !(result instanceof CompiledStructureModifier)) {
|
|
|
|
// Compilation is many orders of magnitude slower than synchronization
|
|
|
|
synchronized (compiling) {
|
|
|
|
final int idCopy = id;
|
|
|
|
final BackgroundCompiler compiler = BackgroundCompiler.getInstance();
|
|
|
|
|
|
|
|
if (!compiling.contains(id) && compiler != null) {
|
|
|
|
compiler.scheduleCompilation(result, new CompileListener<Object>() {
|
|
|
|
@Override
|
|
|
|
public void onCompiled(StructureModifier<Object> compiledModifier) {
|
|
|
|
structureModifiers.put(idCopy, compiledModifier);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
compiling.add(id);
|
|
|
|
}
|
|
|
|
}
|
2012-09-12 19:04:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|