Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements/composter dirt #3319

Merged
merged 6 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.block.BlockRedstoneOre;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
Expand All @@ -26,13 +27,11 @@
import net.minecraftforge.oredict.OreDictionary;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static com.minecolonies.api.util.constant.Constants.ONE_HUNDRED_PERCENT;
import static com.minecolonies.api.util.constant.Constants.ORE_STRING;
import static com.minecolonies.api.util.constant.Constants.SAPLINGS;
import static com.minecolonies.api.util.constant.NbtTagConstants.TAG_SAP_LEAF;
Expand Down Expand Up @@ -64,11 +63,21 @@ public class CompatibilityManager implements ICompatibilityManager
*/
private final List<ItemStorage> compostableItems = new ArrayList<>();

/**
* List of lucky ores which get dropped by the miner.
*/
private final List<ItemStorage> luckyOres = new ArrayList<>();

/**
* If discovery is finished already.
*/
private boolean discoveredAlready = false;

/**
* Random obj.
*/
private static final Random random = new Random();

/**
* Instantiates the compatibilityManager.
*/
Expand All @@ -90,6 +99,7 @@ public void discover()
}
Log.getLogger().info("Finished discovering ores");
discoverCompostableItems();
discoverLuckyOres();

discoveredAlready = true;
}
Expand Down Expand Up @@ -119,6 +129,31 @@ public boolean isCompost(final ItemStack itemStack)
return false;
}

@Override
public boolean isLuckyBlock(final ItemStack itemStack)
{
if (itemStack.isEmpty())
{
return false;
}

for (final String string : Configurations.gameplay.luckyBlocks)
{
if (itemStack.getItem().getRegistryName().toString().equals(string))
{
return true;
}
for (final int id : OreDictionary.getOreIDs(itemStack))
{
if (OreDictionary.getOreName(id).equals(string))
{
return true;
}
}
}
return false;
}

@Override
public IBlockState getLeafForSapling(final ItemStack stack)
{
Expand Down Expand Up @@ -216,6 +251,17 @@ public boolean isDiscoveredAlready()
return discoveredAlready;
}

@Override
public ItemStack getRandomLuckyOre()
{
if (random.nextInt(ONE_HUNDRED_PERCENT) <= Configurations.gameplay.luckyBlockChance)
{
Collections.shuffle(luckyOres);
return luckyOres.get(0).getItemStack().copy();
}
return ItemStack.EMPTY;
}

//------------------------------- Private Utility Methods -------------------------------//

private void discoverOres(final String string)
Expand Down Expand Up @@ -308,6 +354,47 @@ private void discoverCompostableItems()
Log.getLogger().info("Finished discovering compostables");
}

/**
* Run through all blocks and check if they match one of our lucky ores.
*/
private void discoverLuckyOres()
{
if (luckyOres.isEmpty())
{
for (final String ore : Configurations.gameplay.luckyOres)
{
final String[] split = ore.split("!");
if (split.length < 2)
{
Log.getLogger().warn("Wrong configured ore: " + ore);
continue;
}

final Item item = Item.getByNameOrId(split[0]);
if (item == null || item == Items.AIR)
{
Log.getLogger().warn("Invalid lucky block: " + ore);
continue;
}

final ItemStack stack = new ItemStack(item, 1);
try
{
final int rarity = Integer.parseInt(split[1]);
for (int i = 0; i < rarity; i++)
{
luckyOres.add(new ItemStorage(stack));
}
}
catch (final NumberFormatException ex)
{
Log.getLogger().warn("Ore has invalid rarity: " + ore);
}
}
}
Log.getLogger().info("Finished discovering lucky ores");
}

private static NBTTagCompound writeLeafSaplingEntryToNBT(final IBlockState state, final ItemStorage storage)
{
final NBTTagCompound compound = new NBTTagCompound();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,17 @@ public interface ICompatibilityManager
* @return true if so.
*/
boolean isDiscoveredAlready();

/**
* If an itemStack is a lucky block which can result in an extra ore drop.
* @param itemStack the stack to check.
* @return true if so.
*/
boolean isLuckyBlock(final ItemStack itemStack);

/**
* Get a random lucky ore from a luckyblock.
* @return the lucky ore.
*/
ItemStack getRandomLuckyOre();
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,10 @@ public static class Gameplay

@Config.Comment("Should players be allowed to build their colonies over existing villages?")
public boolean protectVillages = false;
/* schematics usage */
@Config.Comment("Should the default schematics be ignored (from the jar)?")
public boolean ignoreSchematicsFromJar = false;

@Config.Comment("Should player made schematics be allowed")
public boolean allowPlayerSchematics = false;

@Config.Comment("Max amount of schematics to be cached on the server")
public int maxCachedSchematics = 100;

@Config.Comment("Should players be allowed to change names? -1 for false, 0 for specific groups, 1 for true")
public int allowGlobalNameChanges = 1;

Expand Down Expand Up @@ -189,9 +183,6 @@ public static class Gameplay
@Config.Comment("Min distance from world spawn")
public int minDistanceFromWorldSpawn = 512;

@Config.Comment("Should the dman create resources out of hot air (Not implemented)")
public boolean deliverymanInfiniteResources = false;

@Config.Comment("Amount of initial citizens")
public int maxCitizens = 4;

Expand Down Expand Up @@ -272,6 +263,31 @@ public static class Gameplay

@Config.Comment("AI Update rate, increase to improve performance. Default: 1")
public int updateRate = 1;

@Config.Comment("Quantity of dirt per Compost filling. Default: 1")
public int dirtFromCompost = 1;

@Config.Comment("Chance to get a lucky block in percent. Default: 1")
public int luckyBlockChance = 1;

@Config.Comment("The blocks where the miner has a chance to get a random ore.")
public String[] luckyBlocks = new String[]
{
"minecraft:stone",
"minecraft:cobblestone",
};

@Config.Comment("The random ores the miner can get separated by ! for rarity")
public String[] luckyOres = new String[]
{
"minecraft:coal_ore!64",
"minecraft:iron_ore!32",
"minecraft:gold_ore!16",
"minecraft:redstone_ore!8",
"minecraft:lapis_ore!4",
"minecraft:diamond_ore!2",
"minecraft:emerald_ore!1"
};
}

public static class Compatibility
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/minecolonies/coremod/MineColonies.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ private static synchronized void initializeNetwork()
getNetwork().registerMessage(RestartCitizenMessage.class, RestartCitizenMessage.class, ++id, Side.SERVER);
getNetwork().registerMessage(SortWarehouseMessage.class, SortWarehouseMessage.class, ++id, Side.SERVER);
getNetwork().registerMessage(PostBoxRequestMessage.class, PostBoxRequestMessage.class, ++id, Side.SERVER);
getNetwork().registerMessage(ComposterRetrievalMessage.class, ComposterRetrievalMessage.class, ++id, Side.SERVER);

//Client side only
getNetwork().registerMessage(BlockParticleEffectMessage.class, BlockParticleEffectMessage.class, ++id, Side.CLIENT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,40 @@

import com.minecolonies.api.crafting.ItemStorage;
import com.minecolonies.api.util.LanguageHandler;
import com.minecolonies.api.util.constant.Constants;
import com.minecolonies.blockout.controls.Button;
import com.minecolonies.blockout.controls.ButtonImage;
import com.minecolonies.blockout.views.View;
import com.minecolonies.coremod.MineColonies;
import com.minecolonies.coremod.colony.ColonyManager;
import com.minecolonies.coremod.colony.buildings.workerbuildings.BuildingComposter;
import com.minecolonies.coremod.items.ModItems;
import com.minecolonies.coremod.network.messages.ComposterRetrievalMessage;
import net.minecraft.init.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;

import java.util.Collection;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import static com.minecolonies.coremod.client.gui.WindowTownHall.BLACK;

/**
* Composter window class. Specifies the extras the composter has for its list.
*/
public class WindowHutComposter extends WindowFilterableList<BuildingComposter.View>
{
/**
* Id of the button to toggle replant of saplings
*/
private static final String BUTTON_TOGGLE_RETRIEVE_DIRT = "retrieveDirt";

/**
* View containing the list.
*/
private static final String PAGE_ITEMS_VIEW = "pageItems";

/**
* Constructor for the window of the worker building.
*
Expand All @@ -31,6 +52,50 @@ public Collection<? extends ItemStorage> getBlockList(final Predicate<ItemStack>
return ColonyManager.getCompatibilityManager().getCopyOfCompostableItems().stream().filter(storage -> filterPredicate.test(storage.getItemStack())).collect(Collectors.toList());
}

@Override
public void onOpened()
{
super.onOpened();
final ButtonImage button = new ButtonImage();
button.setImage(new ResourceLocation(Constants.MOD_ID, "textures/gui/builderhut/builder_button_medium.png"));
button.setPosition(50, 193);
button.setSize(86, 17);
button.setID(BUTTON_TOGGLE_RETRIEVE_DIRT);
button.setTextColor(BLACK);
setupRetrieveDirtButton(button);

findPaneOfTypeByID(PAGE_ITEMS_VIEW, View.class).addChild(button);
registerButton(BUTTON_TOGGLE_RETRIEVE_DIRT, this::switchReplant);
}

/**
* Setup replant button with correct string.
*
* @param button the button to setup.
*/
private void setupRetrieveDirtButton(final Button button)
{
if (((BuildingComposter.View) building).retrieveDirtFromCompostBin)
{
button.setLabel(LanguageHandler.format(Blocks.DIRT.getLocalizedName()));
}
else
{
button.setLabel(LanguageHandler.format(ModItems.compost.getItemStackDisplayName(new ItemStack(ModItems.compost))));
}
}

/**
* Method to send the message to switch the toggle to the server, then updates button
*/
private void switchReplant(final Button retrieve)
{
final BuildingComposter.View composter = (BuildingComposter.View) building;
composter.retrieveDirtFromCompostBin = !composter.retrieveDirtFromCompostBin;
setupRetrieveDirtButton(retrieve);
MineColonies.getNetwork().sendToServer(new ComposterRetrievalMessage(composter, composter.retrieveDirtFromCompostBin));
}

@Override
public String getBuildingName()
{
Expand Down
Loading