Skip to content

Commit 072fd0b

Browse files
authored
Let ItemsAdder be a new OneBlockCustomBlock (#359)
* Let ItemsAdder be a new OneBlockCustomBlock * fix conflict
1 parent 7ddd9a4 commit 072fd0b

File tree

8 files changed

+51
-85
lines changed

8 files changed

+51
-85
lines changed

src/main/java/world/bentobox/aoneblock/AOneBlock.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import world.bentobox.aoneblock.listeners.ItemsAdderListener;
2525
import world.bentobox.aoneblock.listeners.JoinLeaveListener;
2626
import world.bentobox.aoneblock.listeners.NoBlockHandler;
27+
import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlockCreator;
2728
import world.bentobox.aoneblock.oneblocks.OneBlocksManager;
29+
import world.bentobox.aoneblock.oneblocks.customblock.ItemsAdderCustomBlock;
2830
import world.bentobox.aoneblock.requests.IslandStatsHandler;
2931
import world.bentobox.aoneblock.requests.LocationStatsHandler;
3032
import world.bentobox.bentobox.api.addons.GameModeAddon;
@@ -57,6 +59,8 @@ public void onLoad() {
5759
// Check if ItemsAdder exists, if yes register listener
5860
if (Bukkit.getPluginManager().getPlugin("ItemsAdder") != null) {
5961
registerListener(new ItemsAdderListener(this));
62+
OneBlockCustomBlockCreator.register(ItemsAdderCustomBlock::fromId);
63+
OneBlockCustomBlockCreator.register("itemsadder", ItemsAdderCustomBlock::fromMap);
6064
hasItemsAdder = true;
6165
}
6266
// Save the default config from config.yml

src/main/java/world/bentobox/aoneblock/listeners/BlockListener.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.eclipse.jdt.annotation.NonNull;
4242
import org.eclipse.jdt.annotation.Nullable;
4343

44-
import dev.lone.itemsadder.api.CustomBlock;
4544
import world.bentobox.aoneblock.AOneBlock;
4645
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
4746
import world.bentobox.aoneblock.events.MagicBlockEntityEvent;
@@ -435,13 +434,6 @@ private void breakBlock(@Nullable Player player, Block block, @NonNull OneBlockO
435434
private void spawnBlock(@NonNull OneBlockObject nextBlock, @NonNull Block block) {
436435
if (nextBlock.isCustomBlock()) {
437436
nextBlock.getCustomBlock().execute(addon, block);
438-
} else if (nextBlock.isItemsAdderBlock()) {
439-
// Get Custom Block from ItemsAdder and place it
440-
CustomBlock cBlock = CustomBlock.getInstance(nextBlock.getItemsAdderBlock());
441-
if (cBlock != null) {
442-
block.getLocation().getBlock().setType(Material.AIR);
443-
cBlock.place(block.getLocation());
444-
}
445437
} else {
446438
@NonNull
447439
Material type = nextBlock.getMaterial();

src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockObject.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public enum Rarity {
4242
private Map<Integer, ItemStack> chest;
4343
private Rarity rarity;
4444
private OneBlockCustomBlock customBlock;
45-
private String itemsAdderBlock;
4645
private int prob;
4746

4847
/**
@@ -78,18 +77,6 @@ public OneBlockObject(OneBlockCustomBlock customBlock, int prob) {
7877
this.prob = prob;
7978
}
8079

81-
/**
82-
* An ItemsAdder block
83-
*
84-
* @param namedSpaceID - ItemsAdder block
85-
* @param prob - relative probability
86-
*/
87-
88-
public OneBlockObject(String namedSpaceID, int prob) {
89-
this.itemsAdderBlock = namedSpaceID;
90-
this.prob = prob;
91-
}
92-
9380
/**
9481
* A chest
9582
*
@@ -113,7 +100,6 @@ public OneBlockObject(OneBlockObject ob) {
113100
this.rarity = ob.getRarity();
114101
this.prob = ob.getProb();
115102
this.customBlock = ob.getCustomBlock();
116-
this.itemsAdderBlock = ob.getItemsAdderBlock();
117103
}
118104

119105
/**
@@ -147,11 +133,6 @@ public OneBlockCustomBlock getCustomBlock() {
147133
return customBlock;
148134
}
149135

150-
/**
151-
* @return the itemsAdderBlock
152-
*/
153-
public String getItemsAdderBlock() { return itemsAdderBlock; }
154-
155136

156137
/**
157138
* @return the isMaterial
@@ -176,13 +157,6 @@ public boolean isCustomBlock() {
176157
return customBlock != null;
177158
}
178159

179-
/**
180-
* @return the isItemsAdderBlock
181-
*/
182-
public boolean isItemsAdderBlock() {
183-
return itemsAdderBlock != null;
184-
}
185-
186160
/**
187161
* @return the rarity
188162
*/

src/main/java/world/bentobox/aoneblock/oneblocks/OneBlockPhase.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -165,17 +165,6 @@ public void addCustomBlock(OneBlockCustomBlock customBlock, int prob) {
165165
probMap.put(total, new OneBlockObject(customBlock, prob));
166166
}
167167

168-
/**
169-
* Adds a ItemsAdder's custom block and associated probability
170-
* @param namedSpaceID - name space and ID
171-
* @param prob - probability
172-
*/
173-
public void addItemsAdderCustomBlock(String namedSpaceID, int prob) {
174-
total += prob;
175-
blockTotal += prob;
176-
probMap.put(total, new OneBlockObject(namedSpaceID, prob));
177-
}
178-
179168
/**
180169
* Adds an entity type and associated probability
181170
*

src/main/java/world/bentobox/aoneblock/oneblocks/OneBlocksManager.java

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@
3131
import com.google.common.base.Enums;
3232
import com.google.common.io.Files;
3333

34-
import dev.lone.itemsadder.api.CustomBlock;
35-
import dev.lone.itemsadder.api.ItemsAdder;
3634
import world.bentobox.aoneblock.AOneBlock;
3735
import world.bentobox.aoneblock.dataobjects.OneBlockIslands;
3836
import world.bentobox.aoneblock.oneblocks.OneBlockObject.Rarity;
@@ -489,22 +487,7 @@ void addBlocks(OneBlockPhase obPhase, ConfigurationSection phase) {
489487
if (phase.isConfigurationSection(BLOCKS)) {
490488
ConfigurationSection blocks = phase.getConfigurationSection(BLOCKS);
491489
for (String material : blocks.getKeys(false)) {
492-
if (Material.getMaterial(material) != null) {
493-
addMaterial(obPhase, material, Objects.toString(blocks.get(material)));
494-
} else {
495-
if (addon.hasItemsAdder()) {
496-
CustomBlock block = CustomBlock.getInstance(material);
497-
if (block != null) {
498-
addItemsAdderBlock(obPhase, material, Objects.toString(blocks.get(material)));
499-
} else if (ItemsAdder.getAllItems() != null) {
500-
if (ItemsAdder.getAllItems().size() != 0) {
501-
addon.logError("Bad block material in " + obPhase.getPhaseName() + ": " + material);
502-
}
503-
}
504-
} else {
505-
addon.logError("Bad block material in " + obPhase.getPhaseName() + ": " + material);
506-
}
507-
}
490+
addMaterial(obPhase, material, Objects.toString(blocks.get(material)));
508491
}
509492
} else if (phase.isList(BLOCKS)) {
510493
List<Map<?, ?>> blocks = phase.getMapList(BLOCKS);
@@ -559,22 +542,6 @@ private boolean addMaterial(OneBlockPhase obPhase, String material, String proba
559542
return true;
560543
}
561544

562-
private void addItemsAdderBlock(OneBlockPhase obPhase, String block, String probability) {
563-
int prob;
564-
try {
565-
prob = Integer.parseInt(probability);
566-
if (prob < 1) {
567-
addon.logWarning("Bad item weight for " + obPhase.getPhaseName() + ": " + block
568-
+ ". Must be positive number above 1.");
569-
} else {
570-
obPhase.addItemsAdderCustomBlock(block, prob);
571-
}
572-
} catch (Exception e) {
573-
addon.logError("Bad item weight for " + obPhase.getPhaseName() + ": " + block + ". Must be a number.");
574-
}
575-
576-
}
577-
578545
/**
579546
* Return the current phase for the block count
580547
*

src/main/java/world/bentobox/aoneblock/oneblocks/customblock/BlockDataCustomBlock.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package world.bentobox.aoneblock.oneblocks.customblock;
22

3-
import java.util.Map;
4-
import java.util.Objects;
5-
import java.util.Optional;
6-
73
import org.bukkit.Bukkit;
84
import org.bukkit.block.Block;
9-
105
import world.bentobox.aoneblock.AOneBlock;
116
import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlock;
127
import world.bentobox.bentobox.BentoBox;
138

9+
import java.util.Map;
10+
import java.util.Objects;
11+
import java.util.Optional;
12+
1413
/**
1514
* A custom block that is defined by a block data value.
1615
*
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package world.bentobox.aoneblock.oneblocks.customblock;
2+
3+
import dev.lone.itemsadder.api.CustomBlock;
4+
import org.bukkit.Material;
5+
import org.bukkit.block.Block;
6+
import world.bentobox.aoneblock.AOneBlock;
7+
import world.bentobox.aoneblock.oneblocks.OneBlockCustomBlock;
8+
import world.bentobox.bentobox.BentoBox;
9+
10+
import java.util.Map;
11+
import java.util.Objects;
12+
import java.util.Optional;
13+
14+
public class ItemsAdderCustomBlock implements OneBlockCustomBlock {
15+
private final CustomBlock customBlock;
16+
17+
public ItemsAdderCustomBlock(CustomBlock customBlock) {
18+
this.customBlock = customBlock;
19+
}
20+
21+
public static Optional<ItemsAdderCustomBlock> fromId(String id) {
22+
return Optional.ofNullable(CustomBlock.getInstance(id)).map(ItemsAdderCustomBlock::new);
23+
}
24+
25+
public static Optional<ItemsAdderCustomBlock> fromMap(Map<?, ?> map) {
26+
return Optional
27+
.ofNullable(Objects.toString(map.get("id"), null))
28+
.flatMap(ItemsAdderCustomBlock::fromId);
29+
}
30+
31+
@Override
32+
public void execute(AOneBlock addon, Block block) {
33+
try {
34+
block.setType(Material.AIR);
35+
customBlock.place(block.getLocation());
36+
} catch (Exception e) {
37+
BentoBox.getInstance().logError("Could not place custom block " + customBlock.getId() + " for block " + block.getType());
38+
block.setType(Material.STONE);
39+
}
40+
}
41+
}

src/main/java/world/bentobox/aoneblock/oneblocks/customblock/MobCustomBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static Optional<MobCustomBlock> fromMap(Map<?, ?> map) {
4242
EntityType entityType = maybeEntity(entityTypeValue);
4343
Material underlyingBlock = Material.getMaterial(underlyingBlockValue);
4444

45-
if(underlyingBlock == null){
45+
if (underlyingBlock == null) {
4646
BentoBox.getInstance().logWarning("Underlying block " + underlyingBlockValue + " does not exist and will be replaced with STONE.");
4747
}
4848

0 commit comments

Comments
 (0)