3
0

增加对entity tick的拦截与判定

This commit is contained in:
2017-06-28 08:26:41 +08:00
parent 467c637ca6
commit 05b874f281
8 changed files with 419 additions and 93 deletions

View File

@ -46,6 +46,7 @@ public class CauldronHooks
public static int tickingDimension = 0;
public static ChunkCoordIntPair tickingChunk = null;
public static Map<Class<? extends TileEntity>, TileEntityCache> tileEntityCache = new HashMap<Class<? extends TileEntity>, TileEntityCache>();
public static Map<Class<? extends Entity>, EntityCache> entityCache = new HashMap<Class<? extends Entity>, EntityCache>();
private static TObjectLongHashMap<CollisionWarning> recentWarnings = new TObjectLongHashMap<CollisionWarning>();
@ -305,6 +306,26 @@ public class CauldronHooks
}
return true;
}
public static boolean canEntityTick(Entity pEntity,World world){
if(pEntity==null||world.tileentityConfig==null) return false;
if(MinecraftServer.entityConfig.skipEntityTicks.getValue()){
EntityCache eCache=entityCache.get(pEntity.getClass());
if(eCache==null){
String teConfigPath=pEntity.getClass().getName().replace(".","-");
teConfigPath=teConfigPath.replaceAll("[^A-Za-z0-9\\-]",""); // Fix up odd class names to prevent YAML errors
eCache=new EntityCache(pEntity.getClass(),world.getWorldInfo().getWorldName().toLowerCase(),teConfigPath,world.entityConfig.getInt(teConfigPath+".tick-interval",1));
entityCache.put(pEntity.getClass(),eCache);
}
// Skip tick interval
if(eCache.tickInterval>0&&(world.getWorldInfo().getWorldTotalTime()%eCache.tickInterval==0L)){
return true;
}
return false;
}
return true;
}
public static boolean canUpdate(TileEntity tileEntity)
{

View File

@ -0,0 +1,18 @@
package net.minecraftforge.cauldron;
import net.minecraft.entity.Entity;
public class EntityCache{
public Class<? extends Entity> tileEntityClass;
public int tickInterval=1;
public String configPath;
public String worldName;
public EntityCache(Class<? extends Entity> entityClass,String worldName,String configPath,int tickInterval){
this.tileEntityClass=entityClass;
this.worldName=worldName;
this.tickInterval=tickInterval;
this.configPath=configPath;
}
}

View File

@ -0,0 +1,173 @@
package net.minecraftforge.cauldron.command;
import java.util.ArrayList;
import java.util.List;
import net.minecraft.server.MinecraftServer;
import net.minecraftforge.cauldron.configuration.BoolSetting;
import net.minecraftforge.cauldron.configuration.IntSetting;
import net.minecraftforge.cauldron.configuration.Setting;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.util.StringUtil;
import com.google.common.collect.ImmutableList;
public class EntityCommand extends Command{
private static final List<String> COMMANDS=ImmutableList.of("get","set","save","reload");
public EntityCommand(){
super("cauldron_e");
this.description="Toggle certain TileEntity options";
this.usageMessage="/cauldron_e ["+StringUtils.join(COMMANDS,'|')+"] <option> [value]";
this.setPermission("cauldron.command.cauldron_e");
}
@Override
public boolean execute(CommandSender sender,String commandLabel,String[] args){
if(!testPermission(sender)){
return true;
}
if((args.length==1)&&"save".equalsIgnoreCase(args[0])){
MinecraftServer.getServer();
MinecraftServer.entityConfig.save();
sender.sendMessage(ChatColor.GREEN+"Config file saved");
return true;
}
if((args.length==1)&&"reload".equalsIgnoreCase(args[0])){
MinecraftServer.getServer();
MinecraftServer.entityConfig.load();
sender.sendMessage(ChatColor.GREEN+"Config file reloaded");
return true;
}
if(args.length<2){
sender.sendMessage(ChatColor.RED+"Usage: "+usageMessage);
return false;
}
if("get".equalsIgnoreCase(args[0])){
return getToggle(sender,args);
}else if("set".equalsIgnoreCase(args[0])){
return setToggle(sender,args);
}else{
sender.sendMessage(ChatColor.RED+"Usage: "+usageMessage);
}
return false;
}
private boolean getToggle(CommandSender sender,String[] args){
try{
MinecraftServer.getServer();
Setting toggle=MinecraftServer.entityConfig.getSettings().get(args[1]);
MinecraftServer.getServer();
// check config directly
if(toggle==null&&MinecraftServer.entityConfig.isSet(args[1])){
MinecraftServer.getServer();
if(MinecraftServer.entityConfig.isBoolean(args[1])){
MinecraftServer.getServer();
MinecraftServer.getServer();
toggle=new BoolSetting(MinecraftServer.entityConfig,args[1],MinecraftServer.entityConfig.getBoolean(args[1],false),"");
}else{
MinecraftServer.getServer();
if(MinecraftServer.entityConfig.isInt(args[1])){
MinecraftServer.getServer();
MinecraftServer.getServer();
toggle=new IntSetting(MinecraftServer.entityConfig,args[1],MinecraftServer.entityConfig.getInt(args[1],1),"");
}
}
if(toggle!=null){
MinecraftServer.getServer();
MinecraftServer.entityConfig.getSettings().put(toggle.path,toggle);
MinecraftServer.getServer();
MinecraftServer.entityConfig.load();
}
}
if(toggle==null){
sender.sendMessage(ChatColor.RED+"Could not find option: "+args[1]);
return false;
}
Object value=toggle.getValue();
String option=(Boolean.TRUE.equals(value)?ChatColor.GREEN:ChatColor.RED)+" "+value;
sender.sendMessage(ChatColor.GOLD+args[1]+" "+option);
}catch(Exception ex){
sender.sendMessage(ChatColor.RED+"Usage: "+usageMessage);
ex.printStackTrace();
}
return true;
}
private boolean intervalSet(CommandSender sender,String[] args){
try{
int setting=NumberUtils.toInt(args[2],1);
MinecraftServer.getServer();
MinecraftServer.entityConfig.set(args[1],setting);
}catch(Exception ex){
sender.sendMessage(ChatColor.RED+"Usage: "+usageMessage);
return false;
}
return true;
}
private boolean setToggle(CommandSender sender,String[] args){
try{
MinecraftServer.getServer();
Setting toggle=MinecraftServer.entityConfig.getSettings().get(args[1]);
MinecraftServer.getServer();
// check config directly
if(toggle==null&&MinecraftServer.entityConfig.isSet(args[1])){
MinecraftServer.getServer();
MinecraftServer.getServer();
toggle=new BoolSetting(MinecraftServer.entityConfig,args[1],MinecraftServer.entityConfig.getBoolean(args[1],false),"");
MinecraftServer.getServer();
MinecraftServer.entityConfig.getSettings().put(toggle.path,toggle);
MinecraftServer.getServer();
MinecraftServer.entityConfig.load();
}
if(toggle==null){
sender.sendMessage(ChatColor.RED+"Could not find option: "+args[1]);
return false;
}
if(args.length==2){
sender.sendMessage(ChatColor.RED+"Usage: "+args[0]+" "+args[1]+" [value]");
return false;
}
toggle.setValue(args[2]);
Object value=toggle.getValue();
String option=(Boolean.TRUE.equals(value)?ChatColor.GREEN:ChatColor.RED)+" "+value;
sender.sendMessage(ChatColor.GOLD+args[1]+" "+option);
MinecraftServer.getServer();
MinecraftServer.entityConfig.save();
}catch(Exception ex){
sender.sendMessage(ChatColor.RED+"Usage: "+usageMessage);
ex.printStackTrace();
}
return true;
}
@Override
public List<String> tabComplete(CommandSender sender,String alias,String[] args){
Validate.notNull(sender,"Sender cannot be null");
Validate.notNull(args,"Arguments cannot be null");
Validate.notNull(alias,"Alias cannot be null");
if(args.length==1){
return StringUtil.copyPartialMatches(args[0],COMMANDS,new ArrayList<String>(COMMANDS.size()));
}
if(((args.length==2)&&"get".equalsIgnoreCase(args[0]))||"set".equalsIgnoreCase(args[0])){
MinecraftServer.getServer();
MinecraftServer.getServer();
return StringUtil.copyPartialMatches(args[1],MinecraftServer.entityConfig.getSettings().keySet(),new ArrayList<String>(MinecraftServer.tileEntityConfig.getSettings().size()));
}
return ImmutableList.of();
}
}

View File

@ -0,0 +1,77 @@
package net.minecraftforge.cauldron.configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.WorldServer;
import net.minecraftforge.cauldron.CauldronHooks;
import net.minecraftforge.cauldron.EntityCache;
import net.minecraftforge.cauldron.command.EntityCommand;
public class EntityConfig extends ConfigBase{
private final String HEADER="Made By Imcc,to skip entity tick";
/*
* ========================================================================
*/
public final BoolSetting skipEntityTicks=new BoolSetting(this,"settings.skip-entity-ticks",true,
"If enabled, turns on entity tick skip feature");
/*
* ========================================================================
*/
public EntityConfig(String fileName,String commandName){
super(fileName,commandName);
init();
}
public void addCommands(){
commands.put(this.commandName,new EntityCommand());
}
public void init(){
settings.put(skipEntityTicks.path,skipEntityTicks);
load();
}
public void load(){
try{
config=YamlConfiguration.loadConfiguration(configFile);
String header=HEADER+"\n";
for(Setting toggle : settings.values()){
if(!toggle.description.equals(""))
header+="Setting: "+toggle.path+" Default: "+toggle.def+" # "+toggle.description+"\n";
config.addDefault(toggle.path,toggle.def);
settings.get(toggle.path).setValue(config.getString(toggle.path));
}
config.options().header(header);
config.options().copyDefaults(true);
version=getInt("config-version",1);
set("config-version",1);
for(EntityCache tCache : CauldronHooks.entityCache.values()){
tCache.tickInterval=config.getInt("world-settings."+tCache.worldName+"."+tCache.configPath+".tick-interval",config.getInt("world-settings.default."+tCache.configPath+".tick-interval"));
}
this.saveWorldConfigs();
this.save();
}catch(Exception ex){
MinecraftServer.getServer().logSevere("Could not load "+this.configFile);
ex.printStackTrace();
}
}
@Override
public void saveWorldConfigs(){
for(int i=0;i<MinecraftServer.getServer().worlds.size();++i){
WorldServer worldserver=MinecraftServer.getServer().worlds.get(i);
if(worldserver!=null&&worldserver.entityConfig!=null){
worldserver.entityConfig.save();
}
}
}
}

View File

@ -0,0 +1,8 @@
package net.minecraftforge.cauldron.configuration;
public class EntityWorldConfig extends WorldConfig{
public EntityWorldConfig(String worldName,ConfigBase configFile){
super(worldName,configFile);
}
}