    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 var3 = (GameRuleValue)this.theGameRules.get(par1Str);
    035
    036        if (var3 != null)
    037        {
    038            var3.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 var2 = (GameRuleValue)this.theGameRules.get(par1Str);
    052        return var2 != null ? var2.getGameRuleStringValue() : "";
    053    }
    054
    055    /**
    056     * Gets the boolean Game Rule value.
    057     */
    058    public boolean getGameRuleBooleanValue(String par1Str)
    059    {
    060        GameRuleValue var2 = (GameRuleValue)this.theGameRules.get(par1Str);
    061        return var2 != null ? var2.getGameRuleBooleanValue() : false;
    062    }
    063
    064    /**
    065     * Return the defined game rules as NBT.
    066     */
    067    public NBTTagCompound writeGameRulesToNBT()
    068    {
    069        NBTTagCompound var1 = new NBTTagCompound("GameRules");
    070        Iterator var2 = this.theGameRules.keySet().iterator();
    071
    072        while (var2.hasNext())
    073        {
    074            String var3 = (String)var2.next();
    075            GameRuleValue var4 = (GameRuleValue)this.theGameRules.get(var3);
    076            var1.setString(var3, var4.getGameRuleStringValue());
    077        }
    078
    079        return var1;
    080    }
    081
    082    /**
    083     * Set defined game rules from NBT.
    084     */
    085    public void readGameRulesFromNBT(NBTTagCompound par1NBTTagCompound)
    086    {
    087        Collection var2 = par1NBTTagCompound.getTags();
    088        Iterator var3 = var2.iterator();
    089
    090        while (var3.hasNext())
    091        {
    092            NBTBase var4 = (NBTBase)var3.next();
    093            String var5 = var4.getName();
    094            String var6 = par1NBTTagCompound.getString(var4.getName());
    095            this.setOrCreateGameRule(var5, var6);
    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}