下面列出了怎么用net.minecraftforge.common.util.Constants的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void readFromNBT (NBTTagCompound tag) {
super.readFromNBT(tag);
NBTTagList list = tag.getTagList("Items", Constants.NBT.TAG_COMPOUND);
furnaceItemStacks = new ItemStack[getSizeInventory()];
for (int i = 0, n = list.tagCount(); i < n; i++) {
NBTTagCompound itemTag = list.getCompoundTagAt(i);
byte slot = itemTag.getByte("Slot");
if (slot >= 0 && slot < furnaceItemStacks.length)
furnaceItemStacks[slot] = ItemStack.loadItemStackFromNBT(itemTag);
}
furnaceBurnTime = tag.getShort("BurnTime");
furnaceCookTime = tag.getShort("CookTime");
currentItemBurnTime = getItemBurnTime(furnaceItemStacks[SLOT_FUEL]);
if (tag.hasKey("CustomName", Constants.NBT.TAG_STRING))
customName = tag.getString("CustomName");
}
/**
* Get a compound tag by the given name <b>tagName</b> from the other compound tag <b>nbt</b>.
* If one doesn't exist, then it will be created and added if <b>create</b> is true, otherwise null is returned.
*/
@Nullable
public static NBTTagCompound getCompoundTag(@Nullable NBTTagCompound nbt, @Nonnull String tagName, boolean create)
{
if (nbt == null)
{
return null;
}
if (create == false)
{
return nbt.hasKey(tagName, Constants.NBT.TAG_COMPOUND) ? nbt.getCompoundTag(tagName) : null;
}
// create = true
if (nbt.hasKey(tagName, Constants.NBT.TAG_COMPOUND) == false)
{
nbt.setTag(tagName, new NBTTagCompound());
}
return nbt.getCompoundTag(tagName);
}
public static List<Upgrade> getActiveUpgradesFromTag(CompoundNBT tagCompound) {
ListNBT upgrades = tagCompound.getList(KEY_UPGRADES, Constants.NBT.TAG_COMPOUND);
List<Upgrade> functionalUpgrades = new ArrayList<>();
if (upgrades.isEmpty())
return functionalUpgrades;
for (int i = 0; i < upgrades.size(); i++) {
CompoundNBT tag = upgrades.getCompound(i);
Upgrade type = getUpgradeByName(tag.getString(KEY_UPGRADE));
if (type == null)
continue;
type.setEnabled(!tag.contains(KEY_ENABLED) || tag.getBoolean(KEY_ENABLED));
if (type.isEnabled())
functionalUpgrades.add(type);
}
return functionalUpgrades;
}
@Override
public CompoundNBT write(CompoundNBT tag) {
if (renderBlock!= null)
tag.put("renderBlock", NBTUtil.writeBlockState(renderBlock));
tag.putInt("originalDurability", originalDurability);
tag.putInt("priorDurability", priorDurability);
tag.putInt("durability", durability);
tag.putInt("ticksSinceMine", ticksSinceMine);
if (!playerUUID.equals(null))
tag.putUniqueId("playerUUID", playerUUID);
tag.put("upgrades", UpgradeTools.setUpgradesNBT(gadgetUpgrades).getList("upgrades", Constants.NBT.TAG_COMPOUND));
tag.putByte("breakType", (byte) breakType.ordinal());
tag.put("gadgetFilters", MiningProperties.serializeItemStackList(getGadgetFilters()));
tag.putBoolean("gadgetIsWhitelist", isGadgetIsWhitelist());
tag.putBoolean("blockAllowed", blockAllowed);
return super.write(tag);
}
@Override
public void deserializeNBT(NBTTagCompound compound) {
this.workingEnabled = compound.getBoolean("WorkEnabled");
this.progressTime = compound.getInteger("Progress");
if(compound.hasKey("AllowOverclocking")) {
this.allowOverclocking = compound.getBoolean("AllowOverclocking");
}
this.isActive = false;
if (progressTime > 0) {
this.isActive = true;
this.maxProgressTime = compound.getInteger("MaxProgress");
this.recipeEUt = compound.getInteger("RecipeEUt");
NBTTagList itemOutputsList = compound.getTagList("ItemOutputs", Constants.NBT.TAG_COMPOUND);
this.itemOutputs = NonNullList.create();
for (int i = 0; i < itemOutputsList.tagCount(); i++) {
this.itemOutputs.add(new ItemStack(itemOutputsList.getCompoundTagAt(i)));
}
NBTTagList fluidOutputsList = compound.getTagList("FluidOutputs", Constants.NBT.TAG_COMPOUND);
this.fluidOutputs = new ArrayList<>();
for (int i = 0; i < fluidOutputsList.tagCount(); i++) {
this.fluidOutputs.add(FluidStack.loadFluidStackFromNBT(fluidOutputsList.getCompoundTagAt(i)));
}
}
}
private void readDataForItemTypeFromNBT(NBTTagCompound tagData, Map<ItemType, NBTTagCompound> mapTags, Map<ItemType, Integer> mapIndices)
{
if (tagData.hasKey("ItemType", Constants.NBT.TAG_COMPOUND) && tagData.hasKey("Tag", Constants.NBT.TAG_COMPOUND))
{
ItemStack stack = new ItemStack(tagData.getCompoundTag("ItemType"));
if (stack.isEmpty() == false && stack.getItem() instanceof ItemBlockPlacementProperty)
{
ItemBlockPlacementProperty item = (ItemBlockPlacementProperty) stack.getItem();
if (item.hasPlacementProperty(stack))
{
boolean nbtSensitive = item.getPlacementProperty(stack).isNBTSensitive();
ItemType type = new ItemType(stack, nbtSensitive);
mapTags.put(type, tagData.getCompoundTag("Tag"));
if (tagData.hasKey("Index", Constants.NBT.TAG_BYTE))
{
mapIndices.put(type, Integer.valueOf(tagData.getByte("Index")));
}
}
}
}
}
public void readFromNBTCustom(NBTTagCompound nbt)
{
if (nbt.hasKey("Rotation", Constants.NBT.TAG_BYTE))
{
this.facing = EnumFacing.byIndex(nbt.getByte("Rotation"));
}
/*
if (nbt.hasKey("Camo", Constants.NBT.TAG_COMPOUND))
{
this.camoState = NBTUtils.readBlockStateFromTag(nbt.getCompoundTag("Camo"));
}
*/
if (nbt.hasKey("Camo", Constants.NBT.TAG_INT))
{
this.camoState = Block.getStateById(nbt.getInteger("Camo"));
}
if (nbt.hasKey("CamoData", Constants.NBT.TAG_COMPOUND))
{
this.camoData = nbt.getCompoundTag("CamoData");
}
this.ownerData = OwnerData.getOwnerDataFromNBT(nbt);
}
@Override
public void setPlacementProperties(World world, BlockPos pos, ItemStack stack, NBTTagCompound tag)
{
if (tag.hasKey("inserter.stack_limit", Constants.NBT.TAG_BYTE))
{
this.setStackLimit(tag.getByte("inserter.stack_limit"));
}
if (tag.hasKey("inserter.delay", Constants.NBT.TAG_INT))
{
this.setUpdateDelay(tag.getInteger("inserter.delay"));
}
if (tag.hasKey("inserter.redstone_mode", Constants.NBT.TAG_BYTE))
{
this.setRedstoneModeFromInteger(tag.getByte("inserter.redstone_mode"));
}
this.markDirty();
}
@Override
public void handleUpdateTag(NBTTagCompound tag)
{
this.activeTargetId = tag.getByte("s");
this.displayName = tag.getString("n");
for (int i = 0; i < 9; i++)
{
this.colors[i] = this.getColorFromDyeMeta(tag.getByte("mt" + i));
}
for (int i = 0; i < 8; i++)
{
if (tag.hasKey("n" + i, Constants.NBT.TAG_STRING))
{
this.targetDisplayNames[i] = tag.getString("n" + i);
}
}
super.handleUpdateTag(tag);
}
/**
* Remove a compound tag by the name <b>tagName</b>. If <b>containerTagName</b> is not null, then
* the tag is removed from inside a tag by that name.
*/
public static void removeCompoundTag(@Nonnull ItemStack stack, @Nullable String containerTagName, @Nonnull String tagName)
{
NBTTagCompound nbt = getCompoundTag(stack, containerTagName, false);
if (nbt != null && nbt.hasKey(tagName, Constants.NBT.TAG_COMPOUND))
{
nbt.removeTag(tagName);
if (nbt.isEmpty())
{
if (containerTagName != null)
{
stack.getTagCompound().removeTag(containerTagName);
}
else
{
stack.setTagCompound(null);
}
}
}
}
public static MCNetworkState fromNBT(RailNetworkManager railNetworkManager, NBTTagCompound tag){
NBTTagList trainList = tag.getTagList("trains", Constants.NBT.TAG_COMPOUND);
List<MCTrain> trains = new ArrayList<>();
for(int i = 0; i < trainList.tagCount(); i++) {
trains.add(MCTrain.fromNBT(railNetworkManager, trainList.getCompoundTagAt(i)));
}
Map<MCPos, EnumForceMode> forcedSignalMap = new HashMap<>();
NBTTagList forcedSignals = tag.getTagList("forcedSignals", Constants.NBT.TAG_COMPOUND);
for(int i = 0; i < forcedSignals.tagCount(); i++) {
NBTTagCompound t = forcedSignals.getCompoundTagAt(i);
forcedSignalMap.put(new MCPos(t), EnumForceMode.values()[t.getByte("f")]);
}
MCNetworkState state = new MCNetworkState(railNetworkManager);
state.signalForces = forcedSignalMap;
state.setTrains(trains);
return state;
}
@Override
public void setPlacementProperties(World world, BlockPos pos, ItemStack stack, NBTTagCompound tag)
{
if (tag.hasKey("drawbridge.delay", Constants.NBT.TAG_BYTE))
{
this.setDelayFromByte(tag.getByte("drawbridge.delay"));
}
if (tag.hasKey("drawbridge.length", Constants.NBT.TAG_BYTE))
{
this.setMaxLength(tag.getByte("drawbridge.length"));
}
if (tag.hasKey("drawbridge.redstone_mode", Constants.NBT.TAG_BYTE))
{
this.setRedstoneMode(tag.getByte("drawbridge.redstone_mode"));
}
this.markDirty();
}
@Override
public void readFromNBT(NBTTagCompound nbt) {
super.readFromNBT(nbt);
NBTTagList nbttaglist = nbt.getTagList("Items", 10);
inventory = new ItemStack[getSizeInventory()];
for (int i = 0; i < nbttaglist.tagCount(); i++) {
NBTTagCompound nbt1 = nbttaglist.getCompoundTagAt(i);
byte b0 = nbt1.getByte("Slot");
if (b0 >= 0 && b0 < inventory.length)
inventory[b0] = ItemStack.loadItemStackFromNBT(nbt1);
}
brewTime = nbt.getShort("BrewTime");
if (nbt.hasKey("Fuel", Constants.NBT.TAG_SHORT)) {
fuel = nbt.getShort("Fuel");
if (fuel > 0)
currentFuel = 30;
} else {
fuel = nbt.getInteger("Fuel");
currentFuel = nbt.getInteger("CurrentFuel");
}
}
public static BlockPosEU readFromTag(NBTTagCompound tag)
{
if (tag == null ||
tag.hasKey("posX", Constants.NBT.TAG_INT) == false ||
tag.hasKey("posY", Constants.NBT.TAG_INT) == false ||
tag.hasKey("posZ", Constants.NBT.TAG_INT) == false ||
tag.hasKey("dim", Constants.NBT.TAG_INT) == false ||
tag.hasKey("face", Constants.NBT.TAG_BYTE) == false)
{
return null;
}
int x = tag.getInteger("posX");
int y = tag.getInteger("posY");
int z = tag.getInteger("posZ");
int dim = tag.getInteger("dim");
int face = tag.getByte("face");
return new BlockPosEU(x, y, z, dim, EnumFacing.byIndex(face));
}
@SubscribeEvent
@SuppressWarnings("unchecked")
public void onWorldTick(TickEvent.ServerTickEvent event) {
if (event.phase != TickEvent.Phase.END || event.side != Side.SERVER)
return;
if (EtFuturum.enablePlayerSkinOverlay)
if (playerLoggedInCooldown != null)
if (--playerLoggedInCooldown <= 0) {
for (World world : MinecraftServer.getServer().worldServers)
for (EntityPlayer player : (List<EntityPlayer>) world.playerEntities) {
NBTTagCompound nbt = player.getEntityData();
if (nbt.hasKey(SetPlayerModelCommand.MODEL_KEY, Constants.NBT.TAG_BYTE)) {
boolean isAlex = nbt.getBoolean(SetPlayerModelCommand.MODEL_KEY);
EtFuturum.networkWrapper.sendToAll(new SetPlayerModelMessage(player, isAlex));
}
}
playerLoggedInCooldown = null;
}
}
@Override
public Object getMeta(Item target, ItemStack stack) {
Map<String, Object> result = Maps.newHashMap();
String type = safariNets.get(target.getUnlocalizedName());
result.put("type", Objects.firstNonNull(type, "unknown"));
NBTTagCompound tag = stack.getTagCompound();
if (tag != null) {
if (tag.getBoolean("hide")) {
result.put("captured", "?");
} else if (tag.hasKey("id", Constants.NBT.TAG_STRING)) {
result.put("captured", tag.getString("id"));
}
}
return result;
}
public void chunkUnload(TownBlock block) {
ForgeChunkManager.Ticket ticket = get(block.getDim());
ForgeChunkManager.unforceChunk(ticket, block.toChunkCoords());
NBTTagList list = ticket.getModData().getTagList("chunkCoords", Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound chunkNBT = list.getCompoundTagAt(i);
int x = chunkNBT.getInteger("x");
int z = chunkNBT.getInteger("z");
if (x == block.getX() && z == block.getZ()) {
list.removeTag(i);
break;
}
}
}
public void setPropertyValue(UUID uuid, ItemType itemType, String key, Integer valueType, int value)
{
NBTTagCompound tag = this.getOrCreatePropertyTag(uuid, itemType);
switch (valueType)
{
case Constants.NBT.TAG_BYTE:
tag.setByte(key, (byte) value);
break;
case Constants.NBT.TAG_SHORT:
tag.setShort(key, (short) value);
break;
case Constants.NBT.TAG_INT:
tag.setInteger(key, value);
break;
default:
}
this.dirty = true;
}
@Override
public NBTTagCompound getNBTShareTag(ItemStack stack)
{
NBTTagCompound tag = NBTUtils.getCompoundTag(stack, "Carrying", false);
if (tag != null)
{
NBTTagCompound newTagCarrying = new NBTTagCompound();
newTagCarrying.setString("Block", tag.getString("Block"));
if (tag.hasKey("DisplayName", Constants.NBT.TAG_STRING))
{
newTagCarrying.setString("DisplayName", tag.getString("DisplayName"));
}
NBTTagCompound newTag = new NBTTagCompound();
newTag.setTag("Carrying", newTagCarrying);
return newTag;
}
return null;
}
@Nullable
private String getCarriedBlockName(ItemStack stack)
{
NBTTagCompound tag = NBTUtils.getCompoundTag(stack, "Carrying", false);
if (tag != null)
{
String name;
if (tag.hasKey("DisplayName", Constants.NBT.TAG_STRING))
{
name = tag.getString("DisplayName");
}
else
{
name = tag.getString("Block");
}
return name;
}
return null;
}
@Nullable
private String getEntityName(ItemStack containerStack, int index, boolean useDisplayNameFormatting)
{
ItemStack moduleStack = this.getSelectedModuleStack(containerStack, ModuleType.TYPE_MEMORY_CARD_MISC);
if (moduleStack.isEmpty())
{
return null;
}
NBTTagList tagList = NBTUtils.getTagList(moduleStack, WRAPPER_TAG_NAME, "Entities", Constants.NBT.TAG_COMPOUND, false);
if (tagList != null && tagList.tagCount() > index)
{
return this.getNameForEntityFromTag(tagList.getCompoundTagAt(index), useDisplayNameFormatting);
}
return null;
}
public boolean getAlwaysRenderLocation(ItemStack rulerStack, int index)
{
ItemStack moduleStack = this.getSelectedModuleStack(rulerStack, ModuleType.TYPE_MEMORY_CARD_MISC);
if (moduleStack.isEmpty() == false)
{
if (index < 0)
{
index = this.getLocationSelection(rulerStack);
}
NBTTagList tagList = NBTUtils.getTagList(moduleStack, TAG_WRAPPER, TAG_LOCATIONS, Constants.NBT.TAG_COMPOUND, false);
if (tagList != null)
{
NBTTagCompound tag = tagList.getCompoundTagAt(index);
return tag.getBoolean(TAG_RENDER_WITH_ALL);
}
}
return false;
}
public BlockPosEU getPosition(ItemStack rulerStack, int index, boolean isPos1)
{
ItemStack moduleStack = this.getSelectedModuleStack(rulerStack, ModuleType.TYPE_MEMORY_CARD_MISC);
if (moduleStack.isEmpty() == false)
{
NBTTagList tagList = NBTUtils.getTagList(moduleStack, TAG_WRAPPER, TAG_LOCATIONS, Constants.NBT.TAG_COMPOUND, false);
if (tagList == null)
{
return null;
}
if (index < 0)
{
index = this.getLocationSelection(rulerStack);
}
String tagName = isPos1 ? "Pos1" : "Pos2";
NBTTagCompound tag = tagList.getCompoundTagAt(index);
return BlockPosEU.readFromTag(tag.getCompoundTag(tagName));
}
return null;
}
public void toggleAlwaysRenderSelectedLocation(ItemStack rulerStack)
{
ItemStack moduleStack = this.getSelectedModuleStack(rulerStack, ModuleType.TYPE_MEMORY_CARD_MISC);
if (moduleStack.isEmpty() == false)
{
int selected = this.getLocationSelection(rulerStack);
NBTTagList tagList = NBTUtils.getTagList(moduleStack, TAG_WRAPPER, TAG_LOCATIONS, Constants.NBT.TAG_COMPOUND, true);
NBTTagCompound tag = tagList.getCompoundTagAt(selected);
NBTUtils.toggleBoolean(tag, TAG_RENDER_WITH_ALL);
if (selected >= tagList.tagCount())
{
tagList = NBTUtils.insertToTagList(tagList, tag, selected);
}
else
{
tagList.set(selected, tag);
}
NBTUtils.setTagList(moduleStack, TAG_WRAPPER, TAG_LOCATIONS, tagList);
this.setSelectedModuleStack(rulerStack, ModuleType.TYPE_MEMORY_CARD_MISC, moduleStack);
}
}
public int getCapacityCached(ItemStack stack, EntityPlayer player)
{
if (LinkMode.fromStack(stack) == LinkMode.ENABLED)
{
ItemStack moduleStack = this.getSelectedModuleStack(stack, ModuleType.TYPE_LINKCRYSTAL);
if (moduleStack.isEmpty() == false)
{
NBTTagCompound moduleNbt = moduleStack.getTagCompound();
if (moduleNbt != null && moduleNbt.hasKey("CapacityCached", Constants.NBT.TAG_INT))
{
return moduleNbt.getInteger("CapacityCached");
}
}
return 0; // without this the client side code would try to read the tank info, and then crash
}
return this.getCapacity(stack, player);
}
@Override
public ItemBlock createItemBlock()
{
ItemBlockPlacementProperty item = new ItemBlockPlacementProperty(this);
String[] names = new String[] { "extend", "retract", "toggle" };
item.addPlacementProperty(0, "drawbridge.delay", Constants.NBT.TAG_BYTE, 1, 255);
item.addPlacementProperty(0, "drawbridge.length", Constants.NBT.TAG_BYTE, 1, TileEntityDrawbridge.MAX_LENGTH_NORMAL);
item.addPlacementProperty(0, "drawbridge.redstone_mode", Constants.NBT.TAG_BYTE, 0, 2);
item.addPlacementPropertyValueNames(0, "drawbridge.redstone_mode", names);
item.addPlacementProperty(1, "drawbridge.delay", Constants.NBT.TAG_BYTE, 1, 255);
item.addPlacementProperty(1, "drawbridge.length", Constants.NBT.TAG_BYTE, 1, TileEntityDrawbridge.MAX_LENGTH_ADVANCED);
item.addPlacementProperty(1, "drawbridge.redstone_mode", Constants.NBT.TAG_BYTE, 0, 2);
item.addPlacementPropertyValueNames(1, "drawbridge.redstone_mode", names);
return item;
}
/**
* Returns the rarity of a given Skyblock item
*
* @param item the Skyblock item to check
* @return the rarity of the item if a valid rarity is found, {@code INVALID} if no rarity is found, {@code null} if item is {@code null}
*/
public static Rarity getRarity(ItemStack item) {
if (item == null || !item.hasTagCompound()) {
return null;
}
NBTTagCompound display = item.getSubCompound("display", false);
if (display == null || !display.hasKey("Lore")) {
return null;
}
NBTTagList lore = display.getTagList("Lore", Constants.NBT.TAG_STRING);
// Determine the item's rarity
for (int i = 0; i < lore.tagCount(); i++) {
String currentLine = lore.getStringTagAt(i);
for (Rarity rarity : EnumSet.allOf(Rarity.class)) {
if (currentLine.startsWith(rarity.getTag())) {
return rarity;
}
}
}
// If the item doesn't have a valid rarity, return null
return null;
}
@Override
public void handleUpdateTag(NBTTagCompound tag)
{
short mask = tag.getShort("msk");
this.setCreative((mask & 0x8000) != 0);
this.itemHandlerLockable.setSlotLocked(0, (mask & 0x4000) != 0);
this.hasStructureUpgrade = (mask & 0x0800) != 0;
this.hasVoidUpgrade = (mask & 0x0400) != 0;
this.setLabelsFromMask(mask & 0xFF);
if (tag.hasKey("stl", Constants.NBT.TAG_COMPOUND))
{
this.itemHandlerLockable.setTemplateStackInSlot(0, new ItemStack(tag.getCompoundTag("stl")));
}
if (tag.hasKey("st", Constants.NBT.TAG_COMPOUND))
{
this.itemHandlerLockable.setStackInSlot(0, NBTUtils.loadItemStackFromTag(tag.getCompoundTag("st")));
}
this.setMaxStacks(tag.getInteger("mxs"));
this.updateCachedStack();
super.handleUpdateTag(tag);
}
private void readFromNBT(NBTTagCompound nbt)
{
if (nbt == null || nbt.hasKey("PlacementProperties", Constants.NBT.TAG_LIST) == false)
{
return;
}
NBTTagList tagListPlayers = nbt.getTagList("PlacementProperties", Constants.NBT.TAG_COMPOUND);
int countPlayers = tagListPlayers.tagCount();
for (int playerIndex = 0; playerIndex < countPlayers; playerIndex++)
{
this.readAllDataForPlayerFromNBT(tagListPlayers.getCompoundTagAt(playerIndex));
}
}
public static PacketDurabilitySync decode(PacketBuffer buffer) {
CompoundNBT tag = buffer.readCompoundTag();
ListNBT nbtList = tag.getList("list", Constants.NBT.TAG_COMPOUND);
List<Tuple<BlockPos, Integer>> thisList = new ArrayList<>();
for (int i = 0; i < nbtList.size(); i++) {
CompoundNBT nbt = nbtList.getCompound(i);
thisList.add(new Tuple<>(NBTUtil.readBlockPos(nbt.getCompound("pos")), nbt.getInt("dur")));
}
return new PacketDurabilitySync(thisList);
}