Skip to content

Commit

Permalink
add recipes sharable
Browse files Browse the repository at this point in the history
  • Loading branch information
nicegamer7 committed May 18, 2020
1 parent 11f984b commit f3f070f
Showing 1 changed file with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import com.onarandombox.multiverseinventories.profile.PlayerProfile;
import com.onarandombox.multiverseinventories.util.MinecraftTools;
import org.bukkit.GameRule;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Statistic;
import org.bukkit.advancement.Advancement;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.potion.PotionEffect;

import java.util.Arrays;
Expand All @@ -26,6 +29,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
* The Sharables class is where all the default Sharable instances are located as constants as well as a factory class
Expand Down Expand Up @@ -499,7 +503,8 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
new SharableHandler<Location>() {
@Override
public void updateProfile(PlayerProfile profile, Player player) {
//profile.set(LAST_LOCATION, player.getLocation());
// we can't save last_location here! it's too late!
// instead, it's saved in the PlayerTeleportEvent listener.
}

@Override
Expand Down Expand Up @@ -660,6 +665,48 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
}
}).defaultSerializer(new ProfileEntry(false, "game_statistics")).altName("game_stats").build();

/**
* Sharing Recipes.
*/
public static final Sharable<Map> RECIPES = new Sharable.Builder<Map>("recipes", Map.class,
new SharableHandler<Map>() {
@Override
public void updateProfile(PlayerProfile profile, Player player) {
HashSet<NamespacedKey> recipes = new HashSet<>();
Iterator<Recipe> recipeIterator = inventories.getServer().recipeIterator();

while (recipeIterator.hasNext()) {
Recipe recipe = recipeIterator.next();
if (recipe instanceof Keyed) {
NamespacedKey key = ((Keyed) recipe).getKey();
if (player.undiscoverRecipe(key)) recipes.add(key);
}
}

player.discoverRecipes(recipes);
profile.set(RECIPES, recipes.stream().collect(Collectors.groupingBy(NamespacedKey::getNamespace, Collectors.mapping(NamespacedKey::getKey, Collectors.toList()))));
}

@Override
public boolean updatePlayer(Player player, PlayerProfile profile) {
Map<String, List<String>> recipes = profile.get(RECIPES);
recipes = (recipes != null) ? recipes : new HashMap<>();
Iterator<Recipe> recipeIterator = inventories.getServer().recipeIterator();

while (recipeIterator.hasNext()) {
Recipe recipe = recipeIterator.next();
if (recipe instanceof Keyed) {
NamespacedKey key = ((Keyed) recipe).getKey();
if (recipes.containsKey(key.getNamespace()) && recipes.get(key.getNamespace()).contains(key.getKey())) {
player.discoverRecipe(key);
} else player.undiscoverRecipe(key);
}
}

return !recipes.isEmpty();
}
}).defaultSerializer(new ProfileEntry(false, "recipes")).build();

/**
* Grouping for inventory sharables.
*/
Expand Down Expand Up @@ -704,7 +751,7 @@ public boolean updatePlayer(Player player, PlayerProfile profile) {
public static final SharableGroup ALL_DEFAULT = new SharableGroup("all", fromSharables(HEALTH, ECONOMY,
FOOD_LEVEL, SATURATION, EXHAUSTION, EXPERIENCE, TOTAL_EXPERIENCE, LEVEL, INVENTORY, ARMOR, BED_SPAWN,
MAXIMUM_AIR, REMAINING_AIR, FALL_DISTANCE, FIRE_TICKS, POTIONS, LAST_LOCATION, ENDER_CHEST, OFF_HAND,
ADVANCEMENTS, GAME_STATISTICS),
ADVANCEMENTS, GAME_STATISTICS, RECIPES),
"*", "everything");


Expand Down

0 comments on commit f3f070f

Please sign in to comment.