    001package net.minecraft.world;
    002
    003import java.util.Collection;
    004import java.util.Iterator;
    005import java.util.TreeMap;
    006import net.minecraft.nbt.NBTBase;
    007import net.minecraft.nbt.NBTTagCompound;
    008
    009public class GameRules
    010{
    011    private TreeMap theGameRules = new TreeMap();
    012
    013    public GameRules()
    014    {
    015        this.addGameRule("doFireTick", "true");
    016        this.addGameRule("mobGriefing", "true");
    017        this.addGameRule("keepInventory", "false");
    018        this.addGameRule("doMobSpawning", "true");
    019        this.addGameRule("doMobLoot", "true");
    020        this.addGameRule("doTileDrops", "true");
    021        this.addGameRule("commandBlockOutput", "true");
    022    }
    023
    024    /**
    025     * Define a game rule and its default value.
    026     */
    027    public void addGameRule(String par1Str, String par2Str)
    028    {
    029        this.theGameRules.put(par1Str, new GameRuleValue(par2Str));
    030    }
    031
    032    public void setOrCreateGameRule(String par1Str, String par2Str)
    033    {
    034        GameRuleValue gamerulevalue = (GameRuleValue)this.theGameRules.get(par1Str);
    035
    036        if (gamerulevalue != null)
    037        {
    038            gamerulevalue.setValue(par2Str);
    039        }
    040        else
    041        {
    042            this.addGameRule(par1Str, par2Str);
    043        }
    044    }
    045
    046    /**
    047     * Gets the string Game Rule value.
    048     */
    049    public String getGameRuleStringValue(String par1Str)
    050    {
    051        GameRuleValue gamerulevalue = (GameRuleValue)this.theGameRules.get(par1Str);
    052        return gamerulevalue != null ? gamerulevalue.getGameRuleStringValue() : "";
    053    }
    054
    055    /**
    056     * Gets the boolean Game Rule value.
    057     */
    058    public boolean getGameRuleBooleanValue(String par1Str)
    059    {
    060        GameRuleValue gamerulevalue = (GameRuleValue)this.theGameRules.get(par1Str);
    061        return gamerulevalue != null ? gamerulevalue.getGameRuleBooleanValue() : false;
    062    }
    063
    064    /**
    065     * Return the defined game rules as NBT.
    066     */
    067    public NBTTagCompound writeGameRulesToNBT()
    068    {
    069        NBTTagCompound nbttagcompound = new NBTTagCompound("GameRules");
    070        Iterator iterator = this.theGameRules.keySet().iterator();
    071
    072        while (iterator.hasNext())
    073        {
    074            String s = (String)iterator.next();
    075            GameRuleValue gamerulevalue = (GameRuleValue)this.theGameRules.get(s);
    076            nbttagcompound.setString(s, gamerulevalue.getGameRuleStringValue());
    077        }
    078
    079        return nbttagcompound;
    080    }
    081
    082    /**
    083     * Set defined game rules from NBT.
    084     */
    085    public void readGameRulesFromNBT(NBTTagCompound par1NBTTagCompound)
    086    {
    087        Collection collection = par1NBTTagCompound.getTags();
    088        Iterator iterator = collection.iterator();
    089
    090        while (iterator.hasNext())
    091        {
    092            NBTBase nbtbase = (NBTBase)iterator.next();
    093            String s = nbtbase.getName();
    094            String s1 = par1NBTTagCompound.getString(nbtbase.getName());
    095            this.setOrCreateGameRule(s, s1);
    096        }
    097    }
    098
    099    /**
    100     * Return the defined game rules.
    101     */
    102    public String[] getRules()
    103    {
    104        return (String[])this.theGameRules.keySet().toArray(new String[0]);
    105    }
    106
    107    /**
    108     * Return whether the specified game rule is defined.
    109     */
    110    public boolean hasRule(String par1Str)
    111    {
    112        return this.theGameRules.containsKey(par1Str);
    113    }
    114}