Skip to content

Commit

Permalink
Merge pull request #14 from OnapleRPG/default-drop-cumulative-issue-12
Browse files Browse the repository at this point in the history
Allow cumulative drop between default, type, ref, and pool.
  • Loading branch information
zessirb authored Jul 18, 2020
2 parents aa4822b + 2af82ef commit 5bce886
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 54 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,8 @@ build
production
logs
.mixin.out
bin
.classpath
.project
.settings
.vscode
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ It contains a list of the resources that will drop as is on a vanilla server.
```
default = [
"minecraft:dirt",
"minecraft:wood"
"minecraft:stone"
]
harvest_items = [
{
Expand All @@ -86,9 +86,8 @@ harvest_items = [
}
]
```
_Following the above example, dirt and wood are going to drop their respective items, whereas diorite stone will drop a
cobblestone block, the item number 2 of Itemizer, and an item from the first Itemizer pool. Note that we could have
writen only one or two of the three item fetchers._
_Following the above example, dirt and stone are going to drop their default drop, but diorite stone will drop a
cobblestone block, the item number 2 of Itemizer, and an item from the first Itemizer pool, as well as its default drop (since diorite is a type of stone)._

### Reload command
There is a command that reloads the configuration files without needing to restart the server : **/harvester reload**.
Expand Down
18 changes: 5 additions & 13 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,18 @@ plugins {
apply plugin: 'java'
apply plugin: 'maven-publish'
apply plugin: 'org.spongepowered.plugin'

apply plugin: 'eu.davidea.grabver'


versioning {
// required (number)
major = 1
minor = 1
patch = 1
minor = 2
patch = 0

}

version= versioning.name+'-'+versioning.build

apply plugin: 'net.minecraftforge.gradle.forge'

minecraft {
forgeVersion = '1.12.2-14.23.4.2704'
mappings = "snapshot_20180808"
}
// apply plugin: 'net.minecraftforge.gradle.forge'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand All @@ -66,7 +58,7 @@ publishing {
maven(MavenPublication) {
groupId 'com.ylinor'
artifactId 'harvester'
version '0.1'
version '1.2'

from components.java
}
Expand Down Expand Up @@ -94,4 +86,4 @@ sonarqube {
property "sonar.organization", "ylinor-github"
property "sonar.host.url", "https://sonarcloud.io"
}
}
}
53 changes: 27 additions & 26 deletions src/main/java/com/onaple/harvester/HarvestListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,43 +73,44 @@ public void onDropItemEvent(DropItemEvent.Destruct event){
Object source = event.getSource();
if(optionalPlayerCause.isPresent()) {
Player player = optionalPlayerCause.get();
List<String> defaultDrops = ConfigurationHandler.getHarvestDefaultDropList();
if (!event.getEntities().stream().anyMatch(entity -> defaultDrops.contains(entity.get(Keys.REPRESENTED_ITEM).get().getType().getName().toString()))) {
event.getEntities().clear();
}
if (source instanceof BlockSnapshot) {
BlockSnapshot blockSnapshot = (BlockSnapshot) source;
Optional<HarvestDropBean> optionalHarvestable = DropUtil.identifyHarvestDrop(blockSnapshot.getState());
if (optionalHarvestable.isPresent()) {
event.getEntities().clear();
HarvestDropBean harvestable = optionalHarvestable.get();

Optional<ItemStack> dropOptional = DropUtil.getConfiguredDrop(harvestable);
if(dropOptional.isPresent()){
event.getEntities().add(DropUtil.getItemStackEntity(player.getLocation(), dropOptional.get()));
} else {
Harvester.getLogger().warn("Item not found");
}
List<Optional<ItemStack>> drops = DropUtil.getConfiguredDrops(harvestable);
drops.forEach(dropOptional -> {
if(dropOptional.isPresent()){
event.getEntities().add(DropUtil.getItemStackEntity(player.getLocation(), dropOptional.get()));
} else {
Harvester.getLogger().warn("Item not found");
}
});
}
} else {
List<String> defaultDrops = ConfigurationHandler.getHarvestDefaultDropList();
event.filterEntities(entity -> !defaultDrops.contains(entity.get(Keys.REPRESENTED_ITEM).get().toString()));
}
}
}

/**
/**
* Return harvestable if present in configuration
* @param blockState Block to identify
* @return Optional of harvestable
*/
private Optional<HarvestableBean> identifyHarvestable(BlockState blockState) {
String blockTypeName = blockState.getType().getName().trim();
List<HarvestableBean> harvestables = ConfigurationHandler.getHarvestableList();
for (HarvestableBean harvestable: harvestables) {
if (harvestable.getType().trim().equals(blockTypeName)) {
boolean statesMatch = DropUtil.blockHasTraits(harvestable.getStates(), blockState);
if (statesMatch) {
return Optional.of(harvestable);
}
}
}
return Optional.empty();
}
*/
private Optional<HarvestableBean> identifyHarvestable(BlockState blockState) {
String blockTypeName = blockState.getType().getName().trim();
List<HarvestableBean> harvestables = ConfigurationHandler.getHarvestableList();
for (HarvestableBean harvestable: harvestables) {
if (harvestable.getType().trim().equals(blockTypeName)) {
boolean statesMatch = DropUtil.blockHasTraits(harvestable.getStates(), blockState);
if (statesMatch) {
return Optional.of(harvestable);
}
}
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.onaple.harvester.data.beans;

import java.util.Map;
import java.util.Optional;

public class HarvestDropBean {
/**
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/onaple/harvester/utils/DropUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.spongepowered.api.world.Location;
import org.spongepowered.api.world.extent.Extent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -29,12 +30,12 @@ public class DropUtil {
* @param harvestDropBean
* @return the matching itemStack by id,pool or name
*/
public static Optional<ItemStack> getConfiguredDrop(HarvestDropBean harvestDropBean) {

public static List<Optional<ItemStack>> getConfiguredDrops(HarvestDropBean harvestDropBean) {
List<Optional<ItemStack>> drops = new ArrayList<>();
if (harvestDropBean.getName() != null && !harvestDropBean.getName().isEmpty()) {
Optional<ItemType> optionalType = Sponge.getRegistry().getType(ItemType.class, harvestDropBean.getName());
if (optionalType.isPresent()) {
return Optional.of(ItemStack.builder().itemType(optionalType.get()).build());
drops.add(Optional.of(ItemStack.builder().itemType(optionalType.get()).build()));
}
}
try {
Expand All @@ -44,20 +45,20 @@ public static Optional<ItemStack> getConfiguredDrop(HarvestDropBean harvestDropB
if (harvestDropBean.getItemRef() != null) {
Optional<ItemStack> refItem = iItemService.retrieve(harvestDropBean.getItemRef());
if (refItem.isPresent()) {
return Optional.of(refItem.get());
drops.add(Optional.of(refItem.get()));
}
}
if (harvestDropBean.getPoolRef() != null) {
Optional<ItemStack> poolItem = iItemService.fetch(harvestDropBean.getPoolRef());
if (poolItem.isPresent()) {
return Optional.of(poolItem.get());
drops.add(Optional.of(poolItem.get()));
}
}
}
} catch (NoClassDefFoundError e) {
Harvester.getLogger().error("Could not contact Itemizer plugin : " + e.getMessage());
}
return Optional.empty();
return drops;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions version.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Tue May 21 23:09:54 CEST 2019
#Sun Jul 19 01:02:08 CEST 2020
MAJOR=1
MINOR=1
PATCH=1
MINOR=2
PATCH=0
PRE_RELEASE=
BUILD=70
BUILD=82
CODE=0

0 comments on commit 5bce886

Please sign in to comment.