Skip to content

Instantly share code, notes, and snippets.

@wesserboy
Created August 15, 2015 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wesserboy/b05aa0129d3079083ac8 to your computer and use it in GitHub Desktop.
Save wesserboy/b05aa0129d3079083ac8 to your computer and use it in GitHub Desktop.
package com.wesserboy.ItemListMod.proxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
public class ClientProxy extends CommonProxy {
private List<Integer> idList = new ArrayList<Integer>();
private Map<Integer, Byte> typeMap = new HashMap<Integer, Byte>();
@Override
public void createIdList(boolean useCreativeTest) {
Iterator iterator = Item.itemRegistry.iterator();
while(iterator.hasNext()){
Item item = (Item) iterator.next();
if(!useCreativeTest || item.getCreativeTab() != null){
idList.add(Item.getIdFromItem(item));
}
}
}
//Using an id system to visualize the amount of data needed to reconstruct on the server
//id 0: no subItems
//id 1: dmg subItems, starting at 0, incrementing by 1 for every subItem
//id 2: dmg subitems, starting at another value, incrementing by 1 for every subItem
//id 3: dmg subitems, starting at 0, incrementing by another value for every subItem
//id 4: dmg subItems, starting at another value, incrementing with another value for every subItem, but with regular pattern
//id 5: dmg subItems, with irregular pattern
//id 6: nbt subItems...? (still need to figure this mapping out...)
//
//How to send this to the server?
//First send the id of the needed data
//
//id 0: Item id
//id 1: Item id, amount of subItems
//id 2: Item id, amount of subItems, start value
//id 3: Item id, amount of subItems, increment value
//id 4: Item id, amount of subItems, start value, increment value
//id 5: Item id, amount of subItems, dmg values
//id 6: not sure how to map this yet, as a result not sure how to send it yet ;)
//
//variable formats:
//data id --> byte
//item id --> short will probably do (don't think id's go higher than 32767)
//amount of subItems --> byte (if you have more than 127 subItems you're doing something wrong...)
//start value --> byte? haven't encountered a startValue higher than 127
//increment value --> byte
//dmg value --> not sure yet, maybe a scaling system with a header byte indicating the format
@Override
public void mapSubitemsFromList() {
if(idList != null && !idList.isEmpty()){
for(int i = 0; i < idList.size(); i++){
Item item = (Item) Item.getItemById(idList.get(i));
if(!item.getHasSubtypes()){ //item has no subItems
typeMap.put(Item.getIdFromItem(item), (byte) 0);
}else{ //item has subItems
List<ItemStack> subItems = new ArrayList<ItemStack>();
item.getSubItems(item, null, subItems);
System.out.println(idList.get(i) + ": " + subItems.size() + "-->" + (item instanceof ItemBlock ? Block.getBlockFromItem(item).getClass() : item.getClass()));
if(subItems.size() > 0){
if(!isNbtInvolved(subItems)){ //subItems do not have nbt data
typeMap.put(Item.getIdFromItem(item), getDmgId(subItems));
}else{ // (some) subItems have nbt data
typeMap.put(Item.getIdFromItem(item), (byte) 6);
}
}
}
}
}
}
private boolean isNbtInvolved(List<ItemStack> subItems){
for(int i = 0; i < subItems.size(); i++){
if(subItems.get(i).hasTagCompound()){
return true;
}
}
return false;
}
private byte getDmgId(List<ItemStack> subItems){
int[] dmgValues = new int[subItems.size()];
for(int i = 0; i < subItems.size(); i++){
dmgValues[i] = subItems.get(i).getItemDamage();
}
Arrays.sort(dmgValues); //just to make sure :)
if(dmgValues[0] == 0){ //possible id's: 1, 3 (5)
if(incrementsByOne(dmgValues)){
return 1;
}else{ //possibly 3
if(hasPattern(dmgValues)){
return 3;
}
}
}else{ //possible id's: 2, 4 (5)
if(incrementsByOne(dmgValues)){
return 2;
}else{ //possibly 4
if(hasPattern(dmgValues)){
return 4;
}
}
}
return 5; //no pattern at all
}
private boolean incrementsByOne(int[] values){
for(int i = 1; i < values.length; i++){
if(values[i] - values[i-1] != 1){
return false;
}
}
return true;
}
private boolean hasPattern(int[] values){
int pattern = values[1] - values[0];
for(int i = 2; i < values.length; i++){
if(values[i] - values[i - 1] != pattern){
return false;
}
}
return true;
}
//Debug method
@Override
public void printMap() {
if(typeMap != null && !typeMap.isEmpty()){
System.out.println("Start printing the typeMap");
Iterator iterator = typeMap.keySet().iterator();
while(iterator.hasNext()){
int id = (Integer) iterator.next();
System.out.println(id + ". " + Item.itemRegistry.getNameForObject(Item.getItemById(id)) + " - " + typeMap.get(id));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment