-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
983 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>me.autobot</groupId> | ||
<artifactId>ReSbCrafter-parent</artifactId> | ||
<version>1.0</version> | ||
</parent> | ||
<artifactId>common</artifactId> | ||
<version>${project.parent.version}</version> | ||
|
||
<properties> | ||
<maven.compiler.target>16</maven.compiler.target> | ||
<maven.compiler.source>16</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spigotmc-repo</id> | ||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<repository> | ||
<id>sonatype</id> | ||
<url>https://oss.sonatype.org/content/groups/public/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.17.1-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
</project> |
13 changes: 13 additions & 0 deletions
13
common/src/main/java/me/autobot/resbcrafter/IScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.autobot.resbcrafter; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.entity.Entity; | ||
|
||
public interface IScheduler { | ||
void globalTask(Runnable r); | ||
void regionTask(Runnable r, Location l); | ||
void entityTask(Runnable r, Entity e); | ||
void globalTaskDelayed(Runnable r, long d); | ||
void regionTaskDelayed(Runnable r, Location l, long d); | ||
void entityTaskDelayed(Runnable r, Entity e, long d); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<parent> | ||
<groupId>me.autobot</groupId> | ||
<artifactId>ReSbCrafter-parent</artifactId> | ||
<version>1.0</version> | ||
</parent> | ||
<artifactId>core</artifactId> | ||
<version>${project.parent.version}</version> | ||
|
||
<properties> | ||
<maven.compiler.target>16</maven.compiler.target> | ||
<maven.compiler.source>16</maven.compiler.source> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<repositories> | ||
<repository> | ||
<id>spigotmc-repo</id> | ||
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url> | ||
</repository> | ||
<repository> | ||
<id>sonatype</id> | ||
<url>https://oss.sonatype.org/content/groups/public/</url> | ||
</repository> | ||
</repositories> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.spigotmc</groupId> | ||
<artifactId>spigot-api</artifactId> | ||
<version>1.17.1-R0.1-SNAPSHOT</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>me.autobot</groupId> | ||
<artifactId>common</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>me.autobot</groupId> | ||
<artifactId>folia</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
45 changes: 45 additions & 0 deletions
45
core/src/main/java/me/autobot/resbcrafter/ReSbCrafter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package me.autobot.resbcrafter; | ||
|
||
import me.autobot.resbcrafter.listeners.CraftedItem; | ||
import me.autobot.resbcrafter.listeners.DiscoverRecipe; | ||
import me.autobot.resbcrafter.listeners.PrepareCraft; | ||
import me.autobot.resbcrafter.scheduler.BukkitTasks; | ||
import me.autobot.resbcrafter.scheduler.FoliaTasks; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.plugin.PluginManager; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
public final class ReSbCrafter extends JavaPlugin { | ||
|
||
public static IScheduler scheduler; | ||
public static Plugin plugin; | ||
@Override | ||
public void onEnable() { | ||
plugin = this; | ||
// Plugin startup logic | ||
if (getClass("io.papermc.paper.threadedregions.RegionizedServer")) { | ||
scheduler = new FoliaTasks(this); | ||
} else { | ||
scheduler = new BukkitTasks(this); | ||
} | ||
PluginManager manager = Bukkit.getServer().getPluginManager(); | ||
manager.registerEvents(new PrepareCraft(),this); | ||
manager.registerEvents(new CraftedItem(), this); | ||
manager.registerEvents(new DiscoverRecipe(), this); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
// Plugin shutdown logic | ||
} | ||
|
||
boolean getClass(String c) { | ||
try { | ||
Class.forName(c); | ||
return true; | ||
} catch (ClassNotFoundException e) { | ||
return false; | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
core/src/main/java/me/autobot/resbcrafter/constants/Items.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package me.autobot.resbcrafter.constants; | ||
|
||
import org.bukkit.Material; | ||
import org.bukkit.inventory.ItemStack; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public interface Items { | ||
List<Material> SHULKER_BOXES = new ArrayList<>() {{ | ||
add(Material.SHULKER_BOX); | ||
add(Material.WHITE_SHULKER_BOX); | ||
add(Material.ORANGE_SHULKER_BOX); | ||
add(Material.MAGENTA_SHULKER_BOX); | ||
add(Material.LIGHT_BLUE_SHULKER_BOX); | ||
add(Material.YELLOW_SHULKER_BOX); | ||
add(Material.LIME_SHULKER_BOX); | ||
add(Material.PINK_SHULKER_BOX); | ||
add(Material.GRAY_SHULKER_BOX); | ||
add(Material.LIGHT_GRAY_SHULKER_BOX); | ||
add(Material.CYAN_SHULKER_BOX); | ||
add(Material.PURPLE_SHULKER_BOX); | ||
add(Material.BLUE_SHULKER_BOX); | ||
add(Material.BROWN_SHULKER_BOX); | ||
add(Material.GREEN_SHULKER_BOX); | ||
add(Material.RED_SHULKER_BOX); | ||
add(Material.BLACK_SHULKER_BOX); | ||
}}; | ||
/* | ||
ItemStack EMPTY_BOX = new ItemStack(Material.SHULKER_BOX); | ||
ItemStack EMPTY_WHITE_BOX = new ItemStack(Material.WHITE_SHULKER_BOX); | ||
ItemStack EMPTY_ORANGE_BOX = new ItemStack(Material.ORANGE_SHULKER_BOX); | ||
ItemStack EMPTY_MAGENTA_BOX = new ItemStack(Material.MAGENTA_SHULKER_BOX); | ||
ItemStack EMPTY_LIGHT_BLUE_BOX = new ItemStack(Material.LIGHT_BLUE_SHULKER_BOX); | ||
ItemStack EMPTY_YELLOW_BOX = new ItemStack(Material.YELLOW_SHULKER_BOX); | ||
ItemStack EMPTY_LIME_BOX = new ItemStack(Material.LIME_SHULKER_BOX); | ||
ItemStack EMPTY_PINK_BOX = new ItemStack(Material.PINK_SHULKER_BOX); | ||
ItemStack EMPTY_GRAY_BOX = new ItemStack(Material.GRAY_SHULKER_BOX); | ||
ItemStack EMPTY_LIGHT_GRAY_BOX = new ItemStack(Material.LIGHT_GRAY_SHULKER_BOX); | ||
ItemStack EMPTY_CYAN_BOX = new ItemStack(Material.CYAN_SHULKER_BOX); | ||
ItemStack EMPTY_PURPLE_BOX = new ItemStack(Material.PURPLE_SHULKER_BOX); | ||
ItemStack EMPTY_BLUE_BOX = new ItemStack(Material.BLUE_SHULKER_BOX); | ||
ItemStack EMPTY_BROWN_BOX = new ItemStack(Material.BROWN_SHULKER_BOX); | ||
ItemStack EMPTY_GREEN_BOX = new ItemStack(Material.GREEN_SHULKER_BOX); | ||
ItemStack EMPTY_RED_BOX = new ItemStack(Material.RED_SHULKER_BOX); | ||
ItemStack EMPTY_BLACK_BOX = new ItemStack(Material.BLACK_SHULKER_BOX); | ||
*/ | ||
|
||
ItemStack AIR = new ItemStack(Material.AIR); | ||
} |
151 changes: 151 additions & 0 deletions
151
core/src/main/java/me/autobot/resbcrafter/helper/RecipeHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package me.autobot.resbcrafter.helper; | ||
|
||
import me.autobot.resbcrafter.constants.Items; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.inventory.CraftingInventory; | ||
import org.bukkit.inventory.ItemStack; | ||
import org.bukkit.inventory.Recipe; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class RecipeHelper { | ||
public final CraftingInventory craftingInventory; | ||
public final ItemStack[] originalMatrix; | ||
public int[] originalMatrixWithBoxColor = new int[] {-3,-3,-3,-3,-3,-3,-3,-3,-3}; | ||
//public final Map<Integer, ItemStack[]> originalBoxContent = new HashMap<>(); | ||
public final Map<Integer, ItemStack> condensedBoxContent = new HashMap<>(); | ||
public ItemStack[] condensedMatrix = new ItemStack[9]; | ||
//public int[] condensedContentMaxCount = new int[9]; | ||
public int firstBoxIndex = -1; | ||
public int leastMaxStackSize = 64; | ||
public int mostMaxStackSize = 0; | ||
public Player player; | ||
public RecipeHelper(CraftingInventory inventory) { | ||
this.craftingInventory = inventory; | ||
this.originalMatrix = inventory.getMatrix(); | ||
getMatrixContent(); | ||
//System.out.println(Arrays.toString(originalMatrixWithBoxColor)); | ||
getShulkerBoxContent(); | ||
//originalBoxContent.values().forEach(itemStacks -> System.out.println(Arrays.toString(itemStacks))); | ||
this.player = (Player) craftingInventory.getViewers().getFirst(); | ||
} | ||
|
||
public void getMatrixContent() { | ||
// -3 for placeholder, to determine player 4x4 crafting | ||
// -2 for empty item stack | ||
// -1 for non-shulker box | ||
for (int matrixIndex = 0; matrixIndex < this.originalMatrix.length; matrixIndex++) { | ||
ItemStack itemStack = this.originalMatrix[matrixIndex]; | ||
if (isEmpty(itemStack)) { | ||
this.originalMatrixWithBoxColor[matrixIndex] = -2; | ||
continue; | ||
} | ||
this.originalMatrixWithBoxColor[matrixIndex] = Items.SHULKER_BOXES.indexOf(itemStack.getType()); | ||
} | ||
} | ||
public void getShulkerBoxContent() { | ||
int matrixIndex = 0; | ||
for (int colorIndex : originalMatrixWithBoxColor) { | ||
// 0 - 15 | ||
if (colorIndex < 0) { | ||
matrixIndex++; | ||
continue; | ||
} | ||
ItemStack[] boxContents = ShulkerBoxHelper.convertShulkerBox(this.originalMatrix[matrixIndex]).getInventory().getContents(); | ||
//originalBoxContent.put(matrixIndex, boxContents); | ||
getCondensedBoxContent(matrixIndex, boxContents); | ||
matrixIndex++; | ||
} | ||
|
||
// Not using toArray, not retaining shape | ||
this.condensedBoxContent.forEach((index, itemStack) -> { | ||
if (itemStack != null) { | ||
ItemStack clone = new ItemStack(itemStack); | ||
//clone.setAmount(1); | ||
condensedMatrix[index] = clone; | ||
//condensedContentMaxCount[index] = clone.getMaxStackSize(); | ||
} | ||
}); | ||
//System.out.println(Arrays.toString(condensedMatrix)); | ||
} | ||
public void getCondensedBoxContent(int matrixIndex, ItemStack[] itemStacks) { | ||
// Beware of empty box | ||
long itemTypes = Arrays.stream(itemStacks).distinct().count(); | ||
// Mixed content | ||
if (itemTypes > 1) { | ||
this.condensedBoxContent.put(matrixIndex, null); | ||
return; | ||
} | ||
// Fill air if null content | ||
if (isEmpty(itemStacks[0])) { | ||
this.condensedBoxContent.put(matrixIndex, Items.AIR); | ||
} else { | ||
this.condensedBoxContent.put(matrixIndex, itemStacks[0]); | ||
int stackSize = itemStacks[0].getMaxStackSize(); | ||
if (firstBoxIndex == -1) { | ||
firstBoxIndex = matrixIndex; | ||
} | ||
if (stackSize < leastMaxStackSize) { | ||
leastMaxStackSize = stackSize; | ||
firstBoxIndex = matrixIndex; | ||
//System.out.println("LeastSize " + leastMaxStackSize); | ||
} | ||
if (stackSize > mostMaxStackSize) { | ||
mostMaxStackSize = stackSize; | ||
} | ||
} | ||
// if having the least max stack, firstBoxIndex is indicated to the least one | ||
//System.out.println("First " + firstBoxIndex); | ||
|
||
} | ||
public Recipe getRecipe() { | ||
// Not accept non-Shulker box and non-AIR items | ||
if (invalidMatrix()) { | ||
return null; | ||
} | ||
// This Recipe does not contain NBT | ||
return Bukkit.getServer().getCraftingRecipe(condensedMatrix, player.getWorld()); | ||
} | ||
public ItemStack getCondensedResult() { | ||
// Not accept non-Shulker box and non-AIR items | ||
if (invalidMatrix()) { | ||
return null; | ||
} | ||
// Simulate Player Crafting, Return Item with NBT / custom items | ||
// Does not consume items | ||
return Bukkit.craftItem(condensedMatrix, player.getWorld(), player); | ||
} | ||
public boolean invalidMatrix() { | ||
boolean impure = Arrays.stream(this.originalMatrixWithBoxColor).anyMatch(color -> color == -1); | ||
boolean allFull = Arrays.stream(this.originalMatrix) | ||
.filter(i -> i != null && !i.equals(Items.AIR)) | ||
.map(i -> ShulkerBoxHelper.convertShulkerBox(i).getInventory().getContents()) | ||
.flatMap(Arrays::stream) | ||
.filter(Objects::nonNull) | ||
.allMatch(c -> c.getAmount() == c.getMaxStackSize()); | ||
if (impure || !allFull) { | ||
return true; | ||
} | ||
// Mixed Content is presence | ||
return this.condensedBoxContent.containsValue(null); | ||
} | ||
public boolean isEmpty(ItemStack itemStack) { | ||
return itemStack == null || itemStack.isSimilar(Items.AIR); | ||
} | ||
public long getInputBoxCount() { | ||
return Arrays.stream(originalMatrixWithBoxColor).filter(i -> i >= 0).count(); | ||
} | ||
/* | ||
public long getEmptyBoxCount() { | ||
return condensedBoxContent.values().stream().filter(i -> i.equals(Items.AIR)).count(); | ||
} | ||
*/ | ||
public boolean mixStackCrafting() { | ||
return leastMaxStackSize != 64 && mostMaxStackSize != 0 && leastMaxStackSize != mostMaxStackSize; | ||
} | ||
} |
Oops, something went wrong.