Advertisement
GenuineSounds

Test

Feb 9th, 2015
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.07 KB | None | 0 0
  1. package com.genuineflix.api.test;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class TestMetalImplementation implements IMetal {
  7.  
  8.     public static void main(String[] args) {
  9.         TestMetalImplementation copper = new TestMetalImplementation("copper");
  10.         TestMetalImplementation tin = new TestMetalImplementation("tin");
  11.         TestMetalImplementation bronze = new TestMetalImplementation("bronze");
  12.         bronze.compounds.add(new Compound(copper, 3));
  13.         bronze.compounds.add(new Compound(tin, 1));
  14.         System.out.println(bronze);
  15.         NBTTagCompound saved = bronze.save(new NBTTagCompound());
  16.         TestMetalImplementation newBronze = (TestMetalImplementation) new TestMetalImplementation().load(saved);
  17.         System.out.println(newBronze);
  18.         System.out.println("Bronze and new Bronze are " + (bronze.equals(newBronze) ? "" : "not ") + "equal");
  19.     }
  20.  
  21.     private String name;
  22.     private List<Compound> compounds = new ArrayList<>();
  23.  
  24.     public TestMetalImplementation() {}
  25.  
  26.     public TestMetalImplementation(String name) {
  27.         this.name = name;
  28.     }
  29.  
  30.     @Override
  31.     public String getName() {
  32.         return name;
  33.     }
  34.  
  35.     @Override
  36.     public NBTTagCompound save(NBTTagCompound tag) {
  37.         tag.setString("name", name);
  38.         if (compounds.isEmpty())
  39.             return tag;
  40.         NBTTagCompound comps = new NBTTagCompound();
  41.         for (int i = 0; i < compounds.size(); i++)
  42.             comps.setTag("compound" + i, compounds.get(i).save(new NBTTagCompound()));
  43.         tag.setTag("compounds", comps);
  44.         return tag;
  45.     }
  46.  
  47.     @Override
  48.     public IMetal load(NBTTagCompound nbt) {
  49.         name = nbt.getString("name");
  50.         if (nbt.hasKey("compounds")) {
  51.             int i = 0;
  52.             List<Compound> list = new ArrayList<>();
  53.             NBTTagCompound compounds = nbt.getCompoundTag("compounds");
  54.             NBTTagCompound tag;
  55.             while (!(tag = compounds.getCompoundTag("compound" + i)).hasNoTags()) {
  56.                 list.add(new Compound().load(tag));
  57.                 i++;
  58.             }
  59.             this.compounds = list;
  60.         }
  61.         return this;
  62.     }
  63.  
  64.     @Override
  65.     public List<Compound> getCompounds() {
  66.         return compounds;
  67.     }
  68.  
  69.     @Override
  70.     public String toString() {
  71.         return name + " " + compounds;
  72.     }
  73.  
  74.     @Override
  75.     public boolean equals(Object obj) {
  76.         if (!(obj instanceof IMetal))
  77.             return false;
  78.         TestMetalImplementation m = (TestMetalImplementation) obj;
  79.         if (!name.equals(m.name))
  80.             return false;
  81.         if (compounds.size() == 0 && m.compounds.size() == 0)
  82.             return true;
  83.         if (compounds.size() != m.compounds.size())
  84.             return false;
  85.         for (Compound these : compounds) {
  86.             boolean foundMatch = false;
  87.             for (Compound theirs : m.compounds) {
  88.                 if (these.equals(theirs)) {
  89.                     foundMatch = true;
  90.                     break;
  91.                 }
  92.             }
  93.             if (!foundMatch)
  94.                 return false;
  95.         }
  96.         return true;
  97.     }
  98. }
  99.  
  100. class Compound implements NBTSaveable, NBTLoadable<Compound> {
  101.  
  102.     public IMetal metal;
  103.     public int count;
  104.  
  105.     public Compound() {}
  106.  
  107.     public Compound(IMetal metal, int count) {
  108.         this.metal = metal;
  109.         this.count = count;
  110.     }
  111.  
  112.     @Override
  113.     public NBTTagCompound save(NBTTagCompound nbt) {
  114.         nbt.setString("class", metal.getClass().getName());
  115.         nbt.setTag("metal", metal.save(new NBTTagCompound()));
  116.         nbt.setInteger("count", count);
  117.         return nbt;
  118.     }
  119.  
  120.     @Override
  121.     public Compound load(NBTTagCompound nbt) {
  122.         try {
  123.             ClassLoader cl = ClassLoader.getSystemClassLoader();
  124.             @SuppressWarnings("unchecked")
  125.             Class<IMetal> clazz = (Class<IMetal>) cl.loadClass(nbt.getString("class"));
  126.             metal = clazz.newInstance().load(nbt.getCompoundTag("metal"));
  127.             count = nbt.getInteger("count");
  128.         } catch (Exception e) {
  129.             return null;
  130.         }
  131.         return this;
  132.     }
  133.  
  134.     @Override
  135.     public String toString() {
  136.         return count + "x " + metal.getName();
  137.     }
  138.  
  139.     @Override
  140.     public boolean equals(Object obj) {
  141.         if (!(obj instanceof Compound))
  142.             return false;
  143.         Compound c = (Compound) obj;
  144.         return count == c.count && metal.equals(c.metal);
  145.     }
  146. }
  147.  
  148. interface IMetal extends NBTSaveable, NBTLoadable<IMetal> {
  149.  
  150.     public String getName();
  151.  
  152.     public List<Compound> getCompounds();
  153. }
  154.  
  155. interface NBTLoadable<T> {
  156.  
  157.     public T load(NBTTagCompound nbt);
  158. }
  159.  
  160. interface NBTSaveable {
  161.  
  162.     public NBTTagCompound save(NBTTagCompound tag);
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement