Skip to content

Commit

Permalink
Refactor replaceAllOccurences & add progress bar when applying actions (
Browse files Browse the repository at this point in the history
#1144)

* refactor replaceAllOccurences

* remove timer

* don't replace shapeless recipes' ingredient to null

* add domain to modified recipe name

* add progress bar while apply actions

* fix

* fix

* fix

* clear after replace all occurences action is applied.

* upper case

* add IIngredientPredicate to determine toReplace

* remove unused field

* A or B -> C or B

* a little change

* clear comment

* replace with null in shapeless recipes -> remove the ingredient

* describe sub actions

* log exception

* override IBlockEvent#getBlock in changing block events

* add break in checkShapelessNulls

* code style change

* wrong method name

* move class

* if there is a `recipes.remove` wants to remove origin recipe, don't replace the recipe
  • Loading branch information
friendlyhj committed Feb 7, 2021
1 parent 31acd69 commit 41c43b7
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 168 deletions.
Expand Up @@ -3,6 +3,7 @@
import crafttweaker.*;
import crafttweaker.annotations.*;
import crafttweaker.api.network.NetworkSide;
import crafttweaker.api.recipes.ICraftingRecipe;
import crafttweaker.mc1120.brewing.MCBrewing;
import crafttweaker.mc1120.client.MCClient;
import crafttweaker.mc1120.commands.CTChatCommand;
Expand Down Expand Up @@ -154,22 +155,34 @@ public void onPreInitialization(FMLPreInitializationEvent ev) {
@EventHandler
public void onPostInit(FMLPostInitializationEvent ev) {
MinecraftForge.EVENT_BUS.post(new ActionApplyEvent.Pre());
try {
MCRecipeManager.recipes = ForgeRegistries.RECIPES.getEntries();
CraftTweakerAPI.apply(MCRecipeManager.actionRemoveRecipesNoIngredients);
MCRecipeManager.recipesToRemove.forEach(CraftTweakerAPI::apply);
MCRecipeManager.recipesToAdd.forEach(CraftTweakerAPI::apply);
MCFurnaceManager.recipesToRemove.forEach(CraftTweakerAPI::apply);
MCFurnaceManager.recipesToAdd.forEach(CraftTweakerAPI::apply);
LATE_ACTIONS.forEach(CraftTweakerAPI::apply);
MCRecipeManager.recipes = ForgeRegistries.RECIPES.getEntries();

//Cleanup
MCRecipeManager.cleanUpRecipeList();
} catch(Exception e) {
CraftTweaker.LOG.catching(e);
CraftTweakerAPI.logError("Error while applying actions", e);
MCRecipeManager.recipes = ForgeRegistries.RECIPES.getEntries();
if (MCRecipeManager.ActionReplaceAllOccurences.INSTANCE.hasSubAction()) {
MCRecipeManager.ActionReplaceAllOccurences.INSTANCE.describeSubActions();
List<ICraftingRecipe> recipes = CraftTweakerAPI.recipes.getAll();
ProgressManager.ProgressBar progressBar = ProgressManager.push("Applying replace all occurences action", recipes.size());
for (ICraftingRecipe recipe : recipes) {
try {
progressBar.step(recipe.getFullResourceName());
MCRecipeManager.ActionReplaceAllOccurences.INSTANCE.setCurrentModifiedRecipe(recipe);
MCRecipeManager.ActionReplaceAllOccurences.INSTANCE.apply();
} catch (Exception e) {
CraftTweaker.LOG.catching(e);
CraftTweakerAPI.logError("Fail to apply replace all occurence action at recipe " + recipe.getFullResourceName(), e);
}
}
ProgressManager.pop(progressBar);
}
applyActions(Collections.singletonList(MCRecipeManager.actionRemoveRecipesNoIngredients), "applying action remove recipes without ingredients", "fail to apply recipes without ingredient");
applyActions(MCRecipeManager.recipesToRemove, "Applying remove recipe actions", "Fail to apply remove recipe actions");
applyActions(MCRecipeManager.recipesToAdd, "Applying add recipe actions", "Fail to apply add recipe actions");
applyActions(MCFurnaceManager.recipesToRemove, "Applying remove furnace recipe actions", "Fail to apply remove furnace recipe actions");
applyActions(MCFurnaceManager.recipesToAdd, "Applying add furnace recipe actions", "Fail to apply add furnace recipe actions");
applyActions(LATE_ACTIONS, "Applying late actions", "Fail to apply late actions");
MCRecipeManager.recipes = ForgeRegistries.RECIPES.getEntries();
MCRecipeManager.ActionReplaceAllOccurences.INSTANCE.clear();

//Cleanup
MCRecipeManager.cleanUpRecipeList();
MinecraftForge.EVENT_BUS.post(new ActionApplyEvent.Post());
}

Expand Down Expand Up @@ -228,4 +241,20 @@ public void onServerStopped(FMLServerStoppedEvent ev) {
CrafttweakerImplementationAPI.setScriptProvider(scriptsGlobal);
server = null;
}

private void applyActions(List<? extends IAction> actions, String applyingMessage, String errorMessage) {
if (!actions.isEmpty()) {
ProgressManager.ProgressBar progressBar = ProgressManager.push(applyingMessage, actions.size());
actions.forEach(action -> {
progressBar.step(action.describe());
try {
CraftTweakerAPI.apply(action);
} catch (Exception e) {
CraftTweaker.LOG.catching(e);
CraftTweakerAPI.logError(errorMessage + " at action " + action.describe(), e);
}
});
ProgressManager.pop(progressBar);
}
}
}
@@ -1,5 +1,6 @@
package crafttweaker.mc1120.events.handling;

import crafttweaker.api.block.IBlock;
import crafttweaker.api.event.BlockBreakEvent;
import crafttweaker.api.minecraft.CraftTweakerMC;
import crafttweaker.api.player.IPlayer;
Expand All @@ -26,6 +27,11 @@ public IPlayer getPlayer() {
return CraftTweakerMC.getIPlayer(event.getPlayer());
}

@Override
public IBlock getBlock() {
return getBlockState().getBlock();
}

@Override
public int getExperience() {
return event.getExpToDrop();
Expand Down
@@ -1,5 +1,6 @@
package crafttweaker.mc1120.events.handling;

import crafttweaker.api.block.IBlock;
import crafttweaker.api.event.BlockHarvestDropsEvent;
import crafttweaker.api.item.WeightedItemStack;
import crafttweaker.api.minecraft.CraftTweakerMC;
Expand Down Expand Up @@ -34,6 +35,11 @@ public int getFortuneLevel() {
return event.getFortuneLevel();
}

@Override
public IBlock getBlock() {
return getBlockState().getBlock();
}

@Override
public List<WeightedItemStack> getDrops() {
return CraftTweakerMC.getWeightedItemStackList(event.getDrops());
Expand Down
@@ -1,5 +1,6 @@
package crafttweaker.mc1120.events.handling;

import crafttweaker.api.block.IBlock;
import crafttweaker.api.block.IBlockState;
import crafttweaker.api.event.BlockPlaceEvent;
import crafttweaker.api.minecraft.CraftTweakerMC;
Expand Down Expand Up @@ -29,6 +30,11 @@ public IBlockState getPlacedAgainst() {
return CraftTweakerMC.getBlockState(event.getPlacedAgainst());
}

@Override
public IBlock getBlock() {
return getBlockState().getBlock();
}

@Override
public String getHand() {
return String.valueOf(event.getHand());
Expand Down

0 comments on commit 41c43b7

Please sign in to comment.